Which of the following grammars is/are ambiguous?
Topic-wise GATE CS PYQs with verified steps
GATE CSE 2026 Set 2 · Q29
Time
Select all that applyOne or more correct
Solution
Official answer: B) E → E + E E * E id; C) S → aS Sa
Context-Free Grammars and PDAsA 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): This grammar generates the language . Every string of length has a unique derivation sequence, so it is unambiguous.
Option (B): This is the classic ambiguous arithmetic expression grammar. For example, the string can be parsed as either or , resulting in two different parse trees. Hence, it is ambiguous.
Option (C): This grammar generates . Consider the string :
Since there are two distinct leftmost derivations for the string , the grammar is ambiguous.
Option (D): This grammar generates and is regular/right-linear. Each string has a unique derivation, so it is unambiguous.
Therefore, the ambiguous grammars are B and C.
Try the question first, then check your work.
Examiner trap
Check if any single string has more than one leftmost derivation or parse tree (for example, test short strings like or ).
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
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.
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 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 PDAsIndependent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.