Hallo Ich bin neu zu Bison und Flex und ich versuche, einen einfachen Rechner zu erstellen, aber ich habe Fehler beim Kompilieren zu haben. Undefinierter Verweis auf'Yylex 'Bison Fehler
Das folgende ist meine flex .l-Datei (mit dem Namen al):
%{
#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); }
%%
und das folgende ist mein Bison .y-Datei (genannt 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);
}
Das ist, was ich schreibe in der Konsole:
flex a.l bison a.y gcc a.tab.c -lfl -o a.exe
Die Fehler, die ich bekommen ist:
a.tab.c:(.text+0x1f2): undefined reference to `yylex' collect2.exe: error: ld returned 1 exit status
Ich habe auch die folgenden Warnungen:
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")); ^~~~~~~
Würde jemand mir erklären können, warum diese Fehler/Warnungen können passiert sein?
Hallo Ich habe diese Befehle versucht, und ich bin immer noch der Fehler – user8786494
flex al gcc -o a-lex.exe lex.yy.c -lfl Bison ay gcc a.tab.c lex.yy.c bekommen -lfl -o a.exe – user8786494
Sie müssen beide c-Dateien zusammen kompilieren. 'gcc -o a -Wall a.tab.c lex.yy.c -lfl' – rici