Given the following grammars: πΊ1πβπ΄π΅β£πππ΅π΄βππ΄β£ππ΅βππ΅β£ππΊ2:πβπ΄β£π΅β¦
2015
Given the following grammars:
πΊ1πβπ΄π΅β£πππ΅π΄βππ΄β£ππ΅βππ΅β£ππΊ2:πβπ΄β£π΅π΄βππ΄πβ£πππ΅βπππ΅β£πWhich of the following is correct ?
- A.
πΊ1Β is ambiguous andΒ πΊ2Β is unambiguous grammars
- B.
πΊ1Β is unambiguous andΒ πΊ2Β is ambiguous grammars
- C.
bothΒ πΊ1Β andΒ πΊ2Β areΒ ambiguous grammars
- D.
bothΒ πΊ1Β andΒ πΊ2Β are unambiguous grammars
Attempted by 99 students.
Show answer & explanation
Correct answer: C
Conclusion: Both grammars are ambiguous.
First grammar (productions: S β A B | a a B; A β a A | Ξ΅; B β b B | Ξ΅) generates a* b*. It is ambiguous because some strings have two different parse trees. Example: the string "aa".
Derivation 1: S β A B, with A β a a and B β Ξ΅, yields "aa".
Derivation 2: S β a a B, with B β Ξ΅, yields "aa".
Second grammar (productions: S β A | B; A β a A b | a b; B β a b B | Ξ΅) generates the union of {a^n b^n | n β₯ 1} and (ab)*. It is ambiguous because the string "ab" can be derived in two different ways.
Derivation 1: S β A β a b, yielding "ab".
Derivation 2: S β B β a b B β a b (B β Ξ΅), yielding "ab".
Therefore, because each grammar admits at least one string with two distinct parse trees, both grammars are ambiguous.