WAP to find number of Tokens in an expression - Compiler Construction Lab file


WAP to find no of Tokens in an expression

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
 char st[100];
 int tcount=0,i;
 clrscr();
 printf("Enter the expression:");
 gets(st);
 for(i=0;st[i]!='\0';)
 {
    if(( st[i]>='A' && st[i]<='Z') || (st[i]>='a' && st[i]<='z'))
    {
      while(( st[i]>='A' && st[i]<='Z') || (st[i]>='a' && st[i]<='z'))
      {
 i++;
      }
      tcount++;
    }
    else if(st[i]>='0' && st[i]<='9')
    {
       while(st[i]>='0'&& st[i]<='9')
       i++;
       tcount++;
    }
    else if( st[i]==' '){i++;}
    else
    {
      switch(st[i])
      {
  case '"':
  case '(':
  case ')':
  case '[':
  case ']':
  case '*':
  case '/':
  case '%':
  case '^':
  case ',':
  case '=':
  case ';': tcount++;break;
  case '+':if(st[i+1]=='+')i++;tcount++;break;
  case '-':if(st[i+1]=='-')i++;tcount++;break;
  case '<':if(st[i+1]=='=')i++;tcount++;break;
  case '>':if(st[i+1]=='=')i++;tcount++;break;
  case '!':if(st[i+1]=='=')i++;tcount++;break;
  case '&':if(st[i+1]=='&')i++;tcount++;break;
  case '|':if(st[i+1]=='|')i++;tcount++;break;
      }
      i++;
    }
 //printf(" %d\n",tcount);
 }
 printf("Token count:%d",tcount);
 getch();
}

OUTPUT:


No comments:

Post a Comment