Introduction · Compiler Design

Phases of Compilation and Overview

Watch lectures

Phases of Compilation and Overview

1. What a Compiler Does

A compiler translates a high-level source program (C, Java) into an equivalent low-level target program (assembly, machine code), reporting errors. Difference from interpreter: compiler translates whole program before execution; interpreter translates line-by-line during execution.

2. The 6 Phases (exam list — must draw block diagram)

source → [1 Lexical] → tokens → [2 Syntax] → parse tree → [3 Semantic] → annotated tree → [4 IR Gen] → IR → [5 Optimize] → improved IR → [6 Code Gen] → target
              ↓              ↓                    ↓
        Symbol Table Manager  +  Error Handler (all phases)
Phase Input → Output Core job / Example
1. Lexical Analysis characters → tokens removes whitespace/comments; x = a+b → id(x) = id(a) + id(b)
2. Syntax Analysis tokens → parse tree checks grammar; a+-b rejected
3. Semantic Analysis tree → annotated tree type checking; int + float → type coercion, undeclared variable error
4. Intermediate Code Gen tree → 3-address code t1 = a+b; x = t1
5. Code Optimization IR → better IR x = 2*3 → x = 6 (constant folding)
6. Target Code Gen IR → assembly LD R1,a; ADD R1,b; ST x,R1

3. Symbol Table + Error Handler

  • Symbol table: stores name, type, scope, address for every identifier; consulted in all phases.
  • Error handler: lexical (illegal character), syntax (missing ;), semantic (type mismatch) errors.

4. Worked Example — position = initial + rate * 60

lexical: id(position) = id(initial) + id(rate) * 60
syntax:   parse tree with * lower than +
semantic: rate is float → 60 converted to float
IR:       t1 = rate * 60.0; t2 = initial + t1; position = t2
optimize: (nothing to fold here)
codegen:  LDF R1,rate; MULF R1,#60.0; ADDF R1,initial; STF position,R1

5. Exam Points

  • Draw and explain all 6 phases with diagram — 7-10 marks, most frequent question in Module 1.
  • Difference compiler vs interpreter vs assembler — 3-5 marks, frequent.
  • What is symbol table, error handler role — 3 marks, frequent.
Official AppAndroid App

PYQDeck for Android

Solve BEU previous-year exam papers anywhere, anytime. Fast search, offline paper viewing, and step-by-step solutions designed specifically for your phone.

← All Compiler Design modules