GATE CS 2024 Set 2 — Question 43

MCQ+2 / -0.67MediumThree-Address CodeIntermediate Code GenerationCompiler Design

Compiler Design → Intermediate Code Generation → Three-Address Code

Last updated

Question

Consider the following expression: x[i]=(p+r)s[i]+u/wx[i] = (p + r) * -s[i] + u/w. The following sequence shows the list of triples representing the given expression, with entries missing for triples (1), (3), and (6).
(0)+pprr
(1)
(2)uminus(1)
(3)
(4)/uuww
(5)+(3)(4)
(6)
(7)=(6)(5)
Which one of the following options fills in the missing entries CORRECTLY?
A.
(1) =[] ss ii (3) * (0) (2) (6) []= xx ii
B.
(1) []= ss ii (3) – (0) (2) (6) =[] xx (5)
C.
(1) =[] ss ii (3) * (0) (2) (6) []= xx (5)
D.
(1) []= ss ii (3) – (0) (2) (6) =[] xx ii

Correct answer

(A) (1) =[] s i (3) (0) (2) (6) []= x i

Solution

The expression is x[i]=(p+r)s[i]+u/wx[i] = (p + r) * -s[i] + u/w.
We can break this down into three-address code (triples):
1.Compute p+rp+r: (0) + p r
2.Access s[i]s[i]: This requires an array load operation. In triples, this is often denoted as =[] s i (load from array ss at index ii). This corresponds to triple (1).
3.Compute s[i]-s[i]: (2) uminus (1)
4.Compute (p+r)s[i](p+r) * -s[i]: This multiplies the result of (0) and (2). So triple (3) is * (0) (2).
5.Compute u/wu/w: (4) / u w
6.Compute the sum: (5) + (3) (4)
7.Assign the result to x[i]x[i]: The assignment x[i]=x[i] = \dots requires an array store operation. The LHS x[i]x[i] is the target. In the given table, triple (7) is = (6) (5), which means "assign the value of (5) to the location defined by (6)". Therefore, triple (6) must define the location x[i]x[i]. The operation for array store access (l-value) is typically []= x i.
Thus:
(1) =[] s i
(3) * (0) (2)
(6) []= x i
This matches Option (A).

More questions on Intermediate Code Generation

Practice GATE CS PYQs with adaptive difficulty

Timed practice, skill tracking, and AI explanations — free to start.

Start practicing free