I am generating three address code for a c like program containing declaration,arithmetic,boolean, if and while statements. Currently i am beginning with arithmetic expression. I am reading the c like program from a text file.
Lex code:
Yacc code:
Input C like program(contents of test.txt)
a=1+2/3;
I have a make file like:
bison -d -v parser.y
flex -o parser.lex.c parser.lex
gcc -o cparser parser.lex.c parser.tab.c -lfl -lm
./cparser
When i compile my input file, i get the following output:
t1=2/3/3
t2=1+2/3;+t1
a=1+2/3;=t2
Parsing Successful. The three address code is:
syntax error
Successful parsing.
In your lexer code, you have things like:
{number} {yylval=yytext; return NUMBER;}
this will set $$
for that token to point at the lexer internal buffer which will be clobbered by the next call to yylex, so when you go to print it in the parser, you'll print some garbage. You need something like:
{number} {yylval=strdup(yytext); return NUMBER;}
In addition, you have patterns like:
'int' return INT;
The '
character is not special in any way to flex, so this pattern matches the 5-character sequence 'int'
.