GateAI
Context-Free Grammars and PDAs

GATE CSE 2026 Set 2 · Q29

MSQ1 markEasy

Time

Which of the following grammars is/are ambiguous?

Select all that applyOne or more correct

Pick every correct option

Solution

Step-by-step verified solution

Official answer: B) E → E + E E * E id; C) S → aS Sa

Context-Free Grammars and PDAs

A context-free grammar is ambiguous if there exists at least one string generated by the grammar that has more than one distinct leftmost derivation (or more than one parse tree).

  • Option (A): S→aSb∣ϵS \rightarrow aSb \mid \epsilon This grammar generates the language L={anbn∣n≥0}L = \{a^n b^n \mid n \ge 0\}. Every string of length 2n2n has a unique derivation sequence, so it is unambiguous.

  • Option (B): E→E+E∣E∗E∣idE \rightarrow E + E \mid E * E \mid \text{id} This is the classic ambiguous arithmetic expression grammar. For example, the string id+id∗id\text{id} + \text{id} * \text{id} can be parsed as either (id+id)∗id(\text{id} + \text{id}) * \text{id} or id+(id∗id)\text{id} + (\text{id} * \text{id}), resulting in two different parse trees. Hence, it is ambiguous.

  • Option (C): S→aS∣Sa∣ϵS \rightarrow aS \mid Sa \mid \epsilon This grammar generates L=a∗L = a^*. Consider the string w=aw = a:

    1. S⇒aS⇒aϵ=aS \Rightarrow aS \Rightarrow a\epsilon = a

    2. S⇒Sa⇒ϵa=aS \Rightarrow Sa \Rightarrow \epsilon a = a

    Since there are two distinct leftmost derivations for the string aa, the grammar is ambiguous.

  • Option (D): S→aS∣ϵS \rightarrow aS \mid \epsilon This grammar generates L=a∗L = a^* and is regular/right-linear. Each string ana^n has a unique derivation, so it is unambiguous.

Therefore, the ambiguous grammars are B and C.

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Context-Free Grammars and PDAs previous year question is Q29 of the GATE CSE 2026 Set 2 paper. Find more GATE CSE PYQs on Context-Free Grammars and PDAs in Theory of Computation, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Context-Free Grammars and PDAs PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Context-Free Grammars and PDAs in GATE CSE

Context-Free Grammars and PDAs has 26 GATE CSE PYQs across 17 papers from 2011 to 2026. 2 of them are 1-mark MSQs like this one, and GATE CSE 2026 Set 2 has 2.

How GATE tests Context-Free Grammars and PDAs

More from GATE CSE 2026 Set 2

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2026 Set 2 · Q29 · MSQ

Which of the following grammars is/are ambiguous?

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.