यह मेरी पहली बार स्प्लिंट (उबंटू रिपॉजिटरीज़ से) का उपयोग कर रहा है और मुझे तुरंत डब्ल्यूटीएफ द्वारा मारा गया। त्रुटि संदेश:स्प्लिंट डीबगिंग पार्स त्रुटि
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum {
CELL_CHUNK_SIZE = 1024,
};
typedef unsigned char cell;
int main(int argc, char *argv[]) {
if (argc < 1) {
fprintf(stderr, "ERROR: Not enough arguments\n");
return 1;
}
FILE *srcfile; // source file << THIS LINE APPARENTLY IS WRONG
long srclen; // source file size
char *bf; // brainfuck code file in memory
char *ip; // instruction pointer
cell *cells; // brainfuck cells
cell *newcells; // used for creating a new chunk of cells
cell *cp; // cell pointer
unsigned long numcells = CELL_CHUNK_SIZE; // amount of current cells
unsigned nest; // current nesting
int buf; // i/o buffer
srcfile = fopen(argv[1], "rb");
if (srcfile == NULL) {
fprintf(stderr, "ERROR: Couldn't open source file\n");
return 2;
}
// get source file length
fseek(srcfile, 0, SEEK_END);
srclen = ftell(srcfile);
fseek(srcfile, 0, SEEK_SET);
// allocate memory for source file
bf = malloc(srclen);
if (bf == NULL) {
fprintf(stderr, "ERROR: Couldn't allocate memory for source file\n");
return 3;
}
// read source file in memory
if (srclen != fread(bf, sizeof(char), srclen, srcfile)) {
fprintf(stderr, "ERROR: Error while reading source file\n");
free(bf);
return 4;
}
fclose(srcfile);
cells = malloc(CELL_CHUNK_SIZE * sizeof(cell));
memset(cells, 0, CELL_CHUNK_SIZE);
if (cells == NULL) {
fprintf(stderr, "ERROR: Memory allocation failed\n");
free(bf);
free(cells);
return 5;
}
cp = cells; // cell pointer initialized to most-left cell
ip = bf; // instruction pointer initialized to first character
nest = 0;
while (ip >= bf && ip <= (bf + srclen)) {
switch (*ip) {
case '+':
(*cp)++;
break;
case '-':
(*cp)--;
break;
case '>':
cp++;
if ((cp - cells) == numcells) {
newcells = realloc(cells, (numcells + CELL_CHUNK_SIZE) * sizeof(cell)); // allocate memory for new chunk
if (newcells == NULL) {
fprintf(stderr, "ERROR: Memory allocation failed\n");
free(bf);
free(cells);
return 5;
}
cp = newcells + (cp - cells); // point cell pointer to cell in new chunk
cells = newcells; // point cells to new memory location (if altered)
memset(cp, 0, CELL_CHUNK_SIZE); // initialize new chunk
numcells += CELL_CHUNK_SIZE;
}
break;
case '<':
cp--;
break;
case '.':
putchar(*cp);
break;
case ',':
if ((buf = getchar()) != EOF) {
*cp = (unsigned char) buf;
} else *cp = 0;
break;
case '[':
if (!(*cp)) {
ip++; // move past the opening bracket
while (nest > 0 || *ip != ']') { // skip to matching ]
if (*ip == '[') nest++; // enter nest
if (*ip == ']') nest--; // leave nest (or main loop, in which nesting > 0 fails)
ip++; // move right
}
}
break;
case ']':
if (*cp) {
ip--; // move before the closing bracket
while (nest > 0 || *ip != '[') { // rewind to matching [
if (*ip == '[') nest--; // leave nest (or main loop, in which nesting > 0 fails)
if (*ip == ']') nest++; // enter nest
ip--; // move left
}
ip--; // move before the opening bracket
}
break;
}
ip++; // move to next instruction
}
free(cells);
free(bf);
return 0;
}
ध्यान दें कि यह कार्यक्रम त्रुटियों के बिना संकलित (:
[email protected]:~/c/brainfuck$ splint brainfuck.c
Splint 3.1.2 --- 03 May 2009
brainfuck.c:17:6: Parse Error. (For help on parse errors, see splint -help
parseerrors.)
*** Cannot continue.
अब, जाहिरा तौर पर यह कुछ लाइन 16 पर गलत, स्तंभ 6. के कि बाहर की जाँच करते हैं (पूर्ण कोड पोस्टिंग) देखता है gcc -Wall -std=c99 brainfuck.c
) और रनटाइम सामान्य व्यवहार करता है।
नोट: यदि आप brainfuck नाम से नाराज हैं, तो इसके साथ रहें। यह एक प्रोग्रामिंग भाषा है जिसका नाम लेखक द्वारा किया जाता है और मैं उस नाम का सम्मान करता हूं और उसका उपयोग करता हूं।
OMG मैं कभी नहीं पता था कि एकल लाइन टिप्पणी c99 में पेश किए गए। मेरे कंपाइलर ने कभी भी शिकायत नहीं की, '-std = c99' के बिना भी। हालांकि दुख की बात यह है कि यह मेरी समस्या को ठीक नहीं करता है। – orlp
मैंने अपनी पोस्ट में एक और सी 99 चीज जोड़ दी है (कोड के साथ मिश्रित घोषणाएं भी नई हैं) – pmg
@ रात्रिभोजक: कुछ कंप्यूटर्स ने एक्सटेंशन के रूप में '//' स्वीकार किया है, लेकिन यह आधिकारिक तौर पर सी 99 तक भाषा का हिस्सा नहीं था। –