GATE Computer ScienceVery hardGATE CS (PYQ pattern)
For the grammar S → SS | SSS | a, how many distinct parse trees does the string aaaaa (five a's) have?
- A. 14
- B. 42
- C. 38
- D. 45
Correct answer: C. 38
Let t(n) be the number of trees for aⁿ: t(1) = 1, and the root either splits aⁿ into two nonempty parts (SS) or three (SSS). t(2) = 1, t(3) = 2 + 1 = 3, t(4) = (t1t3 + t2t2 + t3t1) + (3 ways of 1+1+2) = 7 + 3 = 10, t(5) = (t1t4 + t2t3 + t3t2 + t4t1) + Σ over compositions into 3 parts = (10 + 3 + 3 + 10) + (3·t3 + 3·t2·t2·1) = 26 + 12 = 38. (14 is the S → SS | a Catalan count.)
More Theory of Computation questions
- For the grammar S → aS | Sb | aSb | c, how many distinct parse trees does the string aacbb have?
- For the grammar S → SaS | SS | b, how many distinct parse trees does the string bbabb have?
- For the grammar E → E + E | E E | a (no precedence or associativity rules), the string a+aa+aa has 14 parse trees. In how many of them is…
- For the grammar S → SS | aSb | ab, how many distinct parse trees does the string ababab have?
- The grammar S → aSb | bSa | SS | ε generates how many strings of length exactly 8?
- The grammar S → aSbS | ε generates 42 strings of length 10. In how many of them does no prefix contain more than two a's in excess of b's…