Back to the 2026 paper

Module 4: Intermediate Code Generation, Code Improvement (optimization)

20267m

Generate the three-address code for the following expression. Then represent the generated code using quadruple, triple, and indirect-triple representations.
x=a∗(b+c)−d/ex = a * (b + c) - d / e

Worked SolutionAI Assisted

Expression: x=a∗(b+c)−d/ex = a * (b + c) - d / e

Operator precedence and parentheses give the evaluation order: (b+c)(b+c) first, then a∗(… )a * (\dots), then d/ed/e, then the subtraction, then the assignment.

Three-address code

t1 = b + c
t2 = a * t1
t3 = d / e
t4 = t2 - t3
x  = t4

Syntax tree of the expression (each internal node becomes one three-address instruction, bottom-up)

MermaidDiagram
Rendering diagram…

(i) Quadruples (op, arg1, arg2, result)

# op arg1 arg2 result
0 + b c t1
1 * a t1 t2
2 / d e t3
3 - t2 t3 t4
4 = t4 x

(ii) Triples (op, arg1, arg2): there is no result field; a result is referred to by the number of the triple that computed it.

# op arg1 arg2
(0) + b c
(1) * a (0)
(2) / d e
(3) - (1) (2)
(4) = x (3)

(iii) Indirect triples: the triples are the same as above, but a separate instruction list holds pointers to them. The order of execution can then be changed by reordering the pointers without touching the triples.

Instruction list Points to
100 (0)
101 (1)
102 (2)
103 (3)
104 (4)
Triple # op arg1 arg2
(0) + b c
(1) * a (0)
(2) / d e
(3) - (1) (2)
(4) = x (3)

Comparison

Form Temporaries in symbol table Easy to reorder for optimization
Quadruples Yes (explicit result names) Yes
Triples No No (numbers refer to positions)
Indirect triples No Yes (reorder the pointer list)

Similar questions

AI LearningWeb Platform

Coursify — Personal AI Course Generator

Struggling with a difficult subject or concept? Turn any engineering topic into a structured course with interactive lessons, quizzes, and research-backed explanations.