GATE Computer ScienceVery hardGATE CS (PYQ pattern)

The statement r = ((x * y) + (y * x)) * ((x * y) − z) + (z − x * y) is turned into a DAG (+ and * commutative, − not), and code is generated with one instruction per interior node plus a separate copy r = t at the end. How many DAG nodes (leaves plus interior) are there, and how many instructions, counting the copy?

  1. A. 10 nodes, 8 instructions
  2. B. 19 nodes, 10 instructions
  3. C. 8 nodes, 7 instructions
  4. D. 9 nodes, 7 instructions

Correct answer: D. 9 nodes, 7 instructions

Leaves x, y, z. Interior: x*y (y*x is the same node), x*y + x*y (both children the same node), x*y − z, the product of those two, z − x*y, and the final +: 6 interior, 9 nodes. Instructions: 6 + the copy = 7. Without commutativity y*x is a separate node: 10 nodes, 8 instructions; the plain tree has 19 nodes.

More Compiler Design questions

Exams that ask this topic