0

我正在尝试编译并执行以下简单程序:

计算器.y

%{
    #include <stdio.h>
    int yylex(void);
    void yyerror(char *);
%}

%token INTEGER

%%

program:
    program expr '\n'       { printf("%d\n", $2); }
    |
    ;

expr:
    INTEGER                 { $$ = $1; }
    | expr '+' expr         { $$ = $1 + $3; }
    | expr '-' expr         { $$ = $1 - $3; }

%%

void yyerror(char *s) {
    fprintf(stderr, "%s\n", s);
}

int main(void) {
    yyparse();
    return 0;
}

计算器.l

%{
    #include <stdlib.h>
    void yyerror(char *);
    #include "y.tab.h"
%}

%%

[0-9]+      {
                yylval = atoi(yytext);
                return INTEGER;
            }

[-+\n]      return *yytext;
[ \t]       ; /* skip whitespace */
.           yyerror("invalid character");

%%

int yywrap(void) {
    return 1;
}

所以我用命令编译:

yacc -d calculator.y -> it generates "y.tab.c and y.tab.h"

lex calculator.l -> it generates "lex.yy.c"

之后,我尝试将文件链接并编译:

cc lex.yy.c y.tab.h -ocalc

但我有以下错误:

$ cc lex.yy.c y.tab.h -ocalc /usr/bin/ld:
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation
0 has invalid symbol index 11 /usr/bin/ld:
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation
1 has invalid symbol index 12 /usr/bin/ld:
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation
2 has invalid symbol index 2 /usr/bin/ld:
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation
3 has invalid symbol index 2 /usr/bin/ld:
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation
4 has invalid symbol index 11 /usr/bin/ld:
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation
5 has invalid symbol index 13 /usr/bin/ld:
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation
6 has invalid symbol index 13 /usr/bin/ld:
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation
7 has invalid symbol index 13 /usr/bin/ld:
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation
8 has invalid symbol index 2 /usr/bin/ld:
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation
9 has invalid symbol index 2 /usr/bin/ld:
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation
10 has invalid symbol index 12 /usr/bin/ld:
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation
11 has invalid symbol index 13 /usr/bin/ld:
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation
12 has invalid symbol index 13 /usr/bin/ld:
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation
13 has invalid symbol index 13 /usr/bin/ld:
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation
14 has invalid symbol index 13 /usr/bin/ld:
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation
15 has invalid symbol index 13 /usr/bin/ld:
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation
16 has invalid symbol index 13 /usr/bin/ld:
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation
17 has invalid symbol index 13 /usr/bin/ld:
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation
18 has invalid symbol index 13 /usr/bin/ld:
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation
19 has invalid symbol index 13 /usr/bin/ld:
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation
20 has invalid symbol index 13 /usr/bin/ld:
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation
21 has invalid symbol index 13 /usr/bin/ld:
/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation
22 has invalid symbol index 21
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o: In
function `_start': (.text+0x18): undefined reference to `main'
/tmp/ccvaf6Nh.o: In function `yylex': lex.yy.c:(.text+0x1df):
undefined reference to `yylval' lex.yy.c:(.text+0x205): undefined
reference to `yyerror' collect2: ld returned 1 exit status

任何人都知道如何解决这个问题?提前致谢。

4

1 回答 1

2

这看起来像是您的 C 库中的损坏。重新安装编译器。

您应该编译 y.tab.c,而不是 y.tab.h。

于 2012-11-09T02:53:28.143 回答