I am working on Lex and Yacc program but I am getting error as :
/tmp/ccTP9YY1.o: In function
yylex': lex.yy.c:(.text+0x289): undefined reference toyylval' 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;
}
yylvalis 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. - riciy.tab.htoy.tab.cin thegcccommand. Compiling a header file doesn't do what you think it does, and it wouldn't help even if it did because the definition ofyylvalis not in the header file, only the declaration. (You already #include the header file generated inlex.yy.c, soyylvalis declared. The linker error is because it is never defined.) - rici