In this article, we are going to cover how the lexical analyzer works and will also cover the basic architecture of lexical analyzer. Let’s discuss one by one.
Pre-requisite – Introduction to Lexical Analyzer
Lexical Analyzer :
- It is the first phase of a compiler is known as Scanner (It’s scan the program).
- Lexical Analyzer will divide the program into some meaningful strings which are known as a token.

Types of token as following –
- Identifier
- Keyword
- Operator
- Constants
- Special symbol(@, $, #)
Above is the terminologies of token which is the key component for working in Lexical Analyzer. Now, with the help of example, you will see how it works.
Let’s consider the following C program given below to understands the working.
int main)(
}
x = y+z;
int x, y, z;
print("Goto GFG %d%d", a);
{
In the first phase, the compiler doesn’t check the syntax. So, here this program as input to the lexical analyzer and convert it into the tokens. So, tokenization is one of the important functioning of lexical analyzer.
The total number of token for this program is 26. Below given is the diagram of how it will count the token.

In this above diagram, you can check and count the number of tokens and can understand how tokenization works in lexical analyzer phase.
This is how you can understand each phase in compiler with clarity and will get an idea of how compiler works internally and each phase of the compiler is the key step.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
Recommended Posts:
- Flex (Fast Lexical Analyzer Generator )
- Difference between Native compiler and Cross compiler
- Incremental Compiler in Compiler Design
- Working of Compiler Phases with Example
- Introduction of Lexical Analysis
- Compiler Theory | Set 1
- Compiler Theory | Set 2
- Introduction to Syntax Analysis in Compiler Design
- Why FIRST and FOLLOW in Compiler Design?
- Runtime Environments in Compiler Design
- Syntax Directed Translation in Compiler Design
- Intermediate Code Generation in Compiler Design
- Peephole Optimization in Compiler Design
- Language Processors: Assembler, Compiler and Interpreter
- Code Optimization in Compiler Design
- Introduction of Object Code in Compiler Design
- Introduction of Compiler Design
- Phases of a Compiler
- Compiler Design | Syntax Directed Definition
- Error detection and Recovery in Compiler
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

