Compiler Design · LL(1) Parsing
Constructing the LL(1) Parsing Table
Overview
The LL(1) parsing table tells a top-down parser which production to apply for each non-terminal and each lookahead terminal. It is built from the grammar's FIRST and FOLLOW sets, and it matters because it turns the parser's choice between alternatives into a simple table lookup.
Key concepts
- LL(1) parsing table: the table used by an LL(1) top-down parser to decide, for each non-terminal (variable) and each lookahead terminal, which production to apply. It is built from the FIRST and FOLLOW sets of the grammar.
- Why FIRST is used: a top-down parser must choose between two alternatives for a variable, and the choice depends on the FIRST set of the right-hand side of each production — which terminal(s) that production can begin generating.
- Why FOLLOW is used: a production such as is used when is not wanted at all; whatever follows can then appear, so the FOLLOW of the left-hand side decides the entry.
- Rows: all variables (non-terminals): , , , , .
- Columns: all terminals of the grammar:
id,+,*,(,), and also$. - Why
$is a column: the input string always ends with$, so the parser may see$in the input and must be able to take an action on it.
How it works
- Lay out the table with non-terminals as rows and terminals (including
$) as columns. - For a production , place it in the row of (the left-hand side), because that production is chosen only when is being expanded in the derivation.
- Determine the column from — the FIRST of the right-hand side, not the FIRST of the left-hand side, because the right-hand side is what actually generates the strings.
- For each terminal in , put in .
Drawing the diagram…
Formulas
- for every — the entry for production in row , column .
- — the columns for .
- — the column for .
Worked examples
Placing
- Row: (the production is used only when is being expanded).
- Column: .
- Therefore write under both
idand(in row . - Meaning: when the parser is at and must derive
id(or(), it applies .
Placing
- Row: (chosen whenever is being expanded).
- Column: .
- Therefore write under
+in row . - Meaning: when the parser is at and must generate
+, it applies .
Choosing between alternatives: for and , when the parser sees id it uses the first production; when it sees (, it uses the second. The decision is driven by the FIRST set of the symbol/production.
Revision summary
- Rows are non-terminals; columns are terminals plus
$. - The column always comes from FIRST of the right-hand side, never FIRST of the left-hand side.
- The row always comes from the left-hand side of the production.
$must be a column because every input string ends with$.- FIRST/FOLLOW exist to let the parser pick one production when a variable has multiple alternatives.
- goes under
idand(; goes under+.
Previous-year exam insights
- LL(1) table construction rule in full: for each production A → α, add A → α to M[A, a] for every a ∈ FIRST(α); if ε ∈ FIRST(α), add A → α to M[A, b] for every b ∈ FOLLOW(A) (with $ included in FOLLOW of the start symbol).
Asked inGATE 2024GATE 2021GATE 2012GATE 2006GATE 2003 - A grammar is LL(1) iff no table cell receives more than one production, i.e. for any two distinct productions A → α and A → β: FIRST(α) ∩ FIRST(β) = ∅, and if β ⇒* ε then FIRST(α) ∩ FOLLOW(A) = ∅ (and at most one production can derive ε).
Asked inGATE 2024GATE 2021 - FIRST of a string X1X2…Xn: include FIRST(X1) − {ε};
if ε ∈ FIRST(X1) include FIRST(X2) − {ε}, and so on;
include ε only if every Xi can derive ε. FIRST(ε) = {ε}.
Asked inGATE 2024GATE 2021GATE 2012 - FOLLOW rules: put $ in FOLLOW(S) for the start symbol S; for A → αBβ put FIRST(β) − {ε} in FOLLOW(B); for A → αB or A → αBβ with ε ∈ FIRST(β), put FOLLOW(A) in FOLLOW(B).
Asked inGATE 2024GATE 2021GATE 2012GATE 2006 - A blank (empty) cell in the LL(1) table means a syntax error for that non-terminal/lookahead pair; the parser reports an error rather than applying a production.
Asked inGATE 2012GATE 2003 - If a non-terminal has a single production whose RHS cannot derive ε, its entry appears only under FIRST of that RHS; if the RHS is a single non-terminal, the entry is under FIRST of that non-terminal (FIRST propagates through leading non-terminals).
Asked inGATE 2006GATE 2003