Which of the following grammars are ambiguous?
2026
Which of the following grammars are ambiguous?
- A.
š ā ššš | epsilon
- B.
Eā šø + šø | šø ā šø | šš
- C.
Sā šš | šš | epsilon
- D.
S ā šš | epsilon
Attempted by 19 students.
Show answer & explanation
Correct answer: B, C
Analysis of Grammar Ambiguity
A grammar is considered ambiguous if there exists at least one string that has more than one leftmost derivation or parse tree.
Option A generates strings of the form an bn. Each string has a unique derivation sequence, making this grammar unambiguous.
Option B allows multiple parse trees for expressions like id + id * id due to missing operator precedence rules. This creates ambiguity.
Option C allows derivation of strings like 'aa' via left recursion (S -> aS -> aa) or right recursion (S -> Sa -> aa). This results in ambiguity.
Option D generates strings of the form an. Each string length corresponds to a unique number of recursive steps, ensuring a single derivation tree.
Conclusion: Options B and C are ambiguous grammars.
A video solution is available for this question ā log in and enroll to watch it.