GATE Computer ScienceHardGATE CS (PYQ pattern)

SDD: S → ( S S ) | a. S.d is inherited: root d = 0, children get parent.d + 1. For a → a: S.v = S.d if S.d is even, otherwise −S.d. For S → ( S1 S2 ): S.v = S1.v + 2 × S2.v. What is S.v at the root for the input "( ( a a ) ( a ( a a ) ) )"?

  1. A. -23
  2. B. -28
  3. C. -20
  4. D. -26

Correct answer: D. -26

Evaluate depth-first: leaves at odd depth give −d, at even depth +d, and right subtrees are weighted by 2; the root value is -26.

More Compiler Design questions

Exams that ask this topic