A program to create a Terminal Table on the basis of the subsets of tokens
{ "if","for","while","int","float","char","+","-","*","/","=","<",">","{",",","(",")",";","?"} . Given a C statement the system program should create and display a uniform table that consists of full list of valid tokens. There must be two entries for each token in the Symbol Table, namely class and index (that is a pointer to a specific location of an appropriate table where the description of the token can be found. The index can be found to any one of the following tables: Identifier table, Literal Table and Terminal Table depending on the type of token. Each of these three table would have its own relevant fields.
#include<string.h> #include<stdlib.h>
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
int tokencount(char *str);
void classification(char iden[],int *key,int *sym,int *ope,int *lit,int *ide,int *pkey,int *psym,int *pope);
void print_table(int,int,int);
#define TRUE 1
#define FALSE 0
typedef struct
{
int indx;
char tok[20];
int pos;
}Terminal_table;
typedef struct
{
int indx;
char tok[20];
char t_nme[20];
int pos;
}Uniform_sym_table;
typedef struct
{
int indx;
char lit[20];
int pos;
}Literal;
typedef struct
{
int indx;
char iden[20];
int pos;
}Identifier;
Terminal_table *TT;
Uniform_sym_table *UST;
Literal *LT;
Identifier *IDE;
int main(void)
{
char *keyword1[]={ "if","for","while","int","float","char","+","-","*","/","=","<",">",",","(",")",";","?"};
char *str,ar[80],dig[2],sp[20];
char *token;
int FLAG=FALSE,count=0,n,len,i=0,tc=0,p=0,q=0,r=0,s=0,t=0,j=0;
int key=FALSE,sym=FALSE,ope=FALSE,lit=FALSE,ide=FALSE,lit_count=0,FLAG1=FALSE,iden_count=0;
int pkey,psym,pope;
printf("\n Enter the maximum length of the statement:");
scanf(" %d", &n);
str=(char *)malloc( (n+1)*sizeof(char));
fflush(stdin);
printf("\n Enter the C statement:");
scanf("%[^\n]",str);
len=strlen(str);
if(len>n)
{
printf("\n Length of the statement is greater than the input length:");
exit(1);
}
token=str;
tc=tokencount(str);
UST=(Uniform_sym_table *)malloc(tc*sizeof(Uniform_sym_table));
TT=(Terminal_table *)malloc(20*sizeof(Terminal_table));
LT=(Literal *)malloc(tc*sizeof(Literal));
IDE=(Identifier *)malloc(tc*sizeof(Identifier));
for(i=0;i<18;i++)
{
strcpy(TT[i].tok,keyword1[i]);
TT[i].indx=i+1;
TT[i].pos=i+1;
}
tc=0;
while(1)
{
while(*token!= ' ')
{
if(*token== '\0')
{
FLAG=TRUE;
break;
}
ar[count++]=*token;
token++;
}
ar[count]='\0';
classification(ar,&key,&sym,&ope,&lit,&ide,&pkey,&psym,&pope);
if(key)
{
UST[tc].indx=tc+1;
strcpy(UST[tc].tok,ar);
strcpy(UST[tc].t_nme,"TRM");
for(i=0;i<19;i++)
{
if((strcmp(TT[i].tok,ar))==0)
break;
}
UST[tc].pos=TT[i].pos;
tc++;
}
if(sym)
{
UST[tc].indx=tc+1;
strcpy(UST[tc].tok,ar);
strcpy(UST[tc].t_nme,"TRM");
for(i=0;i<19;i++)
{
if((strcmp(TT[i].tok,ar))==0)
break;
}
UST[tc].pos=TT[i].pos;
tc++;
}
if(ope)
{
UST[tc].indx=tc+1;
strcpy(UST[tc].tok,ar);
strcpy(UST[tc].t_nme,"TRM");
for(i=0;i<19;i++)
{
if((strcmp(TT[i].tok,ar))==0)
break;
}
UST[tc].pos=TT[i].pos;
tc++;
}
if(lit)
{
UST[tc].indx=tc+1;
for(i=0;i<p;i++)
{
if((strcmp(LT[i].lit,ar))==0)
{
FLAG1=TRUE;
s=i;
break;
}
}
if(FLAG1==0)
{
strcpy(UST[tc].tok,ar);
strcpy(UST[tc].t_nme,"LTR");
LT[p].indx=p+1;
strcpy(LT[p].lit,ar);
LT[p].pos=p+1;
UST[tc].pos=LT[p].pos;
p++;
lit_count++;
}
else
{
strcpy(UST[tc].tok,ar);
strcpy(UST[tc].t_nme,"LTR");
UST[tc].pos=LT[s].pos;
}
tc++;
}
if(ide)
{
UST[tc].indx=tc+1;
for(i=0;i<q;i++)
{
if((strcmp(IDE[i].iden,ar))==0)
{
FLAG1=TRUE;
r=i;
break;
}
}
if(FLAG1==0)
{
strcpy(UST[tc].tok,ar);
strcpy(UST[tc].t_nme,"IDE");
IDE[q].indx=q+1;
strcpy(IDE[q].iden,ar);
IDE[q].pos=q+1;
UST[tc].pos=IDE[q].pos;
q++;
iden_count++;
}
else
{
strcpy(UST[tc].tok,ar);
strcpy(UST[tc].t_nme,"IDE");
UST[tc].pos=IDE[r].pos;
}
tc++;
}
/* if(!key && !sym && !ope && !lit && !ide)
{
printf("\n Invalid tokens: %s ", ar);
}*/
if(FLAG==TRUE)
break;
while(*token==' ')
{
token++;
if(*token=='\0')
{
FLAG=TRUE;
break;
}
}
if(FLAG==TRUE)
break;
count=0;
FLAG1=FALSE;
key=FALSE;
sym=FALSE;
ope=FALSE;
lit=FALSE;
ide=FALSE;
}
print_table(tc,lit_count,iden_count);
printf("\n Enter a key");
getche();
return 0;
}
void print_table(int tc, int lit_count, int iden_count)
{
int i;
printf("\n");
printf("\n\t Uniform Symbol Table");
printf("\n---------------------------------------------------------");
printf("\n #Index\t\tTokens\t\tClass\t\tPosition");
printf("\n---------------------------------------------------------");
for(i=0;i<tc;i++)
{
printf("\n%d\t\t%s\t\t%s\t\t%d",UST[i].indx,UST[i].tok,UST[i].t_nme,UST[i].pos);
}
printf("\n\tTerminal Table\n");
printf("---------------------------------");
printf("\nIndex\tKeywords\tPosition\n");
printf("---------------------------------");
for(i=0;i<18;i++)
{
printf("\n#%d\t%s\t %d",TT[i].indx,TT[i].tok,TT[i].pos);
}
printf("\n\n\n");
printf("\n\t Literal Table");
printf("\n---------------------------------------------------------");
printf("\n #Index\t\tTokens\t\tPosition");
printf("\n---------------------------------------------------------");
for(i=0;i<lit_count;i++)
{
printf("\n%d\t\t%s\t\t%d",LT[i].indx,LT[i].lit,LT[i].pos);
}
printf("\n\n\n");
printf("\n\t Identifier Table");
printf("\n---------------------------------------------------------");
printf("\n #Index\t\tTokens\t\tPosition");
printf("\n---------------------------------------------------------");
for(i=0;i<iden_count;i++)
{
printf("\n%d\t\t%s\t\t%d",IDE[i].indx,IDE[i].iden,IDE[i].pos);
}
return;
}
int tokencount(char *str)
{
char *token,ar[30];
int tc=0,count=0,FLAG=FALSE;
token=str;
while(1)
{
while(*token!= ' ')
{
if(*token== '\0')
{
FLAG=TRUE;
break;
}
ar[count++]=*token;
token++;
}
ar[count]='\0';
tc++;
if(FLAG==TRUE)
break;
while(*token== ' ')
{
token++;
if(*token== '\0')
{
FLAG=TRUE;
break;
}
}
if(FLAG==TRUE)
break;
count=0;
}
return (tc);
}
void classification( char ar[],int *key,int *sym,int *ope,int *lit,int *ide,int *pkey,int *psym,int *pope)
{
int FLAG=FALSE,len,count=0,flag=TRUE,numb=0,d,t=0,m=0;
char *keyword[]={ "if","for","while","int","float","char"};
char *operatr[]={ "+","-","*","/","=","<",">"};
char *symbols[]={ ",","(",")",";","?"};
int i=0;
len=strlen(ar);
for(i=0;i<len;i++)
if(ar[i]==' ')
t++;
if(t==len)
return;
if(len>8)
FLAG=FALSE;
for(i=0;i<=5;i++)
{
if(strcmp(keyword[i],ar)==0)
{
// printf("\n%s\tis a Keyword:",ar);
FLAG=TRUE;
flag=FALSE;
*key=TRUE;
*pkey=i;
}
}
i=0;
for(i=0;i<=6;i++)
{
if(strcmp(operatr[i],ar)==0)
{
// printf("\n%s\tis a Operator:",ar);
FLAG=TRUE;
flag=FALSE;
*ope=TRUE;
*pope=i;
}
}
i=0;
for(i=0;i<5;i++)
{
if(strcmp(symbols[i],ar)==0)
{
// printf("\n%s\tis a Symbol:",ar);
FLAG=TRUE;
flag=FALSE;
*sym=TRUE;
*psym=i;
}
}
if ( ((65 <= ar[0] && ar[0] <= 93 )|| ( 97 <= ar[0] && ar[0] <= 122)) && len <= 8 && FLAG==FALSE)
{
// printf("\n%s\tis an Identifier:",ar);
for(i=1;i<strlen(ar);i++)
{
if( (65 <= ar[i] && ar[i] <= 93 ) || ( 97 <= ar[i] && ar[i] <= 122) || ( 48 <= ar[i] && ar[i] <= 57) || ar[i]==95 )
{
m++;
}
}
if(m==strlen(ar)-1)
{
// printf("\n%s\tis an Identifier:\n",ar);
FLAG=TRUE;
flag=FALSE;
*ide=TRUE;
}
// FLAG=TRUE;
// flag=FALSE;
// *ide=TRUE;
}
for(i=0;i<len;i++)
{
if(48 <= ar[i] && ar[i] <=57 )
count++;
else
continue;
}
if(count==len && flag==TRUE && FLAG==FALSE)
{
numb=atoi(ar);
if(numb==0 || numb >999)
{printf("\n Invalid token: %s ", ar);}//printf("%s\tis not a valid Constant:",ar);
else
{*lit=TRUE;}//printf("\n%s\tis a Constant:",ar);
FLAG=TRUE;
}
i=0;
if( FLAG==FALSE)
{
if(ar[0]=='-' || (48 <= ar[0] && ar[0] <=57))
{printf("\n Invalid token: %s ", ar);}//printf("%s\tis not a valid Constant ",ar);
else
{printf("\n Invalid token: %s ", ar);}//printf("\n%s\tis an INVALID TOKEN:",ar);
}
//printf("\n");
return;
}
No comments:
Post a Comment