grammar to recognize declarations of variables, assignment statement & “while” statement as per syntax of C and generate equivalent three address code

, by Prashant Gunjal

##### pr.h ######
struct holder
{
char name[8];
char type[6];
}st[30];

####### pr_7.l #######
%{
#include<stdio.h>
#include "y.tab.h"
#include "pr.h"
char var_type[5];
int var_cnt=0,i;
extern yylval;
%}
type int|float|char
%%
main {return(mn);}
{type} {
strcpy(var_type,yytext);
return(Type);
}
while {return(WHILE);}
[a-z]+[a-z0-9]* {
for(i=0;i<var_cnt;i++)
if(strcmp(st[i].name,yytext)==0)
break;
if(i==var_cnt)
{
strcpy(st[var_cnt].name,yytext);
strcpy(st[var_cnt++].type,var_type);
}
yylval=i;
return(var);
}
[0-9]+ {yylval=atoi(yytext);return(num);}
[ \t] {;}
. {return(yytext[0]);}
[\n] {;}
%%

######## pr_7.y ##########

/*
6) Write an attributed translation grammar to recognize declarations of variables, assignment statement & “while” statement as per syntax of C and generate equivalent  three address code for the given input made up of constructs mentioned above using LEX and YACC. Write a code to store the identifiers from the input in a symbol table and also to record other relevant information about the identifiers. Display all records stored in the symbol table.
*/

%{
#include<stdio.h>
#include<stdlib.h>
#include "pr.h"
FILE * yyin;
int total_var=0,temp_cnt=0;
char e2_type[6],e1_type[6],e_type[6];
%}
%token mn var Type num WHILE

%%
edash : prog {printf("\n\t!!!End of programm!!!!\n");}
;
prog : mn '('')''{'mstmt'}' {;}
;
mstmt : stmt mstmt
|
stmt
;
stmt : dstmt';' {/*printf("\n\tValid Declration Statement");*/}
|
exstmt';' {//printf("\n\tValid Expression Statement");
}
|
dstmt {printf("\n\t**** Error--- Line Not Terminated by ';' ");}
|
exstmt {printf("\n\t**** Error--- Line Not Terminated by ';'");}
|
whileloop
|
next';'
;


dstmt : Type varlist
;
varlist : v','varlist
|
v
;
v : var {total_var++;}
|
var'='num { total_var++;
//Target code
printf("\n\t%s:=%d",st[$1].name,$3);
}
;


exstmt : var'='e {
if(strcmp(e1_type,"int")==0)
printf("\n\t%s := %d",st[$1].name,$3);
else
printf("\n\t%s:=T%d",st[$1].name,temp_cnt-1);
}
;
e : e '+' e1 {
//target Code
printf("\n\tT%d:=%s+%s",temp_cnt,st[$1].name,st[$3].name);
temp_cnt++;
}
|
e '-' e1 {
//target Code
printf("\n\tT%d:=%s-%s",temp_cnt,st[$1].name,st[$3].name);
temp_cnt++;
}
|
e1 { strcpy(e_type,e1_type);}
;
e1 : e1 '*' e2 {
//target Code
printf("\n\tT%d:=%s*%s",temp_cnt++,st[$1].name,st[$3].name);
}
|
e1 '/' e2 {
//target Code
printf("\n\tT%d:=%s/%s",temp_cnt++,st[$1].name,st[$3].name);
}
|
e2 {strcpy(e1_type,e2_type);}
;
e2 : var {strcpy(e2_type,st[$1].type);}
|
num { strcpy(e2_type,"int");}
;


next : var'+''+' {printf("\n\t%s=%s+1 ",st[$1].name,st[$1].name);}
|
var '-''-' {printf("\n\t%s=%s-1",st[$1].name,st[$1].name);}
;
whileloop : WHILE '(' cnd ')' '{' body '}' {printf("\nEnd : ");}
;
cnd : var '<' var {printf("\nStart:\n\tIf %s < %s goto next else goto End\nNext:",st[$1].name,st[$3].name);}
|
var '<' num {printf("\nStart:\n\tIf %s < %d goto next else goto End\nNext:",st[$1].name,$3);}
|
var '>' var {printf("\nStart:\n\tIf %s > %s goto next else goto End\nNext:",st[$1].name,st[$3].name);}
|
var '>' num {printf("\nStart:\n\tIf %s > %d goto next else goto End\nNext:",st[$1].name,$3);}
|
var '<''=' var {printf("\nStart:\n\tIf %s <= %s goto next else goto End\nNext:",st[$1].name,st[$4].name);}
|
var '<''=' num {printf("\nStart:\n\tIf %s <= %d goto next else goto End\nNext:",st[$1].name,$4);}
|
var '>''=' var {printf("\nStart:\n\tIf %s >= %s goto next else goto End\nNext:",st[$1].name,st[$4].name);}
|
var '>''=' num {printf("\nStart:\n\tIf %s >= %d goto next else goto End\nNext:",st[$1].name,$4);}
;
body : mstmt   {printf("\n\tgoto Start");}
;
%%

int main()
{
int i;
yyin=fopen("ip.c","r");
printf("\n\t***** SEE 3 ADDR code **** ");
yyparse();
printf("\n\t++++Now you want symbal table right??? Then SEE...++++");
for(i=0;i<total_var;i++)
{
printf("\n\t%s\t%s",st[i].name,st[i].type);
}
return(0);
}

void yyerror(char *c)
{
fprintf(stderr,"%s\n",c);
}

####### ip.c ########
main ( ) 
float a=0,b=4,c=5;
int no1 = 0,no2=0;
a=b+c;
no1=no2-a;
while(a<10)
{
c=b+c;
a++;
}
}


0 comments: