Proving That a Grammar Is LL(1) but Not SLR(1)
Consider the grammar
where and are terminals, is the start symbol, and denotes the empty string.
This section proves two different properties:
- The grammar is LL(1) because one input symbol is sufficient to choose every production in a predictive top-down parser.
- The grammar is not SLR(1) because its SLR parsing table contains a reduce–reduce conflict.
An LL(1) table is constructed using FIRST and FOLLOW sets. A grammar is LL(1) when no table cell receives more than one production. SLR(1), in contrast, constructs LR(0) item sets and places reductions according to FOLLOW sets; overlapping reductions create conflicts.
Footnotes
-
Top-Down Parsing - Describes FIRST/FOLLOW-based predictive parsing tables and LL(1) conditions. ↩
-
LR Parsing and SLR(1) Parsing - Defines LR(0) items, SLR reductions using FOLLOW sets, and shift–reduce/reduce–reduce conflict conditions. ↩
Key distinction
LL(1) and SLR(1) use different decision mechanisms. LL(1) chooses productions from the current nonterminal and next input token; SLR(1) chooses shift or reduce actions from LR(0) item states and FOLLOW sets.
Language generated by the grammar
Because both and derive ,
Therefore,
and
Thus,
The two alternatives begin with different terminals:
- begins with , because disappears and the next symbol is .
- begins with , because disappears and the next symbol is .
This separation is the central reason the grammar is LL(1).
Computing FIRST and FOLLOW Sets
- 1Step 1
Since both nonterminals derive epsilon, FIRST(A) = { ε } and FIRST(B) = { ε }.
- 2Step 2
A can disappear, so the first visible symbol is terminal a. Therefore FIRST(AaAb) = { a }.
- 3Step 3
B can disappear, so the first visible symbol is terminal b. Therefore FIRST(BbBa) = { b }.
- 4Step 4
The alternatives have disjoint FIRST sets: FIRST(S) = { a, b }.
- 5Step 5
Because S is the start symbol, the end-marker belongs to FOLLOW(S). Thus FOLLOW(S) = { }.
- 6Step 6
In AaAb, the first A is followed by terminal a, and the second A is followed by terminal b. Hence FOLLOW(A) = { a, b }.
- 7Step 7
In BbBa, the first B is followed by terminal b, and the second B is followed by terminal a. Hence FOLLOW(B) = { a, b }.
FIRST and FOLLOW summary
| Nonterminal or production | FIRST | FOLLOW |
|---|---|---|
| \{\}$ | ||
| — | ||
| — |
The two alternatives for satisfy
Although both alternatives contain nullable nonterminals, neither alternative itself derives :
Consequently, there is no FIRST/FIRST conflict and no FIRST/FOLLOW conflict for .
Constructing the LL(1) parsing table
For a production , place the production in for each . If , place it under every symbol in .
The terminals are , , and $$$.
| Nonterminal | $$$ | ||
|---|---|---|---|
| error | |||
| error | |||
| error |
Every populated table cell contains exactly one production. Therefore, the grammar is LL(1).
Formally,
and the nullable productions for and are selected uniquely using their FOLLOW sets. Hence,
Footnotes
-
Top-Down Parsing - Describes FIRST/FOLLOW-based predictive parsing tables and LL(1) conditions. ↩
Testing the LL(1) Parser
- 1Step 1
The first input symbol is a. The table selects S → AaAb. Then A → ε is selected because a ∈ FOLLOW(A), the terminal a is matched, the second A → ε is selected because b ∈ FOLLOW(A), and finally b is matched.
- 2Step 2
The first input symbol is b. The table selects S → BbBa. Then B → ε is selected because b ∈ FOLLOW(B), the terminal b is matched, the second B → ε is selected because a ∈ FOLLOW(B), and finally a is matched.
- 3Step 3
The parser may begin with a or b, but after the first terminal the required opposite terminal is not present. The input is rejected.
LL(1) derivation traces
For input :
For input :
The first lookahead symbol determines the production for :
| Lookahead | Selected production |
|---|---|
| $$$ | error |
This is exactly the one-symbol prediction required by LL(1).
Fast LL(1) test
When alternatives contain nullable prefixes, skip those prefixes while computing FIRST. Here, A disappears before a and B disappears before b, so the alternatives are distinguished immediately by a and b.
Why the SLR(1) analysis is different
To test SLR(1), augment the grammar with a new start production:
An LR(0) item places a dot at every possible position in a production. For example,
produces
An item with the dot at the end is a completed item and proposes a reduction.
For SLR(1), a completed item produces a reduce action only on terminals in .
Footnotes
-
LR Parsing and SLR(1) Parsing - Defines LR(0) items, SLR reductions using FOLLOW sets, and shift–reduce/reduce–reduce conflict conditions. ↩
Constructing the Relevant LR(0) Item Sets
- 1Step 1
Add S' → S to identify the accepting configuration.
- 2Step 2
The initial item is S' → ·S. Closure adds S → ·AaAb and S → ·BbBa.
- 3Step 3
Because A → ε and B → ε, closure adds the completed items A → ε· and B → ε· to the initial state.
- 4Step 4
The same LR(0) state contains both completed items A → ε· and B → ε·, while it also contains items that can proceed with terminals a and b.
- 5Step 5
Since FOLLOW(A) = {a,b}, place reduce A → ε under a and b. Since FOLLOW(B) = {a,b}, place reduce B → ε under a and b.
- 6Step 6
Each of the cells for lookahead a and b receives two reductions. Therefore, the SLR table is not single-valued.
Initial LR(0) item set
The closure of the initial item is
The two completed items are especially important:
They both occur in the same LR(0) state.
The SLR reduction rule is:
- reduce on every terminal in ;
- reduce on every terminal in .
Since
the actions are:
| State | ||
|---|---|---|
| SLR action | reduce and reduce | reduce and reduce |
Each cell contains a reduce–reduce conflict.
Why this is a reduce–reduce conflict
On lookahead , the SLR parser sees two valid reductions:
On lookahead , it again sees two valid reductions:
Therefore,
Because the completed items occur in the same LR(0) item set, SLR places both reductions in the same ACTION-table cells. This is precisely the SLR reduce–reduce conflict condition.
Thus,
Footnotes
-
LR Parsing and SLR(1) Parsing - Defines LR(0) items, SLR reductions using FOLLOW sets, and shift–reduce/reduce–reduce conflict conditions. ↩
Visual comparison of the two analyses
The apparent contrast is not contradictory:
- Top-down parsing sees the first terminal of the complete alternative.
- SLR parsing sees two completed nullable reductions in the same LR(0) state.
- SLR uses the coarse FOLLOW approximation for reductions, so both reductions are enabled on both terminals.
Common Points of Confusion
Conflict Comparison
Number of competing actions in the critical parsing-table cells
LL(1) versus SLR(1)
Exam-ready conclusion
Do not claim that the grammar is not SLR(1) merely because A and B both derive ε. The decisive proof is the same LR(0) state containing A → ε· and B → ε·, together with FOLLOW(A) ∩ FOLLOW(B) = {a,b}.
Final proof
For LL(1):
Therefore,
Also,
so the epsilon productions are selected unambiguously. The LL(1) parsing table has no multiply defined entries. Hence,
For SLR(1), the initial LR(0) state contains
SLR places the reductions on their FOLLOW sets. Since
both reductions are entered under both and , producing reduce–reduce conflicts. Hence,
Therefore, the required result is
Knowledge Check
What is FIRST(AaAb) for the grammar?
Explore Related Topics
Which Grammar Type Is the Most Powerful? Understanding Type-0, Type-1, Type-2, and Type-3 Grammars
The most expressive grammar in the Chomsky hierarchy is the unrestricted Type‑0 grammar, which generates all recursively enumerable languages and matches the power of a Turing machine.
- The hierarchy is strict: , so each higher type can generate everything the lower types can, plus more.
- Type‑3 (regular) → finite automaton; Type‑2 (context‑free) → pushdown automaton; Type‑1 (context‑sensitive) → linear‑bounded automaton; Type‑0 (unrestricted) → Turing machine.
- Example languages: is context‑free but not regular; is context‑sensitive but not context‑free.
- “More powerful” refers to expressive capacity (ability to generate a larger class of languages), not ease of parsing or practical use.
Which Automaton Accepts Regular Languages? The Correct Answer Is DFA
The deterministic finite automaton (DFA) is the canonical model that exactly accepts regular languages, whereas PDA, LBA, and Turing machines recognize strictly larger language families.
- A language is regular iff some DFA accepts it: .
- DFA ↔ regular languages; PDA ↔ context‑free; LBA ↔ context‑sensitive; Turing machine ↔ recursively enumerable.
- Regular languages form the base of the hierarchy: .
- DFA’s finite memory limits it to patterns like “ends with 01” or “even number of 1’s”, but it cannot handle unbounded counting such as .
Regular Expression Equivalence: Which Pairs Generate the Same Language?