The structure of a compiler refers to how a compiler is organized into different parts (phases), where each phase performs a specific task in translating a program from high-level language → machine code.
Source Program
↓
Lexical Analysis
↓
Syntax Analysis
↓
Semantic Analysis
↓
Intermediate Code Generation
↓
Code Optimization
↓
Code Generation
↓
Target (Machine Code)
Two important components work with all phases:
Stores information about variables, functions, identifiers
Example:
Variable Name | Type | Scope
x | int | global
There are 6 main phases:
Converts source code into tokens (smallest units).
int x = 10;
👉 Tokens:
int | x | = | 10 | ;
👉 “Lexical analyzer converts characters → tokens”
Checks whether tokens follow grammar rules of the language.
Correct:
int x = 10;
Incorrect:
int = x 10;
=
/ \
x 10
👉 “Parser checks grammar using CFG (Context-Free Grammar)”
Checks the meaning of the program.
int x;
x = "Hello"; ❌ Error
👉 “Ensures program is logically correct”
Generates machine-independent intermediate code.
x = a + b
👉 Intermediate Code:
t1 = a + b
x = t1
👉 “Creates intermediate representation (IR)”
Improves code to make it faster and efficient.
Before:
x = a + 0
After:
x = a
👉 “Optimization improves performance without changing output”
Converts intermediate code into machine code.
ADD R1, R2
MOV R1, x
👉 “Final phase produces executable code”
int x = a + b;
| Phase | Output |
|---|---|
| Lexical | int, x, =, a, +, b |
| Syntax | Parse tree |
| Semantic | Type checked |
| Intermediate | t1 = a + b |
| Optimization | t1 = a + b |
| Code Generation | Machine instructions |
👉 Frequently asked:
| Phase | Input | Output | Main Function | Errors |
|---|---|---|---|---|
| Lexical Analysis | Source code | Tokens | Break code into tokens | Invalid symbols |
| Syntax Analysis | Tokens | Parse tree | Check grammar | Syntax errors |
| Semantic Analysis | Parse tree | Annotated tree | Check meaning | Type errors |
| Intermediate Code | Annotated tree | IR code | Generate intermediate form | — |
| Code Optimization | IR code | Optimized IR | Improve efficiency | — |
| Code Generation | Optimized IR | Machine code | Final output | — |
A compiler works in multiple phases, each with a specific role
Understanding each phase helps in:
Open this section to load past papers