About

Kannur University btech CSE study materials, question papers, syllabus . . .

Saturday, June 8, 2013

S6 CSE COMPILER LAB : Lexical Analyser Using C Program

/*Lexical Analyser Using C Program */     

#include<stdio.h>
#include<string.h>
char str[50];
int st;
char c;
void main(int argc,char *argv[])
{
 FILE *ptr;
 int l,i,j,f,k; 
 f=0;
 char key[50][50]={"int","for","do","while","if","char","float","double","case","struct","void","switch","else","return","break"};
 char sps[50][50]={"[","]","{","}","(",")","#",";","\\","'","\"",".","!","?"};
 char op[50][50]={"&&","<",">","||","=","+","-","*","/","%"};
 char fs[50][50]={"%d","%s","%f","%c"};
 char escp[50][50]={"\n","\t","\b","\\n","\\t","\\a","\\b"};
 if(argc!=2)
  printf("Invalid argument list\n");
 else if((ptr=fopen(argv[1],"r"))==NULL)
  printf("File is Empty\n");
 else
 {
  c=fgetc(ptr);
  while(feof(ptr)!=1)
  {
  st=0;
  str[0]=c;
  for(i=1;str[i]!='\n';i++)
  {
    c=fgetc(ptr);
    if(c=='\n')
    break;
    str[i]=c;
  }
  str[i]='\0';
  puts(str);
  k=0;
   for(i=0;i<50;i++)
   {
    if(strcmp(str,key[i])==0)
     {
      printf("The string is a keyword\n");
      k=1;
      break;
     }
    else if(strcmp(str,sps[i])==0)
     {
      printf("The string is a special charactor\n");
      k=1;
      break;
     }
     else if(strcmp(str,op[i])==0)
     {
      printf("The string is an operator\n");
      k=1;
      break;
     }
     else if(strcmp(str,fs[i])==0)
     {
     printf("The string is an format specifiers\n");
     k=1;
     break;
     }
     else if(strcmp(str,escp[i])==0)
     {
      printf("The string is an escape sequence\n");
      k=1;
      break;
     }
  }
  if(k==0)
  {
   l=strlen(str);
   for(i=0;i<l;i++)
   {
   if(st==0)
   {
    if((str[i]>=65&&str[i]<=90)||(str[i]>=97&&str[i]<=122)||(str[i]==95))
      {st=1;l--;}
    else if(str[i]>=49&&str[i]<=57)
      st=2;
    else if((str[i]==48&&str[i+1]==46)||(str[i]==46))
     {
     st=3;
     i++;
     }
   else
    st=4;
   }
   else if(st==1)
   {
   if((str[i]>=65&&str[i]<=90)||(str[i]>=97&&str[i]<=122)||(str[i]>=48&&str[i]<=57)||(str[i]==95))
      st=1;
   else
      st=4;
    }
  else if(st==2)
  {
   if(str[i]>=48&&str[i]<=57)
   st=2;
  else if((str[i]==46))
   st=3;
  else
   st=4;
  }
 else if(st==3)
 {
  if(str[i]>=49&&str[i]<=57)
  st=3;
  else if(str[i]==48)
  {
   for(j=i+1;j<l;j++)
   {
    if(str[j]!=48)
      st=3;
    else
      st=4;
   }
  }
  else
  st=4;
 }
 else
 {
  if(str[i]>=48&&str[i]<=57)
  st=4;
  else
  st=4;
 }
}
if(st==1)
  printf("The string is a valid Identifier\n");
else if(st==2)
 printf("The string is a valid number\n");
else if(st==3)
 printf("The string is a valid floating point number\n");
else
 printf("The string is an invalid token\n");
}
c=fgetc(ptr);
}
}
}

File

manju
while do int float
1.3565
1334
\n
}
/
%d
\t
5gf

Output

manju
The string is a valid Identifier
while
The string is a keyword
1.3565
The string is a valid floating point number
1334
The string is a valid number
\n
The string is an escape sequence
}
The string is a special charactor
/
The string is an operator
%d
The string is an format specifiers
\t
The string is an escape sequence
5gf

The string is an invalid token

0 comments:

Post a Comment