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

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