LL grammar for the language L = { an bm cn+m | m ≥ 0, n ≥ 0 } is
2013
LL grammar for the language
L = { an bm cn+m | m ≥ 0, n ≥ 0 } is
Answer: A. S → aSc | S1 S1 → bS1c | λ — Concept: A top-down (LL) parser must pick a production for a nonterminal using only the next input symbol, so a grammar can be an LL(1) grammar only if, at…
- A.
S → aSc | S1
S1 → bS1c | λ - B.
S → aSc | S1 | λ
S1 → bS1c - C.
S → aSc | S1 | λ
S1 → bS1c | λ - D.
S → aSc | λ
S1 → bS1c | λ - E.
Both of the following are correct LL grammars for L:
S → aSc | S1 ; S1 → bS1c | λ
AND
S → aSc | S1 | λ ; S1 → bS1c | λ
Attempted by 129 students.
Show answer & explanation
Correct answer: A
Concept: A top-down (LL) parser must pick a production for a nonterminal using only the next input symbol, so a grammar can be an LL(1) grammar only if, at every nonterminal, no two alternatives can be triggered by the same lookahead symbol — and in particular, at most one alternative of a nonterminal may derive the empty string λ, since two alternatives that both reach λ create exactly such a clash.
For a concatenation language like { an bm cn+m }, the standard construction pairs the outer a/c layer with its own recursive nonterminal and hands off to a second nonterminal that generates the inner b/c layer down to λ, keeping the empty-string case reachable through exactly one path.
Application:
Define S to generate the outer layer: S → aSc | S1. Peeling the aSc alternative n times gives an S1 cn.
Define S1 to generate the inner layer: S1 → bS1c | λ. Peeling this m times gives bm cm.
Substitute S1 = bm cm into an S1 cn: the result is an bm cm cn = an bm cn+m, matching L for every n, m ≥ 0 (including n = 0, or m = 0, or both).
Check the lookahead sets at S: SELECT(S → aSc) = {a}; SELECT(S → S1) = {b} ∪ FOLLOW(S) = {b, c, $} (FOLLOW(S) includes c from the recursive aSc wrapping, plus end of input). These two sets are disjoint, so the choice at S is deterministic.
Check the lookahead sets at S1: SELECT(S1 → bS1c) = {b}; SELECT(S1 → λ) = FOLLOW(S1) = {c, $}. These are disjoint too, so S1 is deterministic and the whole grammar S → aSc | S1, S1 → bS1c | λ is a valid LL(1) grammar.
Cross-check: Run the same two tests on each of the four base grammars offered — does the grammar generate exactly L, and does S have two ways to reach λ on the same lookahead?
Grammar | Generates exactly L? | Empty-string paths at S |
|---|---|---|
S → aSc | S1 ; S1 → bS1c | λ | Yes | Reaches λ only via S → S1 → λ (one path); no lookahead clash, so this grammar is LL(1). |
S → aSc | S1 | λ ; S1 → bS1c | No | S1 never derives λ, so a string such as "bc" (one b, no a) cannot be produced. |
S → aSc | S1 | λ ; S1 → bS1c | λ | Yes | Reaches λ via S → S1 → λ AND directly via S → λ — two paths on the same lookahead set {c, $}, a lookahead clash, so this grammar is not LL(1) even though it generates L. |
S → aSc | λ ; S1 → bS1c | λ | No | No production of S ever hands off to S1, so no string containing a b can be produced from S at all. |
Result: So exactly one of the four listed grammars is both a correct generator of L and a valid LL(1) grammar — the one with S → aSc | S1, S1 → bS1c | λ. The grammar that additionally gives S a direct λ generates the same language but fails the LL(1) lookahead check, so the option asserting that both of those grammars are valid LL grammars does not hold up.
(Some circulated answer keys for this item have listed the second grammar as acceptable alongside the first; that pairing does not survive the LL(1) lookahead check worked out above, since it introduces two derivations of λ at S on the same lookahead set.)
A video solution is available for this question — log in and enroll to watch it.