Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I've been following the youtube video https://www.youtube.com/watch?v=fFRxWtRibC8

I've created the lexer.l file with the following:

/* definitions */ #include "parser.tab.h" /* rules */ [0-9]+ {yylval.num = atoi(yytext); return NUMBER; } \n {return EOL;} yywrap() {} int main() { yylex(); return 0;

and the parser.y file with the following:

/* definitions */ %union{ int num; char sym; %token EOL %token<num> NUMBER %type<num> exp %token PLUS /* rules */ input: exp EOL {printf("%d\n", $1); } | EOL; NUMBER { $$ = $1; } | exp PLUS exp { $$ = $1 + $3; } int main() { yyparse(); return 0; void yyerror (char* s) { printf ("ERROR: %s\n", s); return 0;

When running the command "gcc lex.yy.c parser.tab.c", I'm getting the following error: undefined reference to `yyerror' collect2.exe: error: ld returned 1 exit status

look in your c files if the function yyerror() is there somewhere, delete all your generated c code and create them again, use a build tool to never forget to generate new code when needed rioV8 Dec 11, 2022 at 15:52 I don't see how that can work at all, since you have a main() definition in both of the source files. So I presume you're not following the video you refer to precisely (it notes that you need to delete main() from the lexer file). That doesn't explain why the linker can't find yyerror , which is apparently defined. Please edit your question to include a minimal reproducible example , consisting of the exact files you are using and all of the commands you ran in order, along with all error messages. rici Dec 12, 2022 at 17:17 By the way, yywrap() {} is really awful. It's Undefined Behaviour because yywrap must return an int and that function doesn't return anything. (which doesn't even specify the return type, also incorrect). Brushing it under the rug ("it is giving me a warning which I'm going to ignore for now") is a pretty good sign that you should find a different tutorial. Try reading the introduction to the Bison manual (skip the section on GLR parsers) and go through the examples rici Dec 12, 2022 at 17:29

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question . Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers .