2011-03-27 5 views
6

이것은 처음으로 부목 (우분투 저장소에서 사용)을 사용하는 것이므로 즉시 WTF에 맞았습니다. 오류 메시지 :이 프로그램이 오류없이 컴파일합니다부목 디버깅 구문 분석 오류

#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이라는 이름으로 불쾌감을 느낀다면 함께 사용해보십시오. 저자가 저 방법으로 명명 한 프로그래밍 언어입니다. 나는 그 이름을 존중하고 사용합니다.

답변

11

부목 C99는 알고 있습니까?

대신 // .../* ... */을 시도하고 부목를 호출 할 때 또한 +slashslashcomment 사용할 수있는 코드

+0

OMG 나는 한 줄 주석이 C99에 도입 알지도 못하는 (코멘트에 넣고,하지만 필요한 담당자가 없습니다). 제 컴파일러는'-std = c99'가 없어도 불평하지 않았습니다. 슬프게도 이것이 내 문제를 해결하지는 못합니다. – orlp

+0

내 게시물에 또 다른 C99 것을 추가했습니다. (코드와 혼합 된 선언도 새 것입니다.) – pmg

+0

@nightcracker : 확장자로'// '를 사용할 수있는 컴파일러가 상당수 있지만 공식적으로 C99까지는 언어의 일부가 아닙니다. –

1

이전에 선언을 이동합니다. 이 경우 :

splint +slashslashcomment brainfuck.c


부목 설명서의 Appendix B :

P:- slashslashcomment은

// 주석이 사용된다. ISO C99는 // 주석을 허용하지만 이전의 표준은 그렇지 않습니다.