attributed translation grammar to recognize declarations of variables,assignment statement with type checking 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_5.l #####
%{
#include<stdio.h>
#include "y.tab.h"
#include "pr.h"
char var_type[5];
int var_cnt=0,i;
extern int yylval;
%}
type int|float|char
%%
"main" {return(mn);}
{type} {
strcpy(var_type,yytext);
return(Type);
}
[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_5.y #####

/*
4) Write an attributed translation grammar to recognize declarations of variables,      
   assignment statement with type checking 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.
   Input:  C code snippet
   Output:  a] equivalent three address code
   b] Symbol table with type information
   c] Error handling for type checking
*/
%{
#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

%%
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 ';'");}
;
dstmt : Type varlist
;
varlist : v','varlist
|
v
;
v : var {total_var++;}
|
var'='num { total_var++;
//Target code
st[$1].val=$3;
printf("\n\t%S:=%d",st[$1].name,$3);
}
;
exstmt : var'='e {
// This part check types in expression
if(strcmp(st[$$].type,e_type)!=0)
if(strcmp(st[$$].type,"int")==0 && strcmp(e_type,"float")==0)
printf("\n\tPlease check type you may need typecasting in expression");


printf("\n\t%s:=T%d",st[$1].name,temp_cnt-1);
}
;
e : e '+' e1 {
typechecker1();
//target Code
printf("\n\tT%d:=%s+%s",temp_cnt,st[$1].name,st[$3].name);
temp_cnt++;
}
|
e '-' e1 {
typechecker1();
//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 {
typechecker2();
//target Code
printf("\n\tT%d:=%s*%s",temp_cnt++,st[$1].name,st[$3].name);
}
|
e1 '/' e2 {
typechecker2();
//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");}
;
%%

int main()
{
int i;
yyin=fopen("ip.c","r");
printf("\n\t***** SEE 3 ADDR code **** ");
yyparse();
printf("\n\t++++ symbal table ...++++");
for(i=0;i<total_var;i++)
{
printf("\n\t%s\t%s\t %d",st[i].name,st[i].type,st[i].val);
}
return(0);
}
void typechecker1()
{
// This part check types in expression
if(strcmp(e1_type,e_type)!=0)
{
if((strcmp(e1_type,"int")==0 && strcmp(e_type,"float")==0)||(strcmp(e1_type,"float")==0 && strcmp(e_type,"int")==0))
strcpy(e_type,"float");
else
printf("\n\tPlease check type you may need typecasting in expression");
}
}
void typechecker2()
{
// This part check types in expression
if(strcmp(e1_type,e2_type)!=0)
{
if((strcmp(e1_type,"int")==0 && strcmp(e2_type,"float")==0)||(strcmp(e1_type,"float")==0 && strcmp(e2_type,"int")==0))
strcpy(e1_type,"float");
else
printf("\n\tPlease check type you may need typecasting in expression");
}
}
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;
a=b*c;
no1=no2-a;
}

2 comments:

Utkarsh said...

Dude u are doing a great work.. Keep it up.. This helps me a lot in pracs..;) Especially Compiler Labs..:)

Anonymous said...

From where can i get code for "pr.h" header file .....?