This is my flex code
%{
#include "fl.tab.h"
%}
%%
[0-9]+ { yylval = atoi(yytext);
return INTEGER; }
\n return 0;
[ \t] ;
. return yytext[0];
%%
And my bison code
%{
#include <stdio.h>
%}
%token INTEGER
%left '+' '-'
%left '*'
%%
Statement : expr {printf("%d\n",$1);}
;
expr : expr '+' INTEGER {$$ = $1 + $3;}
| expr '-' INTEGER {$$ = $1 - $3;}
| expr '*' INTEGER {$$ = $1 * $3;}
| INTEGER {$$ = $1;}
;
%%
int main(void){
yyparse();
return 0;
}
when i input 4 + 5 * 2 it give the output as 18. But the correct answer should be 14. Where am i getting it wrong ?