1. Gyanastra
  2. Compiler Design
  3. LL(1) Parsing
  4. Constructing the LL(1) Parsing Table
Free lesson, no account needed 5 min read 5 practice questions after it
Asked in past papers6 times
  • GATE CSE202420212012200620031998

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 E′→εE' \to \varepsilon is used when E′E' is not wanted at all; whatever follows E′E' can then appear, so the FOLLOW of the left-hand side decides the entry.
  • Rows: all variables (non-terminals): EE, E′E', TT, T′T', FF.
  • 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

  1. Lay out the table with non-terminals as rows and terminals (including $) as columns.
  2. For a production A→αA \to \alpha, place it in the row of AA (the left-hand side), because that production is chosen only when AA is being expanded in the derivation.
  3. Determine the column from FIRST(α)\text{FIRST}(\alpha) — 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.
  4. For each terminal aa in FIRST(α)\text{FIRST}(\alpha), put A→αA \to \alpha in table[A,a]\text{table}[A, a].
Figure. Rows come from the left-hand side and columns from FIRST of the right-hand side. E to T E prime lands under id and open paren, E prime to plus T E prime under plus; a cell with two productions breaks LL(1).

Formulas

  • table[A,a]=A→α\text{table}[A, a] = A \to \alpha for every a∈FIRST(α)a \in \text{FIRST}(\alpha) — the entry for production A→αA \to \alpha in row AA, column aa.
  • FIRST(TE′)=FIRST(T)={id,(}\text{FIRST}(T E') = \text{FIRST}(T) = \{ \texttt{id}, \texttt{(} \} — the columns for E→TE′E \to T E'.
  • FIRST(+TE′)={+}\text{FIRST}(+ T E') = \{ + \} — the column for E′→+TE′E' \to + T E'.

Worked examples

Placing E→TE′E \to T E'

  • Row: EE (the production is used only when EE is being expanded).
  • Column: FIRST(TE′)=FIRST(T)={id,(}\text{FIRST}(T E') = \text{FIRST}(T) = \{ \texttt{id}, \texttt{(} \}.
  • Therefore write E→TE′E \to T E' under both id and ( in row EE.
  • Meaning: when the parser is at EE and must derive id (or (), it applies E→TE′E \to T E'.

Placing E′→+TE′E' \to + T E'

  • Row: E′E' (chosen whenever E′E' is being expanded).
  • Column: FIRST(+TE′)={+}\text{FIRST}(+ T E') = \{ + \}.
  • Therefore write E′→+TE′E' \to + T E' under + in row E′E'.
  • Meaning: when the parser is at E′E' and must generate +, it applies E′→+TE′E' \to + T E'.

Choosing between alternatives: for E→idE \to \texttt{id} and E→(E)E \to \texttt{(} E \texttt{)}, 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 AA 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.
  • E→TE′E \to T E' goes under id and (; E′→+TE′E' \to + T E' 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
Pages 1–2 of 2