Which of the following grammars is/are ambiguous?
- (A)
- (B)
- (C)
- (D)
Which of the following grammars is/are ambiguous?
ANSWER
The ambiguous grammars are (B) and (C).
Official answer: B, CSource: Official answer key, GATE 2026 CS2 (IIT Guwahati)
A grammar is ambiguous if some string in its language has two distinct parse trees (equivalently, two distinct leftmost derivations).
(A) generates . In a string with , the first symbol and the last symbol must come from the same use of , and the string is only when . So every step is forced, and each string has exactly one parse tree. Unambiguous.
(B) fixes no precedence or associativity. For , the root can be with as the right child. It can also be with as the left child. These are two different parse trees for the same string. Ambiguous.
(C) generates . For the string , one derivation is and another is . In the first, the root has children . In the second, the root has children . These are two different parse trees. Ambiguous.
(D) is right-linear and generates . The string can only be derived by applying exactly times and then . There is one parse tree per string. Unambiguous.
The ambiguous grammars are (B) and (C).
KEY IDEA
A grammar is ambiguous if one string has two parse trees. A production that can grow the string from either end, such as , or an operator rule with no precedence or associativity, such as , gives such a string. A grammar that is forced one way, such as or right-linear , cannot be ambiguous.
WATCH OUT FOR
Judging ambiguity from the language rather than the grammar. is a simple regular language, yet grammar (C) for it is ambiguous, while grammar (D) for the same language is not. Another slip is thinking is ambiguous because it has two productions. Having two productions does not make a grammar ambiguous. What matters is whether one string can be derived in two different ways.
From the GATE 2026 CS2 paper. Every question of this paper · Download the full paper · All 9 CFG & PDA questions, with notes