
A Lexical Analyzer is the first phase of a compiler that reads the source code character by character and converts it into tokens .
A Lexical Analyzer is the first phase of a compiler that reads the source code character by character and converts it into tokens.
π It acts as an interface between source code and the parser (syntax analyzer).
Source Code β [Lexical Analyzer] β Tokens β Syntax Analyzer
A token is the smallest meaningful unit in a program.
int x = 10;
π Tokens:
int | x | = | 10 | ;
Each token has:
| Token | Type | Attribute |
|---|---|---|
| int | keyword | β |
| x | identifier | pointer to symbol table |
| 10 | number | value = 10 |
Examples:
@, #2abcπ Converts characters β tokens π Ignores spaces and comments π Works using patterns (regular expressions)
Input Buffering is a technique used by the lexical analyzer to read input efficiently by storing it in memory buffers instead of reading one character at a time.
Reading character-by-character from file is slow.
π Solution: Use buffers to read large chunks at once.
[ Input Buffer ]
| a | b | c | d | e | EOF |
Uses two buffers to improve efficiency.
Buffer 1 Buffer 2
[ a b c d ] [ e f g h ]
β β
forward pointer reloaded
A sentinel is a special character (usually EOF) placed at the end of buffer to indicate buffer boundary.
int x = 10;
^ ^
LB FP
(LB = Lexeme Begin, FP = Forward Pointer)
Input:
count = count + 1;
Steps:
Buffer loads input
FP moves character by character
Tokens identified:
count | = | count | + | 1 | ;
π Frequently asked:
| Topic | Description | Key Feature | Advantage | Exam Importance |
|---|---|---|---|---|
| Lexical Analyzer | Converts source code to tokens | First compiler phase | Simplifies parsing | Very High |
| Token | Smallest unit | Type + attribute | Easy processing | High |
| Input Buffering | Efficient input reading | Uses memory buffer | Faster than char reading | High |
| Single Buffer | One buffer | Simple | Slow | Medium |
| Double Buffer | Two buffers | Uses sentinel | Fast, efficient | Very High |
| Sentinel | End marker | EOF symbol | Avoids checks | Very High |
| Pointers | Track positions | LB & Forward | Helps scanning | High |
Open this section to view and download past papers
Click on Show Past Papers to load available exam papers.