Syntax analysis

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 · slides 5–12
  • Lecture reference: Table-based Predictive Parsing · slides 18–26

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

Lecture reference: Table-based Predictive Parsing · slide 12

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 · slide 11

  • <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 · slide 11

LL(1) parsing table no entry is multiply defined

Non-terminalint*+()$
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

Step 95 of 95
Follow, pass 3 Pass 3 changes nothing. The Follow sets are complete.
First
Follow
First and Follow of the non-terminals
XFirst(X)Follow(X)
E{ (, int } { $, ) }
X{ +, ε } { $, ) }
T{ (, int } { $, +, ) }
Y{ *, ε } { $, +, ) }
Terminals
First and Follow of the terminals
XFirst(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 · slides 24–26
  • Left-factor A → αβ | α γ becomes A → α A’, A’ → β | γ Lecture reference: Table-based Predictive Parsing · slide 6

No left recursion and no common prefixes: there is nothing to rewrite.

Parse the LL(1) parsing algorithm on a token string

Terminals of the grammar, separated by spaces; $ is added at the end.
Row 10 of 10
End of input, and only $ is left on the stack: ACCEPT.

ACCEPT

The token string is in L(G).

next token: $ (end of input)
RowStackInputAction
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-terminalint*+()$
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, $]: ε