0% found this document useful (0 votes)
11 views4 pages

Exp 5

Uploaded by

Sathish Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

Exp 5

Uploaded by

Sathish Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include<stdio.

h>
#include<string.h>
#include<conio.h>
int count=1,i=0,j=0,l=0,findval=0,k=0,kflag=0;
char key[4][12]= {"int","float","char","double"};
char dstr[100][100],estr[100][100];
char token[100],resultvardt[100],arg1dt[100],arg2dt[100];
void entry();
int check(char[]);
int search(char[]);
void typecheck();
struct table { char var[10]; char dt[10]; };
struct table tbl[20];

void main()
{
clrscr();
printf("\n IMPLEMENTATION OF TYPE CHECKING \n");

printf("\n DECLARATION \n\n");


do
{
printf("\t");
gets(dstr[i] );
i++;
}
while(strcmp(dstr[i1],"END"));
printf("\n EXPRESSION \n\n");
do
{ printf("\t");
gets(estr[l] );
l++;
}
while(strcmp(estr[l-1],"END"));

i=0;
printf("\n SEMANTIC ANALYZER(TYPE CHECKING): \n");
while(strcmp(dstr[i],"END"))
{} l=0;

entry();
printf("\n"); i++; while(strcmp(estr[l],"END"))
{
typecheck( );
printf("\n") ;

l++;
}

printf("\n PRESS ENTER TO EXIT FROM TYPE CHECKING\n");


getch();
}
void entry()
{
j=0; k=0 ;
memset(token,0,sizeof(token)) ;
while(dstr[i][j]!=' ')
{
token[k]=dstr[i][j ];
k++;
j++;
}
kflag=check(token );
if(kflag==1)
{
strcpy(tbl[count].dt,toke n);
k=0;
memset(token,0,strlen(token) );
j++;
while(dstr[i][j]!=';')
{
token[k]=dstr[i][j ];
k++;
j++;
}
findval=search(toke n);
if(findval==0)
{

} else
{

}
strcpy(tbl[count].var,token);

printf("The variable %s is already declared",token);

}
else
{

}
kflag=0;
count++;

printf("Enter valid datatype\n");

void typecheck()
{
memset(token,0,strlen(token));
j=0;
k=0;
while(estr[l ][j]!='=')
{
token[k]=estr[l][j ];
k++;
j++;
}
findval=search(toke n);
if(findval>0)
{

} else
{

}
k=0 ;
strcpy(resultvardt,tbl[findval].dt);
findval=0;

printf("Undefined Variable\n");
memset(token,0,strlen(token));
j++;
while(((estr[l][j]!='+')&&(estr[l][j]!='-')&&(estr[l][j]!='*')&&(estr[l][j]!='/')))
{
token[k]=estr[l][j ];
k++;
j++;
}
findval=search(toke n);
if(findval>0)
{

} else
{

}
k=0 ;
strcpy(arg1dt,tbl[findval].dt);
findval=0;

printf("Undefined Variable\n");
memset(token,0,strlen(token));
j++;
while(estr[l][j]!=';')
{
token[k]=estr[l][j ];
k++;
j++;
}
findval=search(toke n);
if(findval>0)
{

} else
{

}
strcpy(arg2dt,tbl[findval].dt);
findval=0;
printf("Undefined Variable\n");
if(!strcmp(arg1dt,arg2dt))

{
if(!strcmp(resultvardt,arg1dt))
{

} else
{
} else
{

}
printf("\tThere is no type mismatch in the expression %s ",estr[l]);

printf("\tLvalue and Rvalue should be same\n");


printf("\tType Mismatch\n");
}
}

int search(char variable[])


{
int i;
for(i=1;i<=count;i+ +)
{
if(strcmp(tbl[i].var,variable) == 0)
{
return i;
}}
return 0;
}

int check(char t[])


{
int in;
for(in=0;in<4;in+ +)
{
if(strcmp(key[in],t)==0)
{ return 1;
}}
return 0;
}

You might also like