Implementation of Semantic analysis (Type checking)


Contents of type1.l file 



%{
#include<stdio.h>
#include<string.h>
#include"y.tab.h"

%}
%%
[a-zA-Z]+[a-zA-Z0-9]*"(" {strcpy(yylval.DataType,yytext);return function;}
int|float|char {strcpy(yylval.DataType,yytext); return Type;}
[a-zA-Z]+[a-zA-Z0-9]*"," {strcpy(yylval.DataType,yytext);return parameter;}
[a-zA-Z]+[a-zA-Z0-9]*"){}" {strcpy(yylval.DataType,yytext);return functionbody; }
");" {strcpy(yylval.DataType,yytext);return functioncall;}
[a-zA-Z]+[a-zA-Z0-9]* {strcpy(yylval.ID,yytext); return Name;}
[0-9]+ {strcpy(yylval.DataType,"int"); return Type;}
[0-9]+.[0-9]+ {strcpy(yylval.DataType,"float"); return Type;}
"'"[a-zA-Z]+"'" {strcpy(yylval.DataType,"char"); return Type;}
";" return SC;
"=" return EQ;
"," return C;
"\n" {}

%%




Contents of type1.y file 






%{
#include<stdio.h>
#include"y.tab.h"
%}
%union
{
char DataType[10];
char ID[10];
}
%token <ID> Name;
%token SC EQ
%token <DataType> Type;
%token function parameter functioncall C functionbody
%start program
%%
program:s|j
j:Type function Type parameter Type functionbody function Type C Type functioncall{if(strcmp($3,$8)==0 && strcmp($5,$10)==0){printf("Correctly Assigned for DataType111");}else{printf("Incorrectly Assigned for DataType");}}
s:Type Name EQ Type SC{if(strcmp($1,$4)==0){printf("Correctly Assigned for DataType111:%s\t ID:%s",$1,$2);}else{printf("Incorrectly Assigned for DataType:%s\t ID:%s",$1,$2);}}
%%
void yyerror(const char *str){}
int yywrap(){return 1;}
main(){yyparse();}

Popular posts from this blog

DDL DML DCL and TCL

Implementation of Calculator using lex and yacc

A Register Allocation algorithm that translates the given code into one with a fixed number of registers.