GATE Computer ScienceHardGATE CS (PYQ pattern)

An SDD uses N → B N1 { N.val = B.val + 2 × N1.val } | B { N.val = B.val } and B → 0 { B.val = 0 } | 1 { B.val = 1 }. What is N.val at the root for the input "1 1 0 1", and what is it for "1 0 1 1"?

  1. A. 11 and 13
  2. B. 13 and 11
  3. C. 13 and 13
  4. D. 11 and 11

Correct answer: A. 11 and 13

The right-recursive rule weights the leftmost bit by 1 and each later bit by twice the previous weight, so the string is read least-significant bit first. 1 1 0 1 → 1 + 2(1 + 2(0 + 2·1)) = 11, and 1 0 1 1 → 1 + 2(0 + 2(1 + 2·1)) = 13. (Reading them as ordinary binary gives 13 and 11, the swapped answer.)

More Compiler Design questions

Exams that ask this topic