Computer Sciences > GATE 2026 SET-2 > Theory of Computation
Which of the following grammars is/are ambiguous?
A
S → aSb | ε
B
E → E + E | E * E | id
C
S → aS | Sa | ε
D
S → aS | ε

Correct : b,c

A grammar is called ambiguous if there is at least one string in the language that can be derived in two or more distinct ways, resulting in different parse trees.
Option A — S → aSb | ε — Unambiguous
This generates strings aⁿbⁿ where n ≥ 0. For any such string, there is exactly one derivation — apply S → aSb exactly n times and then S → ε. No string has two different parse trees. A is unambiguous.
Option B — E → E + E | E * E | id — Ambiguous
This is the classic ambiguous arithmetic grammar. The string id + id * id has two different parse trees depending on which operator is evaluated first — treating it as (id + id) * id or id + (id * id). Since the grammar gives no precedence or associativity rules, both derivations are equally valid. B is ambiguous.
Option C — S → aS | Sa | ε — Ambiguous
Consider the string aa. One derivation is S → aS → aaS → aa (applying aS twice). Another is S → aS → aSa → aεa = aa (mixing aS and Sa). These give different parse trees for the same string. C is ambiguous.
Option D — S → aS | ε — Unambiguous
This generates aⁿ for n ≥ 0. The only way to derive aⁿ is to apply S → aS exactly n times followed by S → ε. There is no room for two different derivations. D is unambiguous.
Correct answer: B and C ✓

Similar Questions

Consider the Deterministic Finite-state Automaton (DFA) A shown below. The DFA runs on the alphabet {0, 1}, and has the set of states {s,p,q,r}, with s being th...
#952 MCQ
Consider the following definition of a lexical token id for an identifier in a programming language, using extended regular expressions: Which one of the fo...
#956 MCQ
Consider the context-free grammar G below: S -> aSb | X X -> aX | Xb | a | b where S and X are non-terminals, and a and b are terminal symbols. The starting non...
#974 MCQ

Related Topics

ambiguous grammar GATE 2026 GATE CS 2026 Set-2 Q29 E→E+E E*E id ambiguous grammar CFG ambiguity GATE theory of computation GATE 2026 context free grammar ambiguous parse tree multiple derivation same string GATE ambiguous grammar MSQ GATE

Unique Visitor Count

Total Unique Visitors

Loading......