GATE Computer ScienceModerateGATE CS (PYQ pattern)
Which one of the following context-free grammars (start symbol S, terminals a, b) is unambiguous?
- A. S → aS | Sb | ε
- B. S → aS | aSb | ε
- C. S → aS | Sa | b
- D. S → aSb | A; A → aA | ε
Correct answer: D. S → aSb | A; A → aA | ε
Every string it generates has a unique parse tree; each other grammar has a string with two parse trees: "ab" in S → aS | Sb | ε; "aab" in S → aS | aSb | ε; "aba" in S → aS | Sa | b.
More Theory of Computation questions
- Which one of the following context-free grammars (start symbol S, terminals a, b) is ambiguous?
- For the grammar S → SS | SSS | a, how many distinct parse trees does the string aaaaa (five a's) have?
- 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?