Proving That a Grammar Is LL(1) but Not SLR(1)

Proving That a Grammar Is LL(1) but Not SLR(1)

Verified Sources
Sep 11, 2026

Consider the grammar

SAaAbBbBaAϵBϵ\begin{aligned} S &\rightarrow AaAb \mid BbBa\\ A &\rightarrow \epsilon\\ B &\rightarrow \epsilon \end{aligned}

where aa and bb are terminals, SS is the start symbol, and ϵ\epsilon denotes the empty string.

This section proves two different properties:

  1. The grammar is LL(1) because one input symbol is sufficient to choose every production in a predictive top-down parser.
  2. 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

  1. Top-Down Parsing - Describes FIRST/FOLLOW-based predictive parsing tables and LL(1) conditions.

  2. 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 AA and BB derive ϵ\epsilon,

AϵandBϵ.A \Rightarrow \epsilon \qquad\text{and}\qquad B \Rightarrow \epsilon.

Therefore,

SAaAbabS \Rightarrow AaAb \Rightarrow ab

and

SBbBaba.S \Rightarrow BbBa \Rightarrow ba.

Thus,

L(G)={ab,ba}.L(G)=\{ab,ba\}.

The two alternatives begin with different terminals:

  • AaAbAaAb begins with aa, because AA disappears and the next symbol is aa.
  • BbBaBbBa begins with bb, because BB disappears and the next symbol is bb.

This separation is the central reason the grammar is LL(1).

Computing FIRST and FOLLOW Sets

  1. 1
    Step 1

    Since both nonterminals derive epsilon, FIRST(A) = { ε } and FIRST(B) = { ε }.

  2. 2
    Step 2

    A can disappear, so the first visible symbol is terminal a. Therefore FIRST(AaAb) = { a }.

  3. 3
    Step 3

    B can disappear, so the first visible symbol is terminal b. Therefore FIRST(BbBa) = { b }.

  4. 4
    Step 4

    The alternatives have disjoint FIRST sets: FIRST(S) = { a, b }.

  5. 5
    Step 5

    Because S is the start symbol, the end-marker belongs to FOLLOW(S). Thus FOLLOW(S) = { }.

  6. 6
    Step 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 }.

  7. 7
    Step 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 productionFIRSTFOLLOW
AA{ϵ}\{\epsilon\}{a,b}\{a,b\}
BB{ϵ}\{\epsilon\}{a,b}\{a,b\}
SS{a,b}\{a,b\}\{\}$
AaAbAaAb{a}\{a\}
BbBaBbBa{b}\{b\}

The two alternatives for SS satisfy

FIRST(AaAb)FIRST(BbBa)={a}{b}=.FIRST(AaAb)\cap FIRST(BbBa) = \{a\}\cap\{b\} = \varnothing.

Although both alternatives contain nullable nonterminals, neither alternative itself derives ϵ\epsilon:

ϵFIRST(AaAb),ϵFIRST(BbBa).\epsilon\notin FIRST(AaAb), \qquad \epsilon\notin FIRST(BbBa).

Consequently, there is no FIRST/FIRST conflict and no FIRST/FOLLOW conflict for SS.

Constructing the LL(1) parsing table

For a production XαX\rightarrow\alpha, place the production in M[X,t]M[X,t] for each tFIRST(α)t\in FIRST(\alpha). If ϵFIRST(α)\epsilon\in FIRST(\alpha), place it under every symbol in FOLLOW(X)FOLLOW(X).

The terminals are aa, bb, and $$$.

Nonterminalaabb$$$
SSSAaAbS\rightarrow AaAbSBbBaS\rightarrow BbBaerror
AAAϵA\rightarrow\epsilonAϵA\rightarrow\epsilonerror
BBBϵB\rightarrow\epsilonBϵB\rightarrow\epsilonerror

Every populated table cell contains exactly one production. Therefore, the grammar is LL(1).

Formally,

FIRST(AaAb)FIRST(BbBa)=,FIRST(AaAb)\cap FIRST(BbBa)=\varnothing,

and the nullable productions for AA and BB are selected uniquely using their FOLLOW sets. Hence,

G is LL(1).\boxed{G\text{ is LL(1)}}.

Footnotes

  1. Top-Down Parsing - Describes FIRST/FOLLOW-based predictive parsing tables and LL(1) conditions.

Testing the LL(1) Parser

  1. 1
    Step 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.

  2. 2
    Step 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.

  3. 3
    Step 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 abab:

SAaAbϵaϵbab.\begin{aligned} S &\Rightarrow AaAb\\ &\Rightarrow \epsilon a\epsilon b\\ &\Rightarrow ab. \end{aligned}

For input baba:

SBbBaϵbϵaba.\begin{aligned} S &\Rightarrow BbBa\\ &\Rightarrow \epsilon b\epsilon a\\ &\Rightarrow ba. \end{aligned}

The first lookahead symbol determines the production for SS:

LookaheadSelected production
aaSAaAbS\rightarrow AaAb
bbSBbBaS\rightarrow BbBa
$$$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:

SS.S'\rightarrow S.

An LR(0) item places a dot at every possible position in a production. For example,

SAaAbS\rightarrow AaAb

produces

SAaAbSAaAbSAaAbSAaAbSAaAb.\begin{aligned} S&\rightarrow \bullet AaAb\\ S&\rightarrow A\bullet aAb\\ S&\rightarrow Aa\bullet Ab\\ S&\rightarrow AaA\bullet b\\ S&\rightarrow AaAb\bullet. \end{aligned}

An item with the dot at the end is a completed item and proposes a reduction.

For SLR(1), a completed item XαX\rightarrow\alpha\bullet produces a reduce action only on terminals in FOLLOW(X)FOLLOW(X).

Footnotes

  1. 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

  1. 1
    Step 1

    Add S' → S to identify the accepting configuration.

  2. 2
    Step 2

    The initial item is S' → ·S. Closure adds S → ·AaAb and S → ·BbBa.

  3. 3
    Step 3

    Because A → ε and B → ε, closure adds the completed items A → ε· and B → ε· to the initial state.

  4. 4
    Step 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.

  5. 5
    Step 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.

  6. 6
    Step 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

I0={SS,SAaAb,SBbBa,Aϵ,Bϵ}.\begin{aligned} I_0=\{& S'\rightarrow\bullet S,\\ &S\rightarrow\bullet AaAb,\\ &S\rightarrow\bullet BbBa,\\ &A\rightarrow\epsilon\bullet,\\ &B\rightarrow\epsilon\bullet \}. \end{aligned}

The two completed items are especially important:

AϵandBϵ.A\rightarrow\epsilon\bullet \qquad\text{and}\qquad B\rightarrow\epsilon\bullet.

They both occur in the same LR(0) state.

The SLR reduction rule is:

  • reduce AϵA\rightarrow\epsilon on every terminal in FOLLOW(A)FOLLOW(A);
  • reduce BϵB\rightarrow\epsilon on every terminal in FOLLOW(B)FOLLOW(B).

Since

FOLLOW(A)=FOLLOW(B)={a,b},FOLLOW(A)=FOLLOW(B)=\{a,b\},

the actions are:

State I0I_0aabb
SLR actionreduce AϵA\rightarrow\epsilon and reduce BϵB\rightarrow\epsilonreduce AϵA\rightarrow\epsilon and reduce BϵB\rightarrow\epsilon

Each cell contains a reduce–reduce conflict.

Why this is a reduce–reduce conflict

On lookahead aa, the SLR parser sees two valid reductions:

AϵorBϵ.A\rightarrow\epsilon \qquad\text{or}\qquad B\rightarrow\epsilon.

On lookahead bb, it again sees two valid reductions:

AϵorBϵ.A\rightarrow\epsilon \qquad\text{or}\qquad B\rightarrow\epsilon.

Therefore,

FOLLOW(A)FOLLOW(B)={a,b}.FOLLOW(A)\cap FOLLOW(B) = \{a,b\} \neq\varnothing.

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,

G is not SLR(1).\boxed{G\text{ is not SLR(1)}}.

Footnotes

  1. 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 SS 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)

1 / 6
Question · Term

What is FIRST(AaAb)?

Click to reveal
Answer · Definition

{a}\{a\}, because AA derives ϵ\epsilon and the first visible terminal is aa.

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):

FIRST(AaAb)={a},FIRST(BbBa)={b}.FIRST(AaAb)=\{a\}, \qquad FIRST(BbBa)=\{b\}.

Therefore,

FIRST(AaAb)FIRST(BbBa)=.FIRST(AaAb)\cap FIRST(BbBa)=\varnothing.

Also,

FOLLOW(A)=FOLLOW(B)={a,b},FOLLOW(A)=FOLLOW(B)=\{a,b\},

so the epsilon productions are selected unambiguously. The LL(1) parsing table has no multiply defined entries. Hence,

G is LL(1).\boxed{G\text{ is LL(1)}}.

For SLR(1), the initial LR(0) state contains

AϵandBϵ.A\rightarrow\epsilon\bullet \qquad\text{and}\qquad B\rightarrow\epsilon\bullet.

SLR places the reductions on their FOLLOW sets. Since

FOLLOW(A)FOLLOW(B)={a,b},FOLLOW(A)\cap FOLLOW(B)=\{a,b\}\neq\varnothing,

both reductions are entered under both aa and bb, producing reduce–reduce conflicts. Hence,

G is not SLR(1).\boxed{G\text{ is not SLR(1)}}.

Therefore, the required result is

The grammar is LL(1) but not SLR(1).\boxed{\text{The grammar is LL(1) but not SLR(1).}}

Knowledge Check

Question 1 of 4
Q1Single choice

What is FIRST(AaAb) for the grammar?

Explore Related Topics

1

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: L3L2L1L0L_3 \subset L_2 \subset L_1 \subset L_0, 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: {anbnn0}\{a^n b^n \mid n \ge 0\} is context‑free but not regular; {anbncnn1}\{a^n b^n c^n \mid n \ge 1\} 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.
2

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 M=(Q,Σ,δ,q0,F)M=(Q,\Sigma,\delta,q_0,F) accepts it: L(M)={wΣδ(q0,w)F}L(M)=\{w\in\Sigma^* \mid \delta^*(q_0,w)\in F\}.
  • DFA ↔ regular languages; PDA ↔ context‑free; LBA ↔ context‑sensitive; Turing machine ↔ recursively enumerable.
  • Regular languages form the base of the hierarchy: RegularContext-FreeContext-SensitiveRecursively Enumerable\text{Regular} \subset \text{Context-Free} \subset \text{Context-Sensitive} \subset \text{Recursively Enumerable}.
  • 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 {anbnn0}\{a^n b^n\mid n\ge0\}.
3

Regular Expression Equivalence: Which Pairs Generate the Same Language?