Consider the grammar with non-terminals , terminals , with as the start symbol, and the following set of rules:
The grammar is NOT LL(1) because:
View worked solution
Answer: C
The given grammar represents the classic dangling-else problem in programming languages, where:
- stands for
if - stands for
condition - stands for
then - stands for
else
An LL(1) grammar must be unambiguous. Let's analyze why this grammar is not LL(1):
1. Ambiguity Analysis
Consider the string: (which represents if b then if b then a else a).
This string has two distinct leftmost derivations (and thus two different parse trees):
Derivation 1 (associating the else with the inner if):
Derivation 2 (associating the else with the outer if):
Since the grammar is ambiguous, it cannot be parsed by any LL(1) parser.
2. Elimination of Other Options
- Option A: There is no left-recursion in the grammar (no production of the form ).
- Option B: Right-recursion (e.g., ) is perfectly fine and does not prevent a grammar from being LL(1).
- Option D: The grammar is context-free because every production has a single non-terminal on its left-hand side.
Hence, the correct option is C.
Free practice · Your answers on this page are not saved.
