C program for Lexical Analyzer

Monday, March 29th, 2010

C program for Lexical Analyzer

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
struct pgm
 {
   char line[20];
 } s[100];
void check(char s[])
 {
    cout<<"\n";
    if(!strcmpi(s,"If"))
     {
        cout<<"keyword:If";
        return;
     }
    if(!strcmpi(s,"Then"))
     {
       cout<<"keyword:Then";
       return;
     }
    if(!strcmpi(s,"Else"))
     {
       cout<<"keyword:else";
       return;
     }
    if(!strcmpi(s,"[END]"))
     {
       return;
      }
    cout<<"expression:"<<s;
 }
void main()
 {
   char t[20];
   int i=0,j=0,k=0;
   clrscr();
   cout<<"\n\n enter the program code:(to stop input type End)\n";
   do
    {
      gets(s[i].line);
    }
 while(strcmpi(s[i++].line,"END"));
  k = k-1;
  do
   {
     k++;
     for(i=0;s[k].line[i]!='\0';i++,j++)
   {
     if(s[k].line[i]==' ')
      {
        t[j]='\0';
        j=-1;
       check(t);
      }
    else
       t[j]=s[k].line[i];
  }
 t[j]='\0';
 j=0;
check(t);
  }
 while(strcmpi(s[k].line,"END"));
 getch();
}

Author Name :
Ranjith

Total : 9 Comments


9 Responses to “C program for Lexical Analyzer”

  1. ashwin says:

    can u post a program which identifies the digits,special characters separately

  2. Anusha says:

    can u send the output for this program

  3. palash says:

    no comment.
    i am searching for an easy way……..

  4. neha says:

    plzz tell me the code for parse tree

  5. Anandhu says:

    BUT ALL THE CASES ARE NOT DISCUSSED HERE…ITZ ONLY A SMALL PART…

  6. piyush says:

    this is the most common way of analyzing text, but there’s another easier way, that is by using lexical analyzer method,the strings to be matched are kept between % % along with the actions to be taken if the matching is found, this is generally used for designing you own programming language. u can learn more from books

  7. darshan soni says:

    it’s not working..plz send another….

  8. Shafiq says:

    send me output plz………….!

  9. suresh says:

    how to implement this program….can you provide me a brief way to execute this program…

Leave a Reply

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup

Email: