WHAT'S NEW?
Loading...

Lexical Analyzer in C

/*  
      Lexical Analyzer for 'C' Language  
      Author: Yasar Shaikh  
      Lexical Analyzer: http://en.wikipedia.org/wiki/Lexical_analysis 
*/  
#include<stdio.h>  
#include<string.h>  
#include<ctype.h>  
#define MAX 1000  
int main()  
{  
     //FILE pointer for getting input file i for general index, 
     //flag for var identification  
     FILE *fp;  
     int keyIndex,preIndex,varIndex,oprIndex,i,flag;  
     char keyStore[MAX][10],preStore[MAX][10],varStore[MAX][10];  
     char oprStore[MAX],fname[80],ch,temp[80];  
     char keyword[32][20]={  
                            "auto","break","case","continue",
                            "char","const","default","do",
                            "double","else","enum","extern",
                            "float","for","goto","if",
                            "int","long","register","return",
                            "short","signed","sizeof","static",
                            "struct","switch","typedef","union",
                            "unsigned","void","volatile","while"  
                          };  
     char preprocessor[2][20]={"include","define"};  

     printf("\nEnter file name:");  
     scanf("%s",fname);  
     //opening a file in Read mode n checking for its availabilty  
     fp=fopen(fname,"r");  
     if(NULL == fp)  
     {  
          printf("\nFile not found.");  
          return 0;  
     }  

     keyIndex=varIndex=preIndex=oprIndex=0;  

     //Do operation until EOF is reached.  
     while(!feof(fp))  
     {  
          ch=fgetc(fp);  
          if(!isalnum(ch))  
               oprStore[oprIndex++]=ch;  
          else  
          {       
               //getting string  
               i=flag=0;  
               temp[i]=ch;  
               i++;  
               temp[i]=fgetc(fp);  
               while( isalnum(temp[i]) )  
                    temp[++i]=fgetc(fp);  
               temp[i]='\0';  

               fseek(fp, SEEK_CUR-2 , SEEK_CUR);  

               //checking is it a keyword/var/preprocessor  
               for(i=0;i<32;i++)  
               {  
                    //keyword checking n storing  
                    if(strcmp(temp,keyword[i]) ==0)  
                    {  
                         strcpy(keyStore[keyIndex++],temp);  
                         flag=1;  
                         break;  
                    }  
                    
                    //preprocessor checking n storing  
                    else if(strcmp(temp,preprocessor[i])==0)  
                    {  
                         strcpy(preStore[preIndex++],temp);  
                         flag=1;  
                         break;  
                    }  
               }   

               //checking for variables  
               if(!flag)  
               {  
                    strcpy(varStore[varIndex++],temp);  
                    strcpy(temp,"");  
               }  
          }   
     }  

     printf("\n******* OUTPUT *******");  
     printf("\n\nPreprocessor's List");  
     for(i=0;i<preIndex;i++)  
          printf("\n%s",preStore[i]);  

     printf("\n\nKeyword List");  
     for(i=0;i<keyIndex;i++)  
          printf("\n%s",keyStore[i]);  

     printf("\n\nVariable List");  
     for(i=0;i<varIndex;i++)  
          printf("\n%s",varStore[i]);  

     printf("\n\nOperators List");  
     for(i=0;i<oprIndex;i++)  
          printf("\n%c",oprStore[i]);       

     return 0;  
}  

/*  
  PS; You can even seperate standard header files, 
      predefined identifiers.  
      The above program mainly focus on important tokens.   
*/  

0 comments: