LL(1) Parsing: Working Mechanism and Parsing Table Construction for a Given Grammar
An LL(1) parser is a deterministic parser that builds a leftmost derivation using a single lookahead symbol. Its core idea is: at each step, look at the current input token (lookahead) and the top stack nonterminal, then consult the predictive parsing table to choose the unique production to apply.
At a high level, predictive parsing works like this:
- Maintain a stack holding grammar symbols to be matched/expanded.
- Repeatedly compare the stack top with the current input symbol.
- If the stack top is a terminal, match it (advance input).
- If the stack top is a nonterminal, consult the parsing table entry for (nonterminal, lookahead) to pick the production and replace the nonterminal on the stack with the production’s RHS (pushed in reverse order).
Below is the standard control flow expressed as a state-style diagram.
LL(1) Parsing (Predictive Parsing) Explained
Key definitions you’ll use
- FIRST set: determines which terminals can start derivations.
- FOLLOW set: determines what can legally follow a nonterminal in a sentential form.
- LL(1) condition: for every nonterminal and lookahead , at most one production is chosen.
These sets are used to fill the parsing table so that is unique when the grammar is LL(1).
Working of an LL(1) Parser (Predictive Parsing Algorithm)
- 1Step 1
Push end-marker $ on the stack, then push the start symbol S. Set lookahead a to the first input token.
- 2Step 2
Let X be the stack top symbol.
- 3Step 3
If X equals the current lookahead a, pop X and advance to the next input token; otherwise reject due to mismatch.
- 4Step 4
Consult the table entry M[X,a]. If it contains a production X → α, pop X and push α onto the stack (rightmost symbol first). If the entry is empty, reject.
- 5Step 5
Accept when the stack top is (both are at end-marker).
Constructing the LL(1) parsing table for the given grammar
Given:
We will build using FIRST/FOLLOW-based rules.
Step 1: Compute FIRST sets
- From , we have FIRST(A) = { }.
- From , we have FIRST(B) = { }.
- From :
- The first terminal of is
- The first terminal of is
So FIRST(S) = { }.
Step 2: Compute FOLLOW sets (needed only to handle ε-productions)
None of the productions produce ε (empty string). Therefore:
- There are no ε-derived contributions to table entries via FOLLOW.
- In standard LL(1) table construction, ε-handling affects entries when a production can derive ε. Since that does not happen here, we can directly place entries using FIRST of the RHS.
We still need the end marker convention for :
- includes end-marker][$]S$ is the start symbol.
- and appear at the end of RHS as and , so their FOLLOW sets would include what can follow those positions—but again, ε is absent, so they will not produce extra table entries beyond those already determined by FIRST.
Table construction rules used (LL(1))
For each production :
- For every terminal , add to .
- If , then for every add to .
Here, is not present anywhere, so we only use the first rule.
LL(1) Parsing Table for the Grammar
Terminals appearing in lookahead are: (and [\]$ for acceptance).
Fill entries
-
For :
- , so
-
For :
- , so
-
For :
- , so
-
For :
- , so
All other entries are error/empty.
LL(1) Parsing Table Entries (Non-empty Cells)
Each row is a stack nonterminal; each column is the lookahead terminal. Non-empty cells contain the chosen production.
For clarity, here is the parsing table in a conventional grid form:
| Nonterminal \ Lookahead | a | b | $ |
|---|---|---|---|
| S | — | ||
| A | — | — | |
| B | — | — |
“—” means: error (no rule for that (nonterminal, lookahead) pair).
Pro Tip: How to predict quickly
Because there are no ε-productions, your LL(1) table here is determined purely by the first terminal of each RHS. That’s why each entry is forced by seeing whether the lookahead is or .
Warning: Empty table entries mean rejection
If the parser is at a nonterminal and the lookahead token is , and is empty, LL(1) parsing must reject—there is no backtracking in predictive parsing.
How LL(1) Parsing Chooses Productions
Look at stack top and lookahead
1Compute (X, a) where X is the stack top and a is the current input token."
Table lookup
2Find in the LL(1) parsing table."
Apply production
3If present, replace X with the production RHS (push RHS reversed)."
Terminal matching
4If stack top is terminal, match it and advance input."
Accept or reject
5Accept at ($$, $). Reject if a mismatch or empty table entry occurs."
LL(1) Quick Check Deck
Knowledge Check
In LL(1) predictive parsing, what guides the choice of which production to apply for a nonterminal X?