CD Notes
Comprehensive guide to compiler architecture, phases, and how source code is transformed into executable code
A compiler is a sophisticated software system that translates human-readable source code into machine-executable code. Understanding how compilers work is fundamental to computer science and helps programmers write better, more efficient code.
What is a Compiler?
A compiler is a computer program that translates source code written in one language (source language) into equivalent code in another language (target language). The most common scenario is translating high-level programming languages into machine code or assembly language.
The Classical Compiler Architecture
Front-End (Analysis Phase)
The front-end analyzes the source code and builds an intermediate representation:
Back-End (Synthesis Phase)
The back-end takes the intermediate representation and generates target code:
Compilation Phases in Detail
Phase 1: Lexical Analysis (Scanning)
Breaks source code into tokens.
Input: int x = 10; Output (Tokens): [INT] [IDENTIFIER:x] [ASSIGN] [NUMBER:10] [SEMICOLON]
Phase 2: Syntax Analysis (Parsing)
Checks if tokens follow grammar rules and builds parse tree.
Input Tokens: [INT] [IDENTIFIER:x] [ASSIGN] [NUMBER:10]
Phase 3: Semantic Analysis
Checks meaning and consistency (type compatibility, variable declaration before use).
Validations:
- Is
xdeclared before use? - Can we assign integer 10 to integer variable x?
- Are all references valid?
Phase 4: Intermediate Code Generation
Generates low-level representation.
Example (Three-Address Code):
Phase 5: Code Optimization
Removes redundant operations, improves efficiency.
Phase 6: Code Generation
Creates machine/assembly code.
Output Example:
Complete Compiler Flow Diagram
Symbol Table
The symbol table is a crucial data structure that stores information about identifiers:
| Symbol | Type | Scope | Address |
|---|---|---|---|
| x | int | local | 0x1000 |
| y | float | local | 0x1004 |
| func | function | global | 0x2000 |
Error Handling
Compilers must detect and report various errors:
| ✗ Syntax Errors | Invalid grammar |
| Example | int x =; (missing value) |
| ✗ Semantic Errors | Invalid meaning |
| Example | int x = "hello"; (type mismatch) |
| ✗ Type Errors | Type incompatibility |
| Example | int x = 3.14; (float to int) |
| ✗ Undeclared Variable | Used before declaration |
| Example | x = 10; (x not declared) |
| ✗ Redeclaration | Multiple declarations |
| Example | int x; int x; (duplicate declaration) |
Types of Compilers
| Type | Purpose | Example |
|---|---|---|
| One-pass | Fast, less optimization | Early Pascal compilers |
| Multi-pass | Better optimization | Modern C, C++ compilers |
| Just-In-Time (JIT) | Runtime compilation | Java, JavaScript engines |
| Cross-compiler | Compiles for different architecture | ARM compiler on x86 |
| Incremental | Recompiles changes only | IDE compilers |
Interview Q&A
Q: What is the difference between a compiler and interpreter? A: A compiler translates entire source code to machine code before execution. An interpreter executes source code line-by-line, translating and executing simultaneously.
Q: What phases must every compiler have? A: Every compiler must have lexical analysis, syntax analysis, and code generation. Semantic analysis and optimization are common but optional.
Q: Why do compilers use intermediate representations? A: IRs make the compiler modular and portable. Front-end targets one IR for multiple source languages; back-end generates code for multiple targets from the same IR.
Q: What is a symbol table? A: A symbol table is a data structure storing information about identifiers (variables, functions) like their names, types, scopes, and memory addresses.
Q: What are the main sources of errors detected by compilers? A: Lexical errors (invalid tokens), syntax errors (grammar violations), semantic errors (type mismatches, undeclared variables), and logical errors (detected at runtime).
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Compiler Overview.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Compiler Design topic.
Search Terms
compiler-design, compiler design, compiler, design, language, processing, system, overview
Related Compiler Design Topics