C program – Lexical Analyzer

by Nideesh C on April 17, 2011 · 0 comments

in C Programming




Lexical analyzer converts stream of input characters into a stream of tokens.
The different tokens that lexical analyzer identifies are as follows:

KEYWORDS: int, char, float, double, if, for, while, else, switch,
struct, printf, scanf, case, break, return, typedef, void

IDENTIFIERS: main, fopen, getch etc

NUMBERS: positive and negative integers, positive and negative floating point numbers.

OPERATORS: +, ++, -, –, ||, *, ?, /, >, >=, <, <=, =, ==, &, &&.

BRACKETS: [ ], { }, ( ).

STRINGS : Set of characters enclosed within the quotes

COMMENT LINES: Ignores single  line, multi line comments

#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();
}



Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. C program – Pass-1 Assembler
  2. C program to check whether a given integer number is positive or negative
  3. C program – Syntax Analyzer
  4. C++ program – convert an Expression from Infix expression to Prefix form
  5. C++ program – convert an Expression from Infix form to Postfix form

Leave a Comment

Previous post:

Next post: