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++


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++.


5 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.

    1. eagle-i

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

    2. Mustapha

      Use this code
      At the very end of the code. Just above the int main() coily bracket.

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



Leave a Reply

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