Question

Which of the following grammars is/are ambiguous?

Choose every correct option
Official answer and solution
Official answer
B and C
From
Official GATE 2026 answer key

Solution

Check whether each grammar admits two distinct parse trees (or leftmost derivations) for some string.

  • (A) S→aSb∣ϵS \rightarrow aSb \mid \epsilon generates {anbn∣n≥0}\{a^n b^n \mid n \ge 0\} uniquely; it is unambiguous.
  • (B) E→E+E∣E∗E∣idE \rightarrow E + E \mid E * E \mid id is the classic ambiguous expression grammar (e.g., id+id+idid + id + id and id+id∗idid + id * id each have two parse trees).
  • (C) S→aS∣Sa∣ϵS \rightarrow aS \mid Sa \mid \epsilon is ambiguous because the string aa has two distinct leftmost derivations: S⇒aS⇒aS \Rightarrow aS \Rightarrow a and S⇒Sa⇒aS \Rightarrow Sa \Rightarrow a.
  • (D) S→aS∣ϵS \rightarrow aS \mid \epsilon is a right-linear deterministic grammar generating a∗a^* unambiguously.

Thus (B) and (C) are ambiguous.

Final answer

Options (B), (C)

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2026 Computer Science and Information Technology - Session 2 (CS2)
Question
29 of 65
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.