夹板调试解析错误

发布于 2024-10-27 03:15:49 字数 4838 浏览 1 评论 0原文

这是我第一次使用 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.)
*** Cannot continue.

现在,显然它在第 16 行第 6 列上看到了一些错误。让我们检查一下(发布完整代码):

#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;
}

请注意,该程序编译时没有错误 (gcc -Wall -std=c99 Brainfuck.c< /code>)并且运行时表现正常。

注意:如果你被“brainfuck”这个名字冒犯了,那就接受它吧。这是一种由作者这样命名的编程语言,我尊重并使用这个名称。

This is my first time using splint (from Ubuntu repositories) and I immediately got hit by a WTF. The error message:

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.

Now, apparently it sees something wrong on line 16, column 6. Let's check that out (posting full code):

#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;
}

Note that this program compiles without errors (gcc -Wall -std=c99 brainfuck.c) and runtime behaves normal.

Note: if you are offended by the name brainfuck, live with it. It's a programming language that is named that way by the author and I respect and use that name.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生生漫 2024-11-03 03:15:49

夹板C99知道吗?

尝试使用 /* ... */ 而不是 // ... 并将声明移至任何代码之前

Is splint C99 aware?

Try /* ... */ instead of // ... and move declarations to before any code

じее 2024-11-03 03:15:49

调用 splint 时也可以使用+slashslashcomment。在本例中:

splint +slashslashcomment Brainfuck.c


Splint Manual's Appendix B:

P:- 斜杠斜杠注释

使用 // 注释。 ISO C99 允许 // 注释,但更早
标准没有。

(会将其放在评论中,但没有必要的代表)

You can also use +slashslashcomment when calling splint. In this case:

splint +slashslashcomment brainfuck.c


Splint Manual's Appendix B:

P:- slashslashcomment

A // comment is used. ISO C99 allows // comments, but earlier
standards did not.

(would put this in a comment, but don't have the necessary rep)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文