LL(1) Parsing Table
Compute First and Follow sets, build the LL(1) parsing table of a grammar, rewrite a grammar that is not LL(1), and trace the table-driven parser on a token string.
- Lecture reference: Table-based Predictive Parsing
- Lecture reference: Table-based Predictive Parsing
Grammar BNF, without EBNF; the first left-hand side is the start symbol
Open in Context-Free Grammars, Predictive Recursive Descent, Shift-Reduce Parsing or LR(1) Parser Builder
The LL(1) parsing algorithm on int * int $: the stack starts as < E $ >, its top on the left. Each row’s action is what is done to that row: the right-hand side pushed, terminal for a match, ε, or ACCEPT.
<N, rest> : if T[N,*next] == Y₁ … Yₙ … else error (); // Why the error?
Show answer
T[N,*next] is blank: no production of N can be used with that token next, so the input is not in L(G).
Lecture reference: Table-based Predictive Parsing
<t, rest> : if t == *next ++ … else error (); // Why the error?
Show answer
The terminal on top of the stack has to be the next token. Any other token cannot be matched.
Lecture reference: Table-based Predictive Parsing
LL(1) parsing table no entry is multiply defined
| Non-terminal | int | * | + | ( | ) | $ |
|---|---|---|---|---|---|---|
| T | ||||||
| E | ||||||
| X | ||||||
| Y |
A cell holds the right-hand side to use; a blank cell is an error. $ is the end marker.
Select an entry: the productions in it, and the First or Follow set that put each one there, are listed here.
First and Follow the rules applied until no set changes
| X | First(X) | Follow(X) |
|---|---|---|
| E | { (, int } | { $, ) } |
| X | { +, ε } | { $, ) } |
| T | { (, int } | { $, +, ) } |
| Y | { *, ε } | { $, +, ) } |
Terminals
| X | First(X) | Follow(X) |
|---|---|---|
| + | { + } | { (, int } |
| ( | { ( } | { (, int } |
| ) | { ) } | { $, +, ) } |
| int | { int } | { $, +, ), * } |
| * | { * } | { (, int } |
Rewrite for LL(1) left recursion removed and left-factored, without EBNF
- Remove left recursion A → A α | β becomes A → β A’, A’ → α A’ | ε Lecture reference: Top-Down Parsing
- Left-factor A → αβ | α γ becomes A → α A’, A’ → β | γ Lecture reference: Table-based Predictive Parsing
No left recursion and no common prefixes: there is nothing to rewrite.
Parse the LL(1) parsing algorithm on a token string
ACCEPT
The token string is in L(G).
| Row | Stack | Input | Action |
|---|---|---|---|
| E $ | int * int $ | T X | |
| T X $ | int * int $ | int Y | |
| int Y X $ | int * int $ | terminal | |
| Y X $ | * int $ | * T | |
| * T X $ | * int $ | terminal | |
| T X $ | int $ | int Y | |
| int Y X $ | int $ | terminal | |
| Y X $ | $ | ε | |
| X $ | $ | ε | |
| $ | $ | ACCEPT |
- On top of the stack
- Just pushed
- Matched
The table
| Non-terminal | int | * | + | ( | ) | $ |
|---|---|---|---|---|---|---|
| T | [T, int]: int Y | [T, *]: blank | [T, +]: blank | [T, (]: ( E ) | [T, )]: blank | [T, $]: blank |
| E | [E, int]: T X | [E, *]: blank | [E, +]: blank | [E, (]: T X | [E, )]: blank | [E, $]: blank |
| X | [X, int]: blank | [X, *]: blank | [X, +]: + E | [X, (]: blank | [X, )]: ε | [X, $]: ε |
| Y | [Y, int]: blank | [Y, *]: * T | [Y, +]: ε | [Y, (]: blank | [Y, )]: ε | [Y, $]: ε |