GATE Computer ScienceHardGATE CS (PYQ pattern)

The statement r = (a + b) * (b + a) − (a + b) / c is turned into a DAG (+ and * commutative, − and / not), and three-address code is generated with one instruction t = x op y per interior node followed by a separate copy r = t for the assignment. How many nodes (leaves plus interior) does the DAG have, and how many instructions are generated, counting the copy?

  1. A. 8 nodes, 6 instructions
  2. B. 13 nodes, 7 instructions
  3. C. 7 nodes, 5 instructions
  4. D. 6 nodes, 5 instructions

Correct answer: C. 7 nodes, 5 instructions

Leaves a, b, c (3). a + b and b + a are one node because + is commutative; then (a+b)*(a+b), (a+b)/c and the final −: 4 interior nodes, 7 nodes in all. Code: t1 = a + b; t2 = t1 * t1; t3 = t1 / c; t4 = t2 − t3; r = t4: 5 instructions. Without commutativity there would be 8 nodes and 6 instructions; the expression tree has 13 nodes.

More Compiler Design questions

Exams that ask this topic