Which of the following grammars is/are ambiguous?
GATE CSE 2026Compiler Design › Grammars & ParsingVerified
GATE CSE 2026 PYQ: Which of the following grammars is/are ambiguous?
- A
- BCorrect answer
- CCorrect answer
- D
Answer
(B, C) E → E + E ∣ E ∗ E ∣ i d E \rightarrow E+E|E * E| i d E → E + E ∣ E ∗ E ∣ i d; S → a S ∣ S a ∣ ϵ S \rightarrow a S|S a| \epsilon S → a S ∣ S a ∣ ϵ
Approach
A grammar is ambiguous iff there exists at least one string that has two distinct parse trees (equivalently, two distinct leftmost derivations). Check each grammar for such a string.
Only grammars B and C produce at least one string with multiple parse trees. A and D are unambiguous.
Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.