optional · CSS study notes
Intermediate Code Generation
Definition
Intermediate code is a machine-independent representation generated after semantic analysis, bridging the language-specific front-end and the machine-specific back-end — allows retargeting a compiler to multiple machines without rewriting the front-end.
Key Points — Common Forms
- Three-Address Code (TAC): each statement has at most one operator on the right, e.g.
t1 = b * c;t2 = a + t1 - Quadruples: TAC represented as 4-field records
(op, arg1, arg2, result) - Triples: like quadruples but refer to other triples by position instead of naming temporaries — saves space, but harder to rearrange (moving a triple breaks references)
- Postfix (Reverse Polish) Notation: operators follow operands, e.g.
a b c * +, naturally represents expression evaluation order - Abstract Syntax Trees (AST): also usable as an intermediate form (see Syntax-Directed Translation)
Example
For x = a + b * c:
TAC:
t1 = b * c
t2 = a + t1
x = t2
Quadruple form: (*, b, c, t1), (+, a, t1, t2), (=, t2, -, x)
📌 CSS Frequency: High (2017 repeated) — "generate three-address code / quadruples for the given expression" is a very common numeric question.
Model Answer (short)
"Intermediate code is a machine-independent representation, most commonly three-address code, generated after semantic analysis to separate the language-specific front-end from the machine-specific back-end. It can be represented as quadruples (op, arg1, arg2, result), triples (which save space by referencing other triples positionally), or postfix notation. Using an intermediate representation lets a compiler support multiple source languages or target machines by only changing the corresponding front-end or back-end."
📜 Past-Paper Questions & MCQs (CSS 2016–2026)
2017 · Paper-I · Q8(e) · 5 marks
Q. Write a short note on intermediate code generation.
Ans.
- Intermediate code is a machine-independent representation produced after semantic analysis, between the front end (analysis) and the back end (synthesis).
- Forms: syntax tree or DAG, postfix notation, and three-address code (quadruples and triples), where each instruction has at most one operator.
- Example:
a = b + c * dbecomest1 = c * d,t2 = b + t1,a = t2. - Why use it: portability (one front end can serve many targets), easier machine-independent optimisation, and a cleaner split of work between compiler phases.