This is a code for generating a 3 address code for the arithmetic expression.
The problem I am facing is that my grammar is able to read the input correctly till the last literal. But unable to reduce the last literal just before the '\n'
example : x = 1 + 2 * 3 - 4
1,2,3 are read properly. Even the minus. When it reads the 4 it is able to reduce the grammar for 2 steps. Then gives an error.
I Have put the print statements for help. This is the whole code. Problem while reading last literal:
x -> IDEN | NUM | ....
f -> x
t -> f (This doesn't happen)
icg.y
%{
#include<stdio.h>
#include<stdlib.h>
int temp_reg_num = 0;
void print_three_address_code(char* text1,char oper,char* text2)
{
printf("t%d = %s %c %s\n",temp_reg_num,text1,oper,text2);
}
%}
%token IDEN, NUM, FLOAT
%union {
char* text;
}
%left '+' '-'
%left '*' '/'
%%
s : IDEN '=' e '\n' {
printf("start\n");
if($3.text == NULL)
printf("%s = t%d\n",$1.text,temp_reg_num-1);
else
printf("%s = %s\n",$1.text,$3.text);
return 0;
}
;
e : e '+' t {
printf("e -> e + t\n");
char temp[5];
print_three_address_code($1.text,'+',$3.text);
sprintf(temp,"t%d",temp_reg_num++);
$$.text = copy_string(temp);
}
| e '-' t {
printf("e -> e - t\n");
char temp[5];
print_three_address_code($1.text,'-',$3.text);
sprintf(temp,"t%d",temp_reg_num++);
$$.text = copy_string(temp);
}
| t {
printf("e -> t\n");
$$.text = copy_string($1.text);
}
;
t : t '*' f {
printf("t -> t * f\n");
char temp[5];
print_three_address_code($1.text,'*',$3.text);
sprintf(temp,"t%d",temp_reg_num++);
$$.text = copy_string(temp);
}
| t '/' f {
printf("t -> t / f\n");
char temp[5];
print_three_address_code($1.text,'/',$3.text);
sprintf(temp,"t%d",temp_reg_num++);
$$.text = copy_string(temp);
}
| f {
printf("t -> f\n");
$$.text = copy_string($1.text);
}
;
f : f '^' x {
printf("f -> f ^ x\n");
char temp[5];
print_three_address_code($1.text,'^',$3.text);
sprintf(temp,"t%d",temp_reg_num++);
$$.text = copy_string(temp);
}
| x {
printf("f -> x\n");
$$.text = copy_string($1.text);
printf("Why syntax error??");
}
;
x : '(' s ')' {}
| NUM {
printf("x -> NUM\n");
$$.text = copy_string($1.text);
}
| IDEN {
printf("x -> IDEN\n");
$$.text = copy_string($$.text,$1.text);
}
| FLOAT {
printf("x -> FLOAT\n");
$$.text = copy_string($1.text);
}
| '-' x {
printf("x -> - x\n");
$$.text = (char*)malloc(sizeof(char)*(strlen($2.text)+2));
$$.text[0] = '-';
strcat($$.text,$2.text);
}
;
%%
main()
{
printf("Enter your expression : ");
yyparse();
}
yyerror()
{
printf("Syntax Error\n");
}
yywrap()
{
return 1;
}
icg.l
%{
#include<stdio.h>
#include<stdlib.h>
#include"y.tab.h"
char* copy_string(char* fromstring)
{
char* tostring = (char*)malloc(sizeof(char)*(strlen(fromstring)+1));
strcpy(tostring,fromstring);
return tostring;
}
%}
iden [a-zA-Z_][a-zA-Z_]*
%%
[0-9]+ {
yylval.text = copy_string(yytext);
return NUM;
}
[0-9]+[.][0-9]+ {
yylval.text = copy_string(yytext);
return FLOAT;
}
{iden} {
yylval.text = copy_string(yytext);
return IDEN;
}
[+] {
return '+';
}
[-] {
return '-';
}
[*] {
return '*';
}
[/] {
return '/';
}
['^'] {
return '^';
}
[=] {
return '=';
}
'\n' {
return '\n';
}
. {}
%%