Thursday 21 March 2013

scanner lexical compiler first phase


#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#define MAX 32
int h[MAX]={0};
int keyw[MAX]={0};
char ident[20][30];


char key[MAX][30]={{'s','t','r','u','c','t'}
,{'v','o','i','d'}
,{'a','u','t','o'}
,{'b','r','e','a','k'}
,{'c','h','a','r'}
,{'c','o','n','s','t'}
,{'c','o','n','t','i','n','u','e'}
,{'d','e','f','a','u','l','t'}
,{'d','o','u','b','l','e'}
,{'e','n','u','m'}
,{'e','x','t','e','r','n'}
,{'f','l','o','a','t'}
,{'f','o','r'}
,{'g','o','t','o'}
,{'l','o','n','g'}
,{'r','e','g','i','s','t','e','r'}
,{'s','h','o','r','t'}
,{'s','i','g','n','e','d'}
,{'s','t','a','t','i','c'}
,{'u','n','i','o','n'}
,{'v','o','l','a','t','i','l','e'}
,{'u','n','s','i','g','n','e','d'}
,{'i','n','t'}
,{'t','y','p','e','d','e','f'}
,{'e','l','s','e'}
,{'i','f'}
,{'d','o'}
,{'c','a','s','e'}
,{'s','w','i','t','c','h'}
,{'w','h','i','l','e'}
,{'s','i','z','e','o','f'}
,{'r','e','t','u','r','n'}};
void display(int x)
{
int i=0;
char c;
// printf("  Key:");
   printf("\n");
do
{
printf("%c",key[x][i]);
i++;
}while(i!=20);
}

int main()
{
int keys=0;  
    FILE *f;
    f=fopen("tree.txt","r");
   
    if(f==NULL)
    {
               printf("\nFAIL");
               getch();
             
    }
   
    char c,id[20];
    int i,resulti=0,resultk=0,flag,x,z,count=0,l=1,no_of_identifiers=0;
    int brackets=0,string=0;
    printf("Tokens\n");
    while((c=getc(f))!=EOF)
    {
        if(c=='"' && string==0)
        {
                  printf("\n\"");
                  string=1;
        }
        else if(c=='"' && string==1)
         {
                           string=0;
                           printf("\n\"");
         }            
        if(c=='(' && brackets==0)
                  brackets=1;
        else if(c==')' && brackets==1)
                       brackets=0;
       
       
        if(string==0)
        {                  
        if(count==0 && (isalnum(c)-isalpha(c)))
        {
                 
                    resultk=-1;
                    resulti=-1;
        }
       
         flag=0;                
         if(isalpha(c))
{
            if(resultk!=-1)
            {        
          for(i=0;i<MAX;i++)
          {
            if(h[i]==count && c==key[i][count])
           {
                                h[i]+=1;
                                    flag=1;                            
                            }  
                       }
                       if(flag==0)
                                  resultk=-1;      
            }
id[count]=c;
++count;      
        }
        else if(c=='\n')
        {
            for(i=0;i<MAX;i++)
                    h[i]=0;
           
           resultk=0;
           count=0;
           l++;
      }
      else if(c==' ')
{
                   if(count!=0)
                   {
                  for(x=0;x<MAX;x++)
       {
      if(h[x]==count)
      {
                                 
                                   keyw[keys++]=x;
                                   //no_of_keys+=1;
                  // printf("\nline:%d",l);
           display(x);
                     }            
                  }
                  for(i=0;i<MAX;i++)
                    h[i]=0;
                  resultk=0;
                  resulti=0;
     count=0;
                           
                }
        }
     
        else if(c==',')
        {
             if(count!=0)
             {
             //printf("\nline:%d  identifier:",l);
             printf("\n");
             for(i=0;i<count;i++)
             {
                        printf("%c",id[i]);        
                        ident[no_of_identifiers][i]=id[i];
             }
             no_of_identifiers++;
                        //printf("%c",id[i]);
              }
             count=0;
             resulti=0;              
                 
        }
        else if(c=='=')
        {
           
             if(count!=0)
             {
             //printf("\nline:%d  identifier:",l);
             printf("\n");
             for(i=0;i<count;i++)
             {
                        printf("%c",id[i]);        
                        ident[no_of_identifiers][i]=id[i];
             }
             no_of_identifiers++;
              }
               printf("\n=");
             count=0;
             resulti=0;              
                 
        }
        else if(c=='(' || c==')')
        {
             if(count>1)
             {
             //printf("\nline:%d  identifier:",l);
             printf("\n");
             for(i=0;i<count;i++)
             {
                        printf("%c",id[i]);        
                        ident[no_of_identifiers][i]=id[i];
             }
             no_of_identifiers++;
              }
               if(c=='(')
           {
                printf("\n(");
                count=0;
           }
           else if(c==')')
           {
                printf("\n)");
                count=0;
           }
             count=0;
             resulti=0;              
                 
        }
        else if(c==';' && brackets==0)
        {
         
             if(resulti!=-1)
             {
                if(count!=0)
                {          
             //printf("\nline:%d identifier:",l);
             printf("\n");
            for(i=0;i<count;i++)
             {
                        printf("%c",id[i]);        
                        ident[no_of_identifiers][i]=id[i];
             }
             no_of_identifiers++;
                }
             }
             printf("\n");
              for(i=0;i<count;i++)
                        printf("%c",id[i]);
                printf("\n;");
             count=0;
             resulti=0;              
           
        }
        else if(isalnum(c)-isalpha(c))
        {
             id[count]=c;
             count++;
        }
       else
       {
         
          count++;
          resultk=-1;
          resulti=-1;
          if(c=='#')
            {         printf("\n#");
                      count=0;
            }
           else if(c=='<')
           {
                printf("\ninclude\n<\n");
                count=0;
           }
           else if(c=='.')
           {
                for(i=0;i<count-1;i++)
                        printf("%c",id[i]);
                printf("\n.\nh\n>");
                count=0;
           }
           else if(c=='{')
           {
                printf("\n{");
                count=0;
           }
           else if(c=='}')
           {
                printf("\n}");
                count=0;
           }
             else if(c=='&')
           {
                printf("\n&");
                count=0;
           }
      }
                 
      }
}



    //  printf("\n\nSYMBOL TABLE\n\n\n\n");

      printf("\n\nKey\t\tidentifiers\n");
     
      int j,max=no_of_identifiers;
      if(max<keys)
                        max=keys;
                       
     
      for(i=0;i<max;i++)
      {
   
                        if(i<keys-1)
                        {
                                  display(keyw[i]);
                         }
                                           printf("\n\t\t");
                                         
                        if(i<=no_of_identifiers)
                        {
                                    for(j=0;j<10;j++)
                                                  printf("%c",ident[i][j]);                
                        }
      }





     getch();
}

No comments:

Post a Comment

Have some problem with this code? or Request the code you want if you cant find it in Blog Archive.