LL(1) Parsing
Put it all together — learn how an LL(1) parser uses a parsing table built from FIRST and FOLLOW sets to parse any LL(1) grammar in linear time without backtracking.
What Does LL(1) Mean?
LL(1) is a notation that describes a class of parsers and grammars:
- L (first): read input Left to right
- L (second): build a Leftmost derivation
- (1): use 1 token of lookahead to make every decision
An LL(1) parser is a table-driven predictive parser that uses exactly one token of lookahead to decide which production to apply. It never backtracks.
Why "1 Token of Lookahead"?
Compare LL(k) parsers with different lookahead values:
LL(1) is the sweet spot: powerful enough for most languages, and the parsing table is small and fast.
The Two Key Components
An LL(1) parser requires:
- A grammar that is LL(1) — no left recursion, no common prefixes, and no conflict in the parsing table.
- A parsing table M[A][a] — a 2D table indexed by (non-terminal, terminal) that tells the parser exactly which production to use.
Building the LL(1) Parsing Table
Algorithm: For each production A → α in the grammar:
Full Example — Arithmetic Expression Grammar
Grammar:
FIRST sets:
FOLLOW sets:
Filling the table:
| Production | FIRST of RHS | FOLLOW of LHS | Table entries added |
|---|---|---|---|
| E → T E' | { (, id } | — | M[E][(] = E→TE', M[E][id] = E→TE' |
| E' → + T E' | { + } | — | M[E'][+] = E'→+TE' |
| E' → ε | { ε } → use FOLLOW | { $, ) } | M[E'][$] = E'→ε, M[E'][)] = E'→ε |
| T → F T' | { (, id } | — | M[T][(] = T→FT', M[T][id] = T→FT' |
| T' → * F T' | { * } | — | M[T'][*] = T'→*FT' |
| T' → ε | { ε } → use FOLLOW | { +, $, ) } | M[T'][+] = T'→ε, M[T'][$] = T'→ε, M[T'][)] = T'→ε |
| F → ( E ) | { ( } | — | M[F][(] = F→(E) |
| F → id | { id } | — | M[F][id] = F→id |
Complete Parsing Table:
Every cell has at most one entry — this grammar is LL(1).
The LL(1) Parsing Algorithm
The parser uses a stack to track what still needs to be matched, and the parsing table to decide what to do next.
Algorithm
Important: Push symbols in reverse order so Y₁ ends up on top of the stack (leftmost is processed first).
Step-by-Step Trace — Parsing "id + id * id"
Input: id + id * id $
LL(1) Conflicts — When a Grammar Is NOT LL(1)
A grammar is not LL(1) if its parsing table has a cell with more than one production. This is called a conflict.
Type 1 — FIRST/FIRST Conflict
Two productions for the same non-terminal start with the same terminal.
Both productions start with a. When we see a, we cannot decide which to use.
Fix: Left-factor the grammar.
Now M[S'][b] = S'→b and M[S'][c] = S'→c — no conflict.
Type 2 — FIRST/FOLLOW Conflict
A non-terminal has both an ε-production and a production that starts with a terminal in FOLLOW.
If a ∈ FOLLOW(A), then when we see a, we cannot decide:
- Use A → a (match the 'a')
- Use A → ε (let 'a' be matched by whoever follows A)
This usually means the grammar is inherently ambiguous for LL(1) parsing and needs to be redesigned.
Checking LL(1) Conditions
A grammar is LL(1) if and only if:
If all three conditions hold for every pair of productions, the grammar is LL(1).
Worked Example — Full LL(1) Parse
Let's work through a complete example from grammar to parse.
Grammar:
This grammar generates all strings of balanced parentheses (including the empty string).
Step 1 — FIRST sets:
Step 2 — FOLLOW sets:
Step 3 — Build parsing table:
Parsing table:
Step 4 — Parse "(())"
The parse correctly recognises "(())" as a valid balanced-parenthesis string.