Friday, May 24, 2013

program to simulate the lexical analyzer which generate symbol table



Aim: Write down program to simulate the lexical analyzer which generate symbol table according to user input.
# include <stdio.h>
# include <conio.h>
# include <string.h>
# include <stdlib.h>

void main()
{
      char str[100],str1[100],idn[100];
      int i,k,flag,con[100],j,p;
      clrscr();

      printf("\n\n=============LEXICAL ANALYZER================\n\n");
      printf("\n\nEnter the string::");
      gets(str);

      printf("\n\n---------------SYMBOLE TABLE------------------\n\n");
      printf("\n\t|| LEXEME   ||   TYPE      ||\n\n");

      i=0;
      while(str[i]!=' ')
      {
            str1[i]=str[i];
            i++;
      }
      str1[i]='\0';

      flag=strcmp(str1,"int");
      if(flag==0)
      {
            printf("\n\n\t|| int          keyword     ||");
      }
      else
      {
            flag=strcmp(str1,"char");
            if(flag==0)
            {
                  printf("\n\n\t|| char        keyword     ||");
            }
            else
            {
                  flag=strcmp(str1,"float");
                  if(flag==0)
                  {
                        printf("\n\n\t|| float        keyword     ||");
                  }
                  else
                  {

                        i=-1;
                  }
            }
      }

NEXT: i++;
      k=0;
      if(str[i]=='1'||str[i]=='2'||str[i]=='3'||str[i]=='4'||str[i]=='5')
      {
            printf("\n\ninvalid identifier start with %c",str[i]);
            goto END;
      }
      if(str[i]=='6'||str[i]=='7'||str[i]=='8'||str[i]=='9'||str[i]=='0'||str [i]=='_')
      {
            printf("\n\ninvalid identifier start with %c",str[i]);
            goto END;
      }
      while(str[i]!=',' && str[i]!=';' && str[i]!='=')
      {
            idn[k]=str[i];
            i++;
            k++;
      }
      idn[k]='\0';
      printf("\n\n\n\t||   %s          identifier  ||", idn);

      if(str[i]=='=')
      {
            printf("\n\n\n\t||   =          operator    ||");
            i++;
            while(str[i]!=',' && str[i]!=';')
            {
                  printf("\n\n\t||   %c  ",str[i]);
                  i++;
            }
            printf("        constant    ||");
      }

      if(str[i]!=';')
      {
            printf("\n\n\n\t||   ,          operator    ||");
            goto NEXT;
      }

      printf("\n\n\n\t||   ;          operator    ||");
      printf("\n\n---------------------------------------------\n\n");

END:  getch();


}

No comments:

Post a Comment