Phases of a Compiler
A compiler is a program that translates high-level source code into machine code. It works in multiple phases to perform lexical analysis, syntax checking, optimization, and code generation. The compilation process is generally divided into two major parts:
- Analysis Phase (Front-End)
- Synthesis Phase (Back-End)
Phases of a Compiler
1. Lexical Analysis (Scanner)
- Converts source code into a stream of tokens.
- Removes whitespace and comments.
- Identifies keywords, identifiers, operators, literals.
- Example:
Tokenized as:
int
(keyword)x
(identifier)=
(assignment operator)10
(integer literal);
(delimiter)
2. Syntax Analysis (Parser)
- Checks whether the token sequence follows the grammatical rules of the programming language.
- Constructs a parse tree (syntax tree).
- Detects syntax errors.
- Example:
int x = 10;
✅ (Valid)int = x 10;
❌ (Syntax Error)
3. Semantic Analysis
- Ensures meaningful and valid operations.
- Checks for:
- Type mismatches (e.g.,
int x = "hello";
❌) - Undeclared variables
- Function signature mismatches
- Type mismatches (e.g.,
- Example:
4. Intermediate Code Generation
- Converts the syntax tree into an intermediate representation (IR).
- IR is independent of machine architecture.
- Example (Three-Address Code):
Translates to:
5. Optimization
- Improves performance by reducing execution time and memory usage.
- Types:
- Peephole Optimization (small local optimizations)
- Loop Optimization (reducing redundant calculations in loops)
- Constant Folding (
3 * 4
→12
at compile time)
- Example:
6. Code Generation
- Converts IR into assembly/machine code.
- Allocates registers, memory, and instructions for the target processor.
- Example:
7. Symbol Table Management
- Maintains a record of variable names, types, scopes, and memory locations.
- Used in semantic analysis, optimization, and code generation.
8. Error Handling
- Detects and reports lexical, syntax, and semantic errors.
- Example Errors:
- Lexical Error:
inta x = 5;
(invalid tokeninta
) - Syntax Error:
int x 5;
(missing=
) - Semantic Error:
int x = "abc";
(type mismatch)
- Lexical Error:
Summary of Compiler Phases
Phase | Purpose |
---|---|
Lexical Analysis | Convert source code to tokens |
Syntax Analysis | Check syntax correctness (parse tree) |
Semantic Analysis | Ensure meaningful expressions (type checking) |
Intermediate Code Generation | Convert to intermediate representation (IR) |
Optimization | Improve performance |
Code Generation | Produce machine/assembly code |
Symbol Table Management | Store variable & function details |
Error Handling | Detect and report errors |
Comments
Post a Comment