I think the following should fix the compile errors. Presumably they are prompted by compilers more sensitive nowadays than they were back in the days when this book was written:
- lexer.l: remove the declaration enum yytokentype which is shipped by including parser.tab.h - parser.y: add forward declarations int yylex(void); and void yyerrror(char*); to, e.g., the section where headers are included. Separately, you could drop '-lfl' from the compile statement in case you add %option noyywrap to the top of lexer.l. I have not tested that, just from reading the code. HTH J. On Sun, 17 Feb 2019 12:57:03 +0100, [email protected] wrote: > I've managed to come a bit more forward, now my lexer.l files look like > this: > > %{ > #include <stdio.h> > #include <stdlib.h> > #include "./parser.tab.h" > enum yytokentype { > NUMBER = 285, > VAL = 292, > ADD = 286, > SUB = 287, > MUL = 288, > DIV = 289, > ABS = 290, > EOL = 291 > }; > int yylval; > %} > > %% > > "+" { return ADD; } > "-" { return SUB; } > "*" { return MUL; } > "/" { return DIV; } > "|" { return ABS; } > [0-9]+ { yylval = atoi(yytext); return NUMBER; } \n { > return EOL; } [ > \t] { /* ignore whitespaces */ } . { printf("Unknow char > :%c", > *yytext); } > > %% > > > parser.y: > > %{ > #include <stdio.h> > #include <stdlib.h> > %} > > > %token NUMBER > %token ADD SUB MUL DIV ABS > %token EOL > > %% > > calclist: /* nothing */ > | calclist exp EOL { printf(" = %d\n", $2); } > ; > > 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(int argc, char **argv){ > yyparse(); > return(0); > } > > int yyerror(char *s){ > fprintf(stderr, "error: %s\n", s); > return(0); > } > > I can compile with flex -l lexer.l; bison -d parser.y; gcc -o parser > parser.tab.c > lex.yy.c -lfl but i get the following error messages: > > > parser.tab.c: In function ‘yyparse’: > parser.tab.c:1124:16: warning: implicit declaration of function ‘yylex’ > [-Wimplicit-function-declaration] > yychar = yylex (); > ^~~~~ > parser.tab.c:1289:7: warning: implicit declaration of function ‘yyerror’ > [-Wimplicit-function-declaration] > yyerror (YY_("syntax error")); > ^~~~~~~ > lexer.l:5:6: error: nested redefinition of ‘enum yytokentype’ > enum yytokentype { > ^~~~~~~~~~~ > lexer.l:5:6: error: redeclaration of ‘enum yytokentype’ > In file included from lexer.l:4:0: > ./parser.tab.h:46:8: note: originally defined here > enum yytokentype > ^~~~~~~~~~~ > lexer.l:6:2: error: redeclaration of enumerator ‘NUMBER’ > NUMBER = 285, > ^~~~~~ > In file included from lexer.l:4:0: > ./parser.tab.h:48:5: note: previous definition of ‘NUMBER’ was here > NUMBER = 258, > ^~~~~~ > lexer.l:8:2: error: redeclaration of enumerator ‘ADD’ > ADD = 286, > ^~~ > In file included from lexer.l:4:0: > ./parser.tab.h:49:5: note: previous definition of ‘ADD’ was here > ADD = 259, > ^~~ > lexer.l:9:2: error: redeclaration of enumerator ‘SUB’ > SUB = 287, > ^~~ > In file included from lexer.l:4:0: > ./parser.tab.h:50:5: note: previous definition of ‘SUB’ was here > SUB = 260, > ^~~ > lexer.l:10:2: error: redeclaration of enumerator ‘MUL’ > MUL = 288, > ^~~ > In file included from lexer.l:4:0: > ./parser.tab.h:51:5: note: previous definition of ‘MUL’ was here > MUL = 261, > ^~~ > lexer.l:11:2: error: redeclaration of enumerator ‘DIV’ > DIV = 289, > ^~~ > In file included from lexer.l:4:0: > ./parser.tab.h:52:5: note: previous definition of ‘DIV’ was here > DIV = 262, > ^~~ > lexer.l:12:2: error: redeclaration of enumerator ‘ABS’ > ABS = 290, > ^~~ > In file included from lexer.l:4:0: > ./parser.tab.h:53:5: note: previous definition of ‘ABS’ was here > ABS = 263, > ^~~ > lexer.l:13:2: error: redeclaration of enumerator ‘EOL’ > EOL = 291 > ^~~ > In file included from lexer.l:4:0: > ./parser.tab.h:54:5: note: previous definition of ‘EOL’ was here > EOL = 264 > ^~~ > > > > _______________________________________________ > [email protected] https://lists.gnu.org/mailman/listinfo/help-bison _______________________________________________ [email protected] https://lists.gnu.org/mailman/listinfo/help-bison
