Topic: Compiler Design / Parsing
The dangling-else grammar stmt -> if expr then stmt | if expr then stmt else stmt | other is
- A. unambiguous
- B. not context-free
- C. ambiguous
- D. regular
Answer: C. ambiguous
Explanation: The classic "dangling-else" grammar is inherently ambiguous. An input string like `if E1 then if E2 then S1 else S2` has two distinct, valid parse trees. The `else S2` can be incorrectly attached to the outer `if E1` or correctly attached to the inner `if E2`. Because a single string can generate more than one parse tree, the grammar is formally ambiguous.