Definition

Compilation happens in six logical phases, front-end (language-dependent) followed by back-end (machine-dependent), with the symbol table and error handler interacting across all phases.

Key Points (in order)

  1. Lexical Analysis — source code → stream of tokens (removes whitespace/comments)
  2. Syntax Analysis — tokens → parse tree, checked against grammar rules
  3. Semantic Analysis — parse tree checked for meaning (type checking, scope rules) → annotated tree
  4. Intermediate Code Generation — produces machine-independent code (e.g., three-address code)
  5. Code Optimization — improves intermediate code for speed/space, without changing meaning
  6. Code Generation — intermediate code → target machine code

Example

For x = a + b * 2:

📌 CSS Frequency: High — diagram/labeling question almost every attempt; sometimes asked to trace one phase with an example expression.

Model Answer (short)

"A compiler works in six phases: lexical analysis (tokenization), syntax analysis (parsing), semantic analysis (type/scope checking), intermediate code generation, code optimization, and code generation. The symbol table and error handler support every phase. The first three phases form the analysis (front-end); the last three form the synthesis (back-end), which allows retargeting to different machines by only rewriting the back-end."