GATE Computer ScienceVery hardGATE CS (PYQ pattern)

Sethi–Ullman labelling: a leaf is 1 as a left child and 0 as a right child (a right operand may be a memory operand); an interior node is max(l1, l2) if l1 ≠ l2, else l1 + 1. How many registers does a * (b + (c * (d + e))) need as written, and how many if the compiler may swap the operands of + and * (both commutative) to minimise the label?

  1. A. 3 as written, 2 after swapping
  2. B. 2 as written, 2 after swapping
  3. C. 3 as written, 1 after swapping
  4. D. 2 as written, 1 after swapping

Correct answer: D. 2 as written, 1 after swapping

As written: d + e = 1; c * (d + e) has c = 1 and 1 → 2; b + 2 → 2; a * 2 → 2. Swapping every operator turns the tree into the left-deep chain (((d + e) * c) + b) * a, where every right child is a memory leaf (0) and every node is max(1, 0) = 1: a single register suffices.

More Compiler Design questions

Exams that ask this topic