The Programming Project: tokens
Showing posts with label tokens. Show all posts
Showing posts with label tokens. Show all posts

Sunday, September 8, 2013

Token Identification using Transition diagram

The following program uses transition diagram to identify the pre-defined tokens of C language. For valid token is will display the token type as well as state-table(traversal) and the states at which there is a failure. For invalid tokens it will simply print the states at which it has failed in different transition diagrams: Given below is a part of the transition diagram for pre-defined symbols: The states 34,36,38,40,42 and 44 are final states and will return "{",",",...",?" respectively

Here is the file of complete Transition diagram http://sdrv.ms/1at8wQ3



#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<malloc.h>
#define TRUE 1
#define FALSE 0
int main(void)
{
char *str,ar[80];
char *start,*current,*next,*super;
char *ftest[45];
int *fail,iden_final[11],ope_final[7],sym_final[7];
int  count=0,n,len,i=0,key=TRUE,ide_len=0,state=0,choice;
int ide=TRUE,ide_t=TRUE,lit=TRUE,lit_t=TRUE,ope=TRUE,symb=TRUE,numb,negative=FALSE,j=0;
fail=(int *)malloc(44*sizeof(int));
for(i=0;i<45;i++)
{
fail[i]=FALSE;
ftest[i]=(char*)malloc(60*sizeof(char));
}
for(i=0;i<11;i++)
iden_final[i]=FALSE;
for(i=0;i<7;i++)
ope_final[i]=sym_final[i]=FALSE;
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);
}
super=str;
while(*super==' ')
super++;
while(*super !='\0' || *super =='\n')
{
start=super;
current=start;
next=current;
//Transistion diagram for Keywords int,if,for
switch(*current)
{
case 'i':
  //printf("\n>>>>>>>>%c",*current);
ar[0]=*current;
next +=1;
if(*next == ' ' || *next == '\0')
{
//printf("\n Failed at state 1:");
strcpy(ftest[1],"Failed at state 1:");
fail[1]=TRUE;
//ftest[strlen("Failed at state 1:")]='\0';
key=FALSE;
break;
}
current=next;
if(*current!='n' && *current !='f')
{
//printf("\n Failed at state 1:");
strcpy(ftest[1],"Failed at state 1:");
fail[1]=TRUE;
//ftest[strlen("Failed at state 1:")]='\0';
key=FALSE;
break;
}
ar[1]=*current;
 // printf("\n>>>>>n or f >>>%c",*current);
next +=1;
if(*next == ' ' || *next == '\0' || *current == 'f')
{
//   printf("\n In test loop:");
if(ar[1]!='f')
{
//printf("\n Failed at state 5:");
strcpy(ftest[5],"Failed at state 5:");
fail[5]=TRUE;
//ftest[strlen("Failed at state 5:")]='\0';
key=FALSE;
}
// printf("\n>>>>>>>>>>> [%c]",*next);
if(*next != ' ' && *next!='\0')
{
//printf("\n Failed at state 5:");
key=FALSE;
strcpy(ftest[5],"Failed at state 5:");
fail[5]=TRUE;
 // ftest[strlen("Failed at state 5:")]='\0';
break;
}
ar[2]='\0';
current=next;
if(key==TRUE)
iden_final[6]=TRUE;
printf("%s\t\t Keyword",ar);
break;
}
current=next;
if(*current!='t')
{
//printf("\n Failed at state 2:");
strcpy(ftest[2],"Failed at state 2:");
fail[2]=TRUE;
//ftest[strlen("Failed at state 2:")]='\0';
key=FALSE;
break;
}
ar[2]=*current;
//printf("\n>>>>>>>>%c",*current);
next +=1;
if(*next !=' ' && *next !='\0')
{
//printf("\n Failed at state 3:");
key=FALSE;
strcpy(ftest[3],"Failed at state 3:");
fail[3]=TRUE;
//ftest[strlen("Failed at state 3:")]='\0';
break;
}
current=next;
ar[3]='\0';
//printf("\nFFFFFFFF%c",*current);
iden_final[4]=TRUE;
printf("%s\t\t Keyword",ar);
break;
// CASE for
case 'f':
ar[0]=*current;
next +=1;
if(*next == ' ' || *next == '\0')
{
//printf("\n Failed at state 7:");
strcpy(ftest[7],"Failed at state 7:");
fail[7]=TRUE;
//ftest[strlen("Failed at state 7:")]='\0';
key=FALSE;
break;
}
current=next;
if(*current!='o')
{
//printf("\n Failed at state 7:");
strcpy(ftest[7],"Failed at state 7:");
fail[7]=TRUE;
//ftest[strlen("Failed at state 7:")]='\0';
key=FALSE;
break;
}
ar[1]=*current;
next +=1;
if(*next ==' ' && *next =='\0')
{
//printf("\n Failed at state 8:");
strcpy(ftest[8],"Failed at state 8:");
fail[8]=TRUE;
//ftest[strlen("Failed at state 8:")]='\0';
key=FALSE;
break;
}
current=next;
if(*current!='r')
{
//printf("\n Failed at state 8:");
strcpy(ftest[8],"Failed at state 8:");
fail[8]=TRUE;
//ftest[strlen("Failed at state 8:")]='\0';
key=FALSE;
break;
}
ar[2]=*current;
next +=1;
if(*next !=' ' && *next !='\0')
{
//printf("\n Failed at state 9:");
strcpy(ftest[9],"Failed at state 9:");
fail[9]=TRUE;
//ftest[strlen("Failed at state 9:")]='\0';
key=FALSE;
break;
}
current=next;
ar[3]='\0';
//printf("\nFFFFFFFF%c",*current);
iden_final[10]=TRUE;
printf("%s\t\t Keyword",ar);
break;
default:
//printf("\n Failure at state 0 in Keyword transistion:");
strcpy(ftest[0],"Failed at state 0:");
fail[0]=TRUE;
//ftest[strlen("Failed at state 0:")]='\0';
key=FALSE;
break;
}
// State traversal diagram
if(iden_final[6]==TRUE)
{
printf("\n i\t\t 0(s)---->1");
printf("\n f\t\t 1------->2");
printf("\n *D\t\t 5---->6(f)");
printf("\n");
}
if(iden_final[4]==TRUE)
{
printf("\n i\t\t 0(s)---->1");
printf("\n n\t\t 1------->2");
printf("\n t\t\t 2------->3");
printf("\n *D\t\t 3---->4(f)");
printf("\n");
}
 if(iden_final[10]==TRUE)
{
printf("\n f\t\t 0(s)---->7");
printf("\n 0\t\t 7------->8");
printf("\n r\t\t 8------->9");
printf("\n *D\t\t 9--->10(f)");
printf("\n");
}

// Transistion diagram for identifier
ide_len=0;
i=0;
if(key==FALSE)
{
state=0;
current=start;
while(*current!= ' ' && *current !='\0')
{
ar[i++]=*current;
current++;
}
ar[i]='\0';
current=start;
if( !(65 <= *current && *current <= 93 ) && !(97 <= *current && *current <= 122 )  )
{
//printf("\n Failed at state 11 in identifier transistion:");
strcpy(ftest[11],"Failed at state 11:");
fail[11]=TRUE;
//ftest[strlen("Failed at state 11:")]='\0';
ide_t=FALSE;
ide=FALSE;
}
current +=1;
next=current;
ide_len++;
while(*current!= ' ' && *current !='\0' && ide_t==TRUE)
{
if( (65 <= *current && *current <= 93 ) ||  ( 97 <= *current && *current <= 122)  || ( 48 <= *current && *current <= 57) || *current==95 )
{
ide_len++;
state++;
}
else
{
//printf("\n Failed at state 12:");
strcpy(ftest[1],"Failed at state 12:");
fail[12]=TRUE;
//ftest[strlen("Failed at state 12:")]='\0';
ide_t=FALSE;
ide=FALSE;
break;
}
current++;
}
if(ide_len>8 || ide_t==FALSE )
{ide=FALSE;}//printf("\n %s is Invalid identifier:",ar);
else
{
ide=TRUE;
printf("\n %s\t\t Identifier:",ar);
printf("\n %c\t\t11(s)------->12",ar[0]);
for(i=1;i<=state;i++)
printf("\n %c\t\t12------->12",ar[i]);
printf("\n *D\t\t12------->13");
printf("\n");
}

} // end of trans for iden
// Start of transisition for constant
i=0;
j=0;
state=0;
if(key==FALSE && ide==FALSE)
{
current=start;
while(*current!= ' ' && *current !='\0')
{
ar[i++]=*current;
current++;
}
ar[i]='\0';
current=start;
next=start;
if( !(48 <= *current && *current <= 57 ) )
{
//printf("\n Failed at state 14 in Literal transistion:");
strcpy(ftest[14],"Failed at state 14:");
fail[14]=TRUE;
//ftest[strlen("Failed at state 14:")]='\0';
//addddddd
lit_t=FALSE;
}
if(*current=='-')
negative=TRUE;
current +=1;
next=current;
ide_len++;
while(*current!= ' ' && *current !='\0' && lit_t==TRUE)
{
if((48 <= *current && *current <= 57 ))
{
state++;
}
else
{
//printf("\n Failed at state 15:");
strcpy(ftest[15],"Failed at state 15:");
fail[15]=TRUE;
//ftest[strlen("Failed at state 15:")]='\0';
if(*current!='.')
{
j++;
lit_t=FALSE;
}
else
lit_t=FALSE;
//break;
}
current++;
}
if(lit_t==FALSE )
{
if(negative==TRUE && lit_t==TRUE)
{
printf("\n %s\t\t Out of range:",ar);
lit=TRUE;
}
else
lit=FALSE;
}//printf("\n %s is Invalid identifier:",ar);
else
{
numb=atoi(ar);
if(numb <=0 || numb >999)
{
printf("\n %s\t\t Out of range:",ar);
lit=TRUE;
}
else {
lit=TRUE;
printf("\n %s\t\t Constant:",ar);
printf("\n %c\t\t14(s)------->15",ar[0]);
for(i=1;i<=state;i++)
printf("\n %c\t\t15------->15",ar[i]);
printf("\n *D\t\t15------->16");
printf("\n");

}
}
} // end of if for literal trans
//start of transistion of operators
i=0;
if(key==FALSE && lit==FALSE && ide==FALSE)// && negative == FALSE)
{
//printf("\n in OPERATORSSSSSSSSSSSSSSSSSS");
strcpy(ar,"");
current=start;
next=current;
ar[0]=*current;
//    printf("\n>>>>>>>>> %c",*current);
switch(*current)
{
case '+':
next +=1;
if(*next ==' ' || *next=='\0')
{
ar[1]='\0';
ope=TRUE;
current=next;
ope_final[0]=TRUE;
}
else
{
//printf("\n Failed at state 18:");
strcpy(ftest[18],"Failed at state 18:");
fail[18]=TRUE;
//ftest[strlen("Failed at state 18:")]='\0';
ope=FALSE;
}
break;
case '-':
next +=1;
if(*next ==' ' || *next=='\0')
{
ar[1]='\0';
ope=TRUE;
current=next;
ope_final[1]=TRUE;
}
else
{
//printf("\n Failed at state 20:");
if((48<= *next && *next <= 57))
{
//printf("\n Failed at state 20:  %c",*next);
strcpy(ftest[20],"Failed at state 20:");
fail[20]=FALSE;
//ftest[strlen("Failed at state 20:")]='\0';
ope=FALSE;
}
else
{
          printf("\n Failed at state 20:  %c",*next);
strcpy(ftest[20],"Failed at state 20:");
fail[20]=TRUE;
//ftest[strlen("Failed at state 20:")]='\0';
ope=FALSE;
}
}
break;
case '*':
next +=1;
if(*next ==' ' || *next=='\0')
{
ar[1]='\0';
ope=TRUE;
current=next;
ope_final[2]=TRUE;
}
else
{
//printf("\n Failed at state 22:");
strcpy(ftest[22],"Failed at state 22:");
fail[22]=TRUE;
//ftest[strlen("Failed at state 22:")]='\0';
ope=FALSE;
}
break;
case '/':
next +=1;
if(*next ==' ' || *next=='\0')
{
ar[1]='\0';
ope=TRUE;
current=next;
ope_final[3]=TRUE;
}
else
{
//printf("\n Failed at state 24:");
strcpy(ftest[24],"Failed at state 24:");
fail[24]=TRUE;
//ftest[strlen("Failed at state 24:")]='\0';
ope=FALSE;
}
break;
case '=':
next +=1;
if(*next ==' ' || *next=='\0')
{
ar[1]='\0';
ope=TRUE;
current=next;
ope_final[4]=TRUE;
}
else
{
//printf("\n Failed at state 26:");
strcpy(ftest[26],"Failed at state 26:");
fail[26]=TRUE;
//ftest[strlen("Failed at state 26:")]='\0';
ope=FALSE;
}
break;
case '>':
next +=1;
if(*next ==' ' || *next=='\0')
{
ar[1]='\0';
ope=TRUE;
current=next;
ope_final[5]=TRUE;
}
else
{
//printf("\n Failed at state 28:");
strcpy(ftest[28],"Failed at state 28:");
fail[28]=TRUE;
//ftest[strlen("Failed at state 28:")]='\0';
ope=FALSE;
}
break;
case '<':
next +=1;
if(*next ==' ' || *next=='\0')
{
ar[1]='\0';
ope=TRUE;
current=next;
ope_final[6]=TRUE;
}
else
{
//printf("\n Failed at state 30:");
strcpy(ftest[30],"Failed at state 30:");
fail[30]=TRUE;
//ftest[strlen("Failed at state 30:")]='\0';
ope=FALSE;
}
break;
default:
ope=FALSE;
//printf("\n Failed at state 17 in operator transistion:");
strcpy(ftest[17],"Failed at state 17:");
fail[17]=TRUE;
//ftest[strlen("Failed at state 17:")]='\0';
break;
}// end of switch
if(ope==TRUE)
printf("\n %s\t\t Operator",ar);
else
{}//printf("\n %s\t\t is an Invalid Token:",ar);
}     // end of if for operator transisition
// State Table for operators
for(i=0;i<7;i++)
{
if(ope_final[i]==TRUE)
{
printf("\n %s\t\t 17(s)------>%d",ar,18+i*2);
printf("\n *D\t\t %d--------->%d(f)",18+i*2,(18+i*2)+1);
printf("\n");
}
}
// start of transistion for symbols
if(key==FALSE && lit==FALSE && ide==FALSE && ope==FALSE)// && negative == TRUE)
{
//printf("\n in SYMBOLSSSSSSSSSSSSSSSSS");
strcpy(ar,"");
current=start;
next=current;
ar[0]=*current;
//    printf("\n>>>>>>>>> %c",*current);
switch(*current)
{
case '{':
next +=1;
if(*next ==' ' || *next=='\0')
{
ar[1]='\0';
symb=TRUE;
current=next;
sym_final[0]=TRUE;
}
else
{
//printf("\n Failed at state 33:");
strcpy(ftest[33],"Failed at state 33:");
fail[33]=TRUE;
//ftest[strlen("Failed at state 33:")]='\0';
symb=FALSE;
}
break;
case ',':
next +=1;
if(*next ==' ' || *next=='\0')
{
ar[1]='\0';
ope=TRUE;
current=next;
sym_final[1]=TRUE;
}
else
{
//printf("\n Failed at state 35:");
strcpy(ftest[35],"Failed at state 35:");
fail[35]=TRUE;
//ftest[strlen("Failed at state 35:")]='\0';
symb=FALSE;
}
break;
case '(':
next +=1;
if(*next ==' ' || *next=='\0')
{
ar[1]='\0';
symb=TRUE;
current=next;
sym_final[2]=TRUE;
}
else
{
//printf("\n Failed at state 37:");
strcpy(ftest[37],"Failed at state 37:");
fail[37]=TRUE;
//ftest[strlen("Failed at state 37:")]='\0';
symb=FALSE;
}
break;
case ')':
next +=1;
if(*next ==' ' || *next=='\0')
{
ar[1]='\0';
symb=TRUE;
current=next;
sym_final[3]=TRUE;
}
else
{
//printf("\n Failed at state 39:");
strcpy(ftest[39],"Failed at state 39:");
fail[39]=TRUE;
//ftest[strlen("Failed at state 39:")]='\0';
ope=FALSE;
}
break;
case ';':
next +=1;
if(*next ==' ' || *next=='\0')
{
ar[1]='\0';
symb=TRUE;
current=next;
sym_final[4]=TRUE;
}
else
{
//printf("\n Failed at state 41:");
strcpy(ftest[41],"Failed at state 41:");
fail[41]=TRUE;
//ftest[strlen("Failed at state 41:")]='\0';
symb=FALSE;
}
break;
case '?':
next +=1;
if(*next ==' ' || *next=='\0')
{
ar[1]='\0';
symb=TRUE;
current=next;
sym_final[5]=TRUE;
}
else
{
//printf("\n Failed at state 43:");
strcpy(ftest[43],"Failed at state 43:");
fail[43]=TRUE;
//ftest[strlen("Failed at state 43:")]='\0';
symb=FALSE;
}
break;
case '<':
next +=1;
if(*next ==' ' || *next=='\0')
{
ar[1]='\0';
symb=TRUE;
current=next;
sym_final[6]=TRUE;
}
else
{
 // printf("\n Failed at state 30:");
symb=FALSE;
}
break;
default:
symb=FALSE;
//printf("\n Failed at state 32 in operator transistion:");
strcpy(ftest[32],"Failed at state 32:");
fail[32]=TRUE;
//ftest[strlen("Failed at state 32:")]='\0';
break;
}// end of switch
if(symb==TRUE)
printf("\n %s\t\t Symbol",ar);
else
{
if(atof(ar) || ar[0]=='0')
{
if(lit_t==FALSE && j>=1)
printf("\n %s\t\t is an Invalid Identifier:",ar);
else
printf("\n %s\t\t is an Invalid Constant:",ar);
}
else
printf("\n %s\t\t is an Invalid Token:",ar);
}
}              // end of if for symbol transistion
//printf("\ncurrent =[%c]",*current);
// State table for Symbols
for(i=0;i<7;i++)
{
if(sym_final[i]==TRUE)
{
printf("\n %s\t\t 32(s)------>%d",ar,33+i*2);
printf("\n *D\t\t %d--------->%d(f)",33+i*2,(33+i*2)+1);
printf("\n");
}
}
// Display failed states
printf("\n Want to see the failed states in different transistion 1/2:");
scanf("%d",&choice);
switch(choice)
{
case 1:
for(i=0;i<45;i++)
if(fail[i]==TRUE)
printf("\n%s",ftest[i]);
break;
case 2:
default:
break;
}
// variables resetting
for(i=0;i<11;i++)
iden_final[i]=FALSE;
for(i=0;i<7;i++)
ope_final[i]=sym_final[i]=FALSE;
for(i=0;i<45;i++)
fail[i]=FALSE;
key=TRUE;
ide_len=0;
ide=TRUE;
ide_t=TRUE;
lit=TRUE;
lit_t=TRUE;
ope=TRUE;
symb=TRUE;
negative=FALSE;
strcpy(ar,"");
printf("\n");
current=start;
next=current;
//printf("\n current =[%c]",*current);
do
{
if(*current=='\0' || *current=='\n')
break;

 // printf("\n current(loop) =[%c]",*current);
current++;
} while(*current!=' ');
do
{
if(*current=='\0' || *current=='\n')
break;

  // printf("\n current(loop) =[%c]",*current);
current++;
} while(*current==' ');
//printf("\n after current =[%c]",*current);
if(*current=='\0' || *current=='\n')
break;
next=current;
start=current;
super=current;
//printf("\n super =[%c]",*super);
if(*super=='\0' || *super =='\n' || *super ==' ')
break;
} // one token classification
return 0;
}

Thursday, August 1, 2013

Uniform Symbol Table

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;
}

Wednesday, July 10, 2013

Tokens

Given a program statement in C, list the tokens in the order as they appear in the source statement. Assume that the source statement may contain more than one blank space between two successive tokens and classify the tokens

      

#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<malloc.h>
void  classification(char iden[]);
#define TRUE 1
#define FALSE 0
int main(void)
{

     char *str,ar[10];
    char *token;
     int FLAG=FALSE,count=0,n,len,i,t=0;
    printf("\n Enter the maximum length of the statement:");
     scanf(" %d", &n);
     str=(char *)malloc( n*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;
 //   printf("\n The tokens are:\n");
    while(1)
        {
        while(*token!= ' ')
            {
            if(*token== '\0')
                {
                FLAG=TRUE;
                break;
                }
             //    printf("%c",*token);
                ar[count++]=*token;
                token++;
                }
                ar[count]='\0';
      //      printf(" %s ",ar);
             classification(ar);
      //   printf("\n");
          if(FLAG==TRUE)
            break;
        while(*token== ' ')    
            {
            *token++;
            if(*token== '\0')
                {
                     FLAG=TRUE;
                break;
                }
                }
          if(FLAG==TRUE)
                break;
          count=0;
          }
     return 0;
}
void classification( char ar[])
{

int  FLAG=FALSE,len,count=0,flag=TRUE,numb=0,d,t=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;
      }
    }
    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;
        }
    }
    i=0;
for(i=0;i<=7;i++)
    {
    if(strcmp(symbols[i],ar)==0)
        {
        printf("\n%s\tis a Symbol:",ar);
        FLAG=TRUE;
        flag=FALSE;
        }
    }
if ( ((65 <= ar[0] && ar[0] <= 93 )|| ( 97 <= ar[0] && ar[0] <= 122)) && len <= 8 && FLAG==FALSE)
    {
    printf("\n%s\tis an Identifier:",ar);
    FLAG=TRUE;
    flag=FALSE;
    }
for(i=0;i<len;i++)
    {
    if(49 <= ar[i] && ar[i] <=57 )
        count++;
    else
       continue;
    }
if(count==len && flag==TRUE && FLAG==FALSE)
    {
    printf("\n%s\tis a Constant:",ar);
    FLAG=TRUE;
    }
i=0;
if( FLAG==FALSE)
    {
    if(ar[0]=='-' || ar[0]=='0')
        numb=atoi(ar);
    if(numb <= 0)
    printf("%d\tis not a valid Constant ",numb);
   else
    printf("\n%s\tis an INVALID TOKEN:",ar);
   }
printf("\n");
return;
}