C splint调试分析错误
这是我第一次使用splint(来自Ubuntu存储库),我立即被WTF击中。错误消息:C splint调试分析错误,c,splint,C,Splint,这是我第一次使用splint(来自Ubuntu存储库),我立即被WTF击中。错误消息: nightcracker@nightcracker-pc:~/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.) *** Canno
nightcracker@nightcracker-pc:~/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列看到了一些错误。让我们检查一下(发布完整代码):
#包括
#包括
#包括
枚举{
单元块大小=1024,
};
typedef无符号字符单元;
int main(int argc,char*argv[]){
如果(argc<1){
fprintf(stderr,“错误:参数不足\n”);
返回1;
}
FILE*srcfile;//源文件=bf&&ip 0 | |*ip!=']'){//跳到匹配]
if(*ip='[')nest++;//输入nest
如果(*ip=']')嵌套--;//离开嵌套(或主循环,其中嵌套>0失败)
ip++;//向右移动
}
}
打破
案例']':
如果(*cp){
ip--;//在结束括号之前移动
而(nest>0 | |*ip!='['){//倒带到匹配[
如果(*ip=='[')嵌套--;//离开嵌套(或主循环,其中嵌套>0失败)
if(*ip=']')nest++;//输入nest
ip--;//向左移动
}
ip--;//在开口支架之前移动
}
打破
}
ip++;//移动到下一条指令
}
游离(细胞);
自由(bf);
返回0;
}
请注意,此程序编译时没有错误(gcc-Wall-std=c99 brainfuck.c
),运行时行为正常
注意:如果你被brainfuck这个名字冒犯了,就接受它。它是一种编程语言,作者以这种方式命名,我尊重并使用这个名字。splint C99知道吗
尝试
/*…*/
而不是/…
,并将声明移动到任何代码之前调用splint时,也可以使用+slashslashcomment
。在这种情况下:
splint+slashslashcomment.c
夹板手册:
p:-
slashslashcomment
使用//注释。ISO C99允许//注释,但更早
标准没有
(会将其放入注释中,但没有必要的代表)天哪,我从来都不知道c99中引入了单行注释。我的编译器从未抱怨过,即使没有
-std=c99
。尽管很遗憾,这并不能解决我的问题。我在我的帖子中添加了另一个c99内容(与代码混合的声明也是新的)@nightcracker:相当多的编译器接受了/
作为扩展,但直到C99它才正式成为语言的一部分。然后它被认为是一种不好的做法吗?我发现它更容易、更好,因为它不会阻止注释代码块。接受。移动声明起到了作用。我从不知道这一点他的:
#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;
}