未定义的引用'yylex'野牛错误

问题描述:

您好我是野牛和flex的新手,我试图创建一个简单的计算器,但我似乎在编译时出现错误。未定义的引用'yylex'野牛错误

以下是我的Flex .L文件(名为人):

%{ 
#include "a.tab.h" 
%} 
number [0-9]+ 

%% 

"+"  {return ADD;} 
"-"  {return SUB;} 
"*"  {return MUL;} 
"/"  {return DIV;} 
"|"  {return ABS;} 
{number}  { return NUMBER;} 
\n  {return EOF;} 
[ \t] { } 
. {printf("Mystery Character %s\n", yytext); } 

%% 

和下面是我的野牛.Y文件(名为AY):

%{ 
#include <stdio.h> 
int yyparse(void); 
%} 

%token NUMBER ADD SUB MUL DIV ABS EOL 

%% 

calclist: /*nothing*/ | calclist exp EOL { printf("=%d\n, $1") }; 

exp: factor 
    | exp ADD factor { $$ = $1 + $3; } 
    | exp SUB factor { $$ = $1 - $3; } 
    ; 

factor: term | 
     | factor MUL term { $$ = $1 * $3; } 
     | factor DIV term { $$ = $1/$3; } 

term: NUMBER 
    | ABS term { $$ = $2 >= 0? $2 : - $2; } 
    ; 

%% 

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

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

这是我写的在控制台:

flex a.l 
bison a.y 
gcc a.tab.c -lfl -o a.exe 

错误我得到的是:

a.tab.c:(.text+0x1f2): undefined reference to `yylex' 
collect2.exe: error: ld returned 1 exit status 

我也得到了以下警告:

a.tab.c: In function 'yyparse': 
a.tab.c:595:16: warning: implicit declaration of function 'yylex' [-Wimplicit-function-declaration] 
# define YYLEX yylex() 
      ^
a.tab.c:1240:16: note: in expansion of macro 'YYLEX' 
     yychar = YYLEX; 
       ^~~~~ 
a.tab.c:1396:7: warning: implicit declaration of function 'yyerror' [-Wimplicit-function-declaration] 
     yyerror (YY_("syntax error")); 
     ^~~~~~~ 
a.y: At top level: 
a.y:32:6: warning: conflicting types for 'yyerror' 
void yyerror(char *s) 
     ^~~~~~~ 
a.tab.c:1396:7: note: previous implicit declaration of 'yyerror' was here 
     yyerror (YY_("syntax error")); 
     ^~~~~~~ 

会有人能向我解释为什么这些错误/警告可能发生?

会有两个生成的C文件,一个由flex生成,另一个由bison生成。由flex创建的名称将被称为“lex.yy.c”,您还需要编译该文件。

+0

您好我尝试了这些命令,我​​仍然得到错误-o A-lex.exe的lex.yy.c -lfl 野牛唉 GCC a.tab.c的lex.yy.c – user8786494

+0

挠人 GCC -lfl -o a.exe – user8786494

+0

您需要将两个c文件一起编译。 'gcc -o a -Wall a.tab.c lex.yy.c -lfl' – rici