Lexical Analyzer in C and C++

Here you will get program to implement lexical analyzer in C and C++.

Compiler is responsible for converting high level language in machine language. There are several phases involved in this and lexical analysis is the first phase.

Lexical analyzer reads the characters from source code and convert it into tokens.

Lexical Analyzer in C and C++

Different tokens or lexemes are:

  • Keywords
  • Identifiers
  • Operators
  • Constants

Take below example.

c = a + b;

After lexical analysis a symbol table is generated as given below.

Token Type
c identifier
= operator
a identifier
+ operator
b identifier
; separator

Now below I have given implementation of very simple lexical analyzer which reads source code from file and then generate tokens.

Program for Lexical Analyzer in C

Program for Lexical Analyzer in C++

Output

Lexical Analyzer in C and C++

The source code present in file is shown in above image.

Comment below if you have any queries regarding above program for lexical analyzer in C and C++.

 

9 thoughts on “Lexical Analyzer in C and C++

  1. Midhat Aamir

    The code is running perfectly with no errors, but the output screen does not hold. It vanishes immediately. What should I do please tell me. I’ll be very grateful to you.

    Reply
    1. eagle-i

      Open Command Prompt, go to program location and then run the executable.

      Reply
    2. Mustapha

      Use this code
      System(“pause”);
      At the very end of the code. Just above the int main() coily bracket.

      Int main() {
      //ur own code
      System(“pause”) ;

      }

      Reply
    3. Rinnie

      include —–
      getch() at the end of the program in c language

      Reply
  2. Singapore Karthi

    The code is running perfectly with no errors, but the output screen does not hold. It vanishes immediately. What should I do please tell me. I’ll be very grateful to you.
    Compiler is responsible for converting high level language in machine language. There are several phases involved in this and lexical analysis is the first phase.std;

    int isKeyword(char buffer[]){
    char keywords[32][10] = {“auto”,”break”,”case”,”char”,”const”,”continue”,”default”,
    “do”,”double”,”else”,”enum”,”extern”,”float”,”for”,”goto”,
    “if”,”int”,”long”,”register”,”return”,”short”,”signed”,
    “sizeof”,”static”,”struct”,”switch”,”typedef”,”union”,
    “unsigned”,”void”,”vola

    hola hola hola hooooooooooooo!!!!!!!!!!!!!!!!!!!!!!!!!

    Reply
  3. anvita sharma

    if the input is an identifier ‘abc3’, it shows abc is an identifier and 3 is a digit.. please put a program accordingly to identify digits in identifiers and delimiters

    Reply
  4. Shubh

    in c program write 3 line code to hold the output
    #include //after #include
    getch(); // last line inside int main()
    clrscr(); // next line just after getch();

    Reply

Leave a Reply

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