Skip to main content

GATE CSE 2026Compiler Design › Grammars & ParsingVerified

GATE CSE 2026 PYQ: Which of the following grammars is/are ambiguous?

Which of the following grammars is/are ambiguous?

  1. A
    S→aSb∣ϵS \rightarrow a S b \mid \epsilon
  2. B
    E→E+E∣E∗E∣idE \rightarrow E+E|E * E| i d
    Correct answer
  3. C
    S→aS∣Sa∣ϵS \rightarrow a S|S a| \epsilon
    Correct answer
  4. D
    S→aS∣ϵS \rightarrow a S \mid \epsilon

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.