• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
projectsgeek

ProjectsGeek

Download Mini projects with Source Code, Java projects with Source Codes

  • Home
  • Java Projects
  • C++ Projects
  • VB Projects
  • PHP projects
  • .Net Projects
  • NodeJs Projects
  • Android Projects
    • Project Ideas
      • Final Year Project Ideas
      • JSP Projects
  • Assignment Codes
    • Fundamentals of Programming Language
    • Software Design Laboratory
    • Data Structure and Files Lab
    • Computer Graphics Lab
    • Object Oriented Programming Lab
    • Assembly Codes
  • School Projects
  • Forum

Lexical analyzer Code in C Language

May 4, 2011 by ProjectsGeek Leave a Comment

Lexical analyzer Code in C Language

Implement Lexical Analyzer code for subset of C using C Language.

Lexical Analysis-Finite Automate, Regular Expression, RE to DFA,Implementation of lexical Analyzer,Syntax Analysis,Context Free Grammars ,Derivation of Parse Tress,Parsers,Top Down Parsers: Recursive Descent Parser, Predictive Parser,Bottom Up Parsing Shift Reduce Parser, SLR parser and Lexical analyzer Code.

 Lexical analyzer Code

#include  
 #include  
 #include  
 #include  
 #define max 100  
 typedef struct lit_tab  
 {  
   char token[max];  
 }littab;  
 typedef struct iden_tab  
 {  
   char token[max];  
 }identab;  
 typedef struct ust  
 {  
   char token[max];  
   char type;  
   int pos;  
 }ustx;  
 littab lit_tab[50];  
 identab iden_tab[50];  
 ustx ust[100];  
 int ust_ptr=0;  
 char terminal[max][max]={"{","}","(",")","<",">","<=",">=","=",",",";","\"","+","-","*","/","void","printf","int","float","if","else"};  
 void print_ust()  
 {  
   printf("UST\n");  
   int i=0;  
   printf("TOKEN\tTYPE\tPOS\n");  
   while(i<ust_ptr) <br="">   {  
     printf("%s\t%c\t%d\n",ust[i].token,ust[i].type,ust[i].pos);  
     i++;  
     getch();  
   }  
 }  
 void print_ter()  
 {  
   printf("Literal Table");  
   int i=0;  
   clrscr();  
   while(strcmp(terminal[i],NULL)!=0)  
   {  
     printf("%d.%s\n",i+1,terminal[i]);  
     i++;  
   }  
   getch();  
 }  
 void print_lit()  
 {  
   printf("\nLiteral Table");  
   int i=0;  
   clrscr();  
   while(strcmp(lit_tab[i].token,NULL)!=0)  
   {  
     printf("%d.%s\n",i+1,lit_tab[i].token);  
     i++;  
   }  
   getch();  
 }  
 void print_iden()  
 {  
   printf("\nIdentifier Table");  
   int i=0;  
   clrscr();  
   while(strcmp(iden_tab[i].token,NULL)!=0)  
   {  
     printf("%d.%s\n",i+1,iden_tab[i].token);  
     i++;  
   }  
   getch();  
 }  
 int search_lit(char str[])  
 {  
   int i=0;  
    while(strcmp(lit_tab[i].token,NULL)!=0)  
    {  
     if(strcmp(lit_tab[i].token,str)==0)  
       return i;  
     i++;  
    }  
    return -1;  
 }  
 int insert_lit(char str[])  
 {  
   int i=0;  
   while(strcmp(lit_tab[i].token,NULL)!=0)  
   {  
     i++;  
   }  
   strcpy(lit_tab[i].token,str);  
   return i;  
 }  
 int search_iden(char str[])  
 {  
   int i=0;  
    while(strcmp(iden_tab[i].token,NULL)!=0)  
    {  
     if(strcmp(iden_tab[i].token,str)==0)  
       return i;  
     i++;  
    }  
    return -1;  
 }  
 int insert_iden(char str[])  
 {  
   int i=0;  
   while(strcmp(iden_tab[i].token,NULL)!=0)  
   {  
     i++;  
   }  
   strcpy(iden_tab[i].token,str);  
   return i;  
 }  
 int search(char str[])  
 {  
   int i=0;  
   while(strcmp(terminal[i],NULL)!=0)  
   {  
     if(strcmp(terminal[i],str)==0 )  
       return i;  
     i++;  
   }  
   return -1;  
 }  
 void ust_insert(char token[],char type,int pos)  
 {  
   strcpy(ust[ust_ptr].token,token);  
   ust[ust_ptr].type=type;  
   ust[ust_ptr].pos=pos;  
   ust_ptr++;  
 }  
 void main()  
 {  
   FILE *fp;  
   clrscr();  
   fp=fopen("lex.txt","r");  
   if(fp==NULL)  
   {  
     printf("File Not Found");  
     return;  
   }  
   char str[max],token[max];  
   int i,j;  
   while( !feof(fp) )  
   {  
     fgets(str,max,fp);  
     i=0;  
     strcpy(token,NULL);  
     while(str[i]!='\n' && str[i]!=NULL)  
     {  
       while(str[i]==' ' || str[i]=='\t')  
         i++;  
       j=0;  
       if(str[i]=='/')  
       {  
         if(str[i+1]=='/')  
         {  
           fgets(str,max,fp);  
           continue;  
         }  
         else if(str[i+1]=='*')  
         {  
           i+=2;  
           while(str[i]!='*' && str[i+1]!='/')  
           {  
             if(str[i]=='\n')  
             {  
               fgets(str,max,fp);  
               i=0;  
             }  
             i++;  
           }  
           i+=2;  
         }  
       }  
       while(str[i]!=' ' && str[i]!='\t' && str[i]!='\n')  
       {  
         if(str[i-1]=='"' && str[i]!=')')  
         {  
           while(str[i]!='"')  
           {  
             token[j]=str[i];  
             j++;i++;  
           }  
           break;  
         }  
         token[j]=str[i];  
         j++;  
         i++;  
         if(ispunct(str[i]))  
             //if(str[i]=='(' || str[i]==')' ||str[i]==';' ||str[i]==','||str[i]=='+'||str[i]=='='||str[i]=='-'||str[i]=='/'||str[i]=='*')  
           break;  
         if(ispunct(str[i-1]) )  
             //if(str[i-1]==','||str[i-1]=='='||str[i-1]=='/'||str[i-1]=='+'||str[i-1]=='-'||str[i-1]=='*')  
           break;  
       }  
       token[j]=NULL;  
       int x;  
       int l=strlen(token);  
       int pos;  
       x=search(token);  
       if(x!=-1)  
         ust_insert(token,'T',x);  
       else if(str[i-l-1]!='"' && !(token[0]>='0'&& token[0]<='9')&&strcmp(token,NULL)!=0 )  
       {  
         x=search_iden(token);  
         if(x==-1)  
         {  
           pos=insert_iden(token);  
           ust_insert(token,'I',pos);  
         }  
         else  
           ust_insert(token,'I',x);  
       }  
       else if( strcmp(token,NULL)!=0)  
       {  
         x=search_lit(token);  
         if(x==-1)  
         {  
           pos=insert_lit(token);  
           ust_insert(token,'L',pos);  
         }  
         else  
           ust_insert(token,'L',x);  
       }  
     }  
   }  
   print_ter();  
   print_lit();  
   print_iden();  
   print_ust();  
   getch();  
 }

 

Other Projects to Try:

  1. Prims algorithm Code in C Language
  2. Breadth First and Depth First Search C Language
  3. Dijkstra Algorithm in C Language
  4. BFS AND DFS Algorithm using C Language
  5. Lexical Analyzer for Subset of C

Filed Under: System Programming

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Tags

.Net Projects Download Android Project Ideas Android Projects Angular 2 Assembly Codes C # Projects C & C++ Projects C++ Projects Class Diagrams Computer Graphics Database Project Data Mining Projects DataScience Projects Datastructure Assignments Download Visual Basic Projects Electronics project Hadoop Projects Installation Guides Internet of Things Project IOS Projects Java Java Interview Questions Java Projects JavaScript JavaScript Projects java tutorial JSON JSP Projects Mechanical Projects Mongodb Networking Projects Node JS Projects OS Problems php Projects Placement Papers Project Ideas Python Projects seminar and presentation Struts

Search this Website


Footer

Download Java Project
Download Visual Basic Projects
Download .Net Projects
Download VB Projects
Download C++ Projects
Download NodeJs Projects
Download School Projects
Download School Projects
Ask Questions - Forum
Latest Projects Ideas
Assembly Codes
Datastructure Assignments
Computer Graphics Lab
Operating system Lab
australia-and-India-flag
  • Home
  • About me
  • Contact Form
  • Submit Your Work
  • Site Map
  • Privacy Policy