编译lex和yacc文件时出现大量错误
我试图用yacc/lex制作一个简单的计算器,但是我不断地得到大量的错误,其中很多人说错误在生成的文件中 我运行编译lex和yacc文件时出现大量错误,c,flex-lexer,yacc,C,Flex Lexer,Yacc,我试图用yacc/lex制作一个简单的计算器,但是我不断地得到大量的错误,其中很多人说错误在生成的文件中 我运行gcc lex.yy.cy.tab.c-o minicalc并得到如下错误 bas.y:34:16: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token int main(void) { y.tab.c:499:2: error: expected declaration specifier
gcc lex.yy.cy.tab.c-o minicalc
并得到如下错误
bas.y:34:16: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
int main(void) {
y.tab.c:499:2: error: expected declaration specifiers before ‘;’ token
};
In file included from lex.yy.c:459:0:
/usr/include/unistd.h: In function ‘yyerror’:
/usr/include/unistd.h:258:22: error: storage class specified for parameter ‘useconds_t’
typedef __useconds_t useconds_t;
^~~~~~~~~~
这些是最常见的,但还有更多。问题是,我会犯这样的错误
bas.y:34:16: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
int main(void) {
y.tab.c:499:2: error: expected declaration specifiers before ‘;’ token
};
In file included from lex.yy.c:459:0:
/usr/include/unistd.h: In function ‘yyerror’:
/usr/include/unistd.h:258:22: error: storage class specified for parameter ‘useconds_t’
typedef __useconds_t useconds_t;
^~~~~~~~~~
这使得我的代码中似乎没有错误
这是我的莱克斯密码:
%{
#include <stdlib.h>
#include "y.tab.h"
void yyerror(char *)
%}
%%
/* a is value of last expresion */
a {
yyval = *yytext - 'a';
return LAST;
}
/* integers */
[0-9]+ {
yyval = atoi(yytext);
return INTEGER;
}
/* operators */
[-+()=/*\n] { return *yytext; }
/* skip whitespace */
[ \t] { ; }
/* all else is error */
. yyerror("invalid character");
%%
int yywrap(void) {
return 1;
}
提前感谢。您在
.y
和.l
文件中的无效yyerror(char*)
后缺少分号。因此编译器需要一个代码>在生成的代码中紧跟其后的行上,导致您看到的错误消息。您在.y
和.l
文件中的void yyerror(char*)
之后缺少分号。因此编译器需要一个代码>在生成的代码中紧跟其后的行上,导致您看到的错误消息