English 中文(简体)
splint debugging parse error
原标题:
  • 时间:2011-03-27 01:43:24
  •  标签:
  • c
  • splint

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
");
        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
");
        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
");
        return 3;
    }

    // read source file in memory
    if (srclen != fread(bf, sizeof(char), srclen, srcfile)) {
        fprintf(stderr, "ERROR: Error while reading source file
");
        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
");
        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
");
                        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.

最佳回答

Is splint C99 aware?

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

问题回答

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)





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签