CSS Prepare

optional · CSS study notes

Phases of a Compiler

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
  • Symbol Table Management and Error Handling run throughout all phases, not as separate sequential steps.
  • Front-end = phases 1–3 (analysis); Back-end = phases 4–6 (synthesis).

Example

For x = a + b * 2:

  • Lexer: id(x) = id(a) + id(b) * num(2)
  • Parser: builds expression tree respecting precedence
  • Semantic: checks a, b, x are declared & type-compatible
  • Intermediate: t1 = b*2; t2 = a+t1; x = t2

📌 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."

📜 Past-Paper Questions & MCQs (CSS 2016–2026)

2018 · Paper-I · Q8(a) · 5 marks

Q. What is the difference between lexers and parsers?

Ans.

BasisLexer (scanner)Parser
PhaseFirst phase (lexical analysis)Second phase (syntax analysis)
InputStream of charactersStream of tokens
OutputTokens (identifier, number, operator …)Parse tree or syntax tree
Specified byRegular expressions, finite automataContext-free grammar
Other workRemoves whitespace and comments, builds the symbol tableChecks structure and reports syntax errors
Errors foundIllegal characters, malformed tokensMissing semicolon or bracket, wrong statement order

MCQs

2026 P-I · Q15. A compiler needs to recognise tokens. Which phase does this?

  • (A) Syntax Analysis
  • (B) Lexical Analysis ✅
  • (C) Code Generation
  • (D) None of these
  • Answer: (B) Lexical Analysis.
  • Why: the lexical analyser (scanner) reads the characters and groups them into tokens (identifiers, numbers, operators). Syntax analysis then checks the order of those tokens, and code generation comes much later.

2025 P-I · Q20. Which phase of the compiler is responsible for abstract syntax tree (AST) generation?

  • (A) Syntax analysis ✅
  • (B) Semantic analysis
  • (C) Lexical analysis
  • (D) None of these
  • Answer: (A) Syntax analysis.
  • Why: the parser builds the parse tree or AST from the token stream. Semantic analysis later decorates the tree with types and checks meaning, and lexical analysis only produces tokens.

2025 P-I · Q13. What is the role of a parser?

  • (A) To check logical errors
  • (B) To find syntax errors ✅
  • (C) To identify run-time errors
  • (D) None of these
  • Answer: (B) To find syntax errors.
  • Why: the parser checks that the tokens follow the grammar of the language and reports syntax errors. Logical errors and run-time errors are not found by a compiler's parser.