[英]Yacc and Lex error compilation
I'm trying to compile and execute the following simple program: 我正在尝试编译并执行以下简单程序:
calculator.y calculator.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;
}
calculator.l calculator.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;
}
So I compile with the commands: 所以我用命令编译:
yacc -d calculator.y -> it generates "y.tab.c and y.tab.h"
lex calculator.l -> it generates "lex.yy.c"
And after that I try to link and compile the files together with that: 然后我尝试链接和编译文件:
cc lex.yy.c y.tab.h -ocalc
But I have the following error: 但是我有以下错误:
$ 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
Anyone knows how to solve that? 谁知道如何解决? Thanks in advance.
提前致谢。
This looks like a corruption in your C library. 这看起来像是C库中的损坏。 Reinstall the compiler.
重新安装编译器。
You should be compiling y.tab.c, not y.tab.h. 你应该编译y.tab.c,而不是y.tab.h.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.