相关文章推荐
神勇威武的馒头  ·  Unsafe code - C# ...·  6 月前    · 
绅士的炒饭  ·  Spring cloud ...·  1 年前    · 
玩足球的麻辣香锅  ·  Amazon.com : swag ...·  1 年前    · 
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

/tmp/ccTP9YY1.o: In function yylex': lex.yy.c:(.text+0x289): undefined reference to yylval' lex.yy.c:(.text+0x2a8): undefined reference to `yylval' collect2: error: ld returned 1 exit status

I have searched a lot on Internet like stackoverflow and other sites but none of them seems to capable of solving my problem. I might be implementing those suggestions incorrectly. Can anyone please suggest me anything about this ? Thanks in advance.

#include <stdlib.h> #include "y.tab.h" void yyerror(char *); [a-z] { yylval = *yytext - 'a'; return VARIABLE; [0-9]+ { yylval = atoi(yytext); return INTEGER; [-+()=/*\n] { return *yytext; } [ \t] ; . { yyerror("invalid character"); } void yyerror(char *s) { fprintf(stderr, "%s\n", s); int yywrap(void) { return 1;

Commands used :

rohit@rohit-HP-Pavilion-g4-Notebook-PC:~$ lex echo.l
rohit@rohit-HP-Pavilion-g4-Notebook-PC:~$ yacc -d yaccCalc.y 
rohit@rohit-HP-Pavilion-g4-Notebook-PC:~$ gcc lex.yy.c y.tab.h -ll
/tmp/ccTP9YY1.o: In function `yylex':
lex.yy.c:(.text+0x289): undefined reference to `yylval'
lex.yy.c:(.text+0x2a8): undefined reference to `yylval'
collect2: error: ld returned 1 exit status

Yacc file :

%token INTEGER VARIABLE
%left '+' '-'
%left '*' '/'
   void yyerror(char *);
   int yylex(void);
   int sym[26];
program:
    program statement '\n'
statement:
    expr                      { printf("%d\n", $1); }
    | VARIABLE '=' expr       { sym[$1] = $3; }
expr:
    INTEGER
    | VARIABLE                { $$ = sym[$1]; }
    | expr '+' expr           { $$ = $1 + $3; }
    | expr '-' expr           { $$ = $1 - $3; }
    | expr '*' expr           { $$ = $1 * $3; }
    | expr '/' expr           { $$ = $1 / $3; }
    | '(' expr ')'            { $$ = $2; }
void yyerror(char *s) {
   fprintf(stderr, "%s\n", s);
int main(void) {
   yyparse();
   return 0;
                You need to link the scanner with the parser (because yylval is defined in the parser). I'm certain that that is what the other answers say, but it is not possible to tell what error you made in attempting to accomplish it unless you reveal the precise commands you used to compile and link your project.
– rici
                Apr 29, 2017 at 7:57
                Change y.tab.h to y.tab.c in the gcc command. Compiling a header file doesn't do what you think it does, and it wouldn't help even if it did because the definition of yylval is not in the header file, only the declaration. (You already #include the header file generated in lex.yy.c, so yylval is declared. The linker error is because it is never defined.)
– rici
                Apr 29, 2017 at 13:57

Two files are generated by Yacc (and Bison), namely y.tab.h and y.tab.c assuming you do not override the output file with option -o. Include y.tab.h in your source code and compile y.tab.c. So perhaps you made a typo when trying to compile:

gcc lex.yy.c y.tab.c -ll
-------------------^-------- compile y.tab.c, not y.tab.h

You should compile the .c file, not the .h file. Below is the command you need to compile your lex and yacc program:

1. yacc -d yaccCalc.y
2. lex echo.l
3. cc lex.yy.c y.tab.c -ll -ly
        

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.