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
Total Unique Visitors