Which of the following grammars is/are ambiguous?
View worked solution
Answer: B;C
A grammar is ambiguous if there exists at least one string in its language that has more than one leftmost derivation (or equivalently, more than one distinct parse tree).
Let's analyze each option:
- Option A:
This grammar generates the language . For any string , there is exactly one unique derivation sequence (applying exactly times followed by ). Since every string in the language has a unique parse tree, this grammar is unambiguous. - Option B:
This is the classic expression grammar. For the string , we can construct two distinct parse trees representing different operator precedences:- One where is evaluated first:
- One where is evaluated first:
- Option C:
This grammar generates the language . Consider the string . We can derive it in two distinct ways:- Derivation 1:
- Derivation 2:
- Option D:
This grammar generates . For any string , we must apply exactly times and then terminate with . There is no other way to derive any string, so this grammar is unambiguous.
Therefore, the ambiguous grammars are B and C.
Free practice · Your answers on this page are not saved.
