解析器代码中奇怪的编译器错误

时间:2012-01-06 10:22:27

标签: c parsing compiler-errors undefined-behavior

Parser.h

enum { PLUS, MINUS, DIVIDE, MULTIPLY, NUMBER, END } type;

int token;

/* parsing functions */
void parse_token (void);

Parser.c

void get_token (void)
{       
     token++;   
     parse_token(); /* LINE 11 */
}

void parse_token (void) /* LINE 14 */
{
    if ( strchr ("1234567890.", token) )
        type = NUMBER;

    else if ( strchr ("+", token) )
        type = PLUS;

    else if ( strchr ("-", token) )
        type = MINUS;

    else if ( strchr ("/", token) )
        type = DIVIDE;

    else if ( strchr ("*",token) )
        type = MULTIPLY;

    else if ( token == '\0' )
        type = END;
    else 
        show_error(strcat("Couldn't parse token : ", token));
}

错误

parser.c:14:6: warning: conflicting types for ‘parse_token’ [enabled by default]
parser.c:11:2: note: previous implicit declaration of ‘parse_token’ was here
parser.c: In function ‘parse_token’:
parser.c:16:2: warning: passing argument 2 of ‘strchr’ makes integer from pointer without a cast [enabled by default]
/usr/include/string.h:235:14: note: expected ‘int’ but argument is of type ‘char *’
parser.c:17:3: error: ‘type’ undeclared (first use in this function)
parser.c:17:3: note: each undeclared identifier is reported only once for each function it appears in
parser.c:17:10: error: ‘NUMBER’ undeclared (first use in this function)
parser.c:19:2: warning: passing argument 2 of ‘strchr’ makes integer from pointer without a cast [enabled by default]
/usr/include/string.h:235:14: note: expected ‘int’ but argument is of type ‘char *’
parser.c:20:10: error: ‘PLUS’ undeclared (first use in this function)
parser.c:22:2: warning: passing argument 2 of ‘strchr’ makes integer from pointer without a cast [enabled by default]
/usr/include/string.h:235:14: note: expected ‘int’ but argument is of type ‘char *’
parser.c:23:10: error: ‘MINUS’ undeclared (first use in this function)
parser.c:25:2: warning: passing argument 2 of ‘strchr’ makes integer from pointer without a cast [enabled by default]
/usr/include/string.h:235:14: note: expected ‘int’ but argument is of type ‘char *’
parser.c:26:10: error: ‘DIVIDE’ undeclared (first use in this function)
parser.c:28:2: warning: passing argument 2 of ‘strchr’ makes integer from pointer without a cast [enabled by default]
/usr/include/string.h:235:14: note: expected ‘int’ but argument is of type ‘char *’
parser.c:29:10: error: ‘MULTIPLY’ undeclared (first use in this function)
parser.c:32:10: error: ‘END’ undeclared (first use in this function)
parser.c: In function ‘show_error’:
parser.c:40:2: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]

我完全是竹子。 :(

任何帮助?

2 个答案:

答案 0 :(得分:5)

你可以编译它(通过包含标题,如Luchian Grigore所说),你会发现你不能对常量字符串strcat()进行编译。

常量字符串在只读内存中分配,不能修改。即使你可以修改它,你也会覆盖内存中的其他东西。

答案 1 :(得分:3)

您没有提供标题,因此翻译单位无法了解typetoken的声明。

你需要:

#include "Parser.h"

在实施文件的开头。