Section: LL Parsing
LL(k) Parser:
• top-down parser - starts with start
symbol on stack, and repeatedly
replace nonterminals until string is
generated.
• predictive parser - predict next
rewrite rule
• first L of LL means - read input
string left to right
• second L of LL means - produces
leftmost derivation
• k - number of lookahead symbols
1
LL parsing process:
• convert CFG to PDA (different
method than before)
• Use the PDA and lookahead
symbols
• Lookahead symbol is next symbol
in input string
2
Convert CFG to NPDA
The constructed NPDA:
• Three states: s, q, f
start in state s
push S on stack, move into q
all rewrite rules in state q: If lhs of
rewrite rule on top of stack, replace
it with rhs of rewrite rule and stay
in state q
additional rules in q to recognize
nonterminals: read input symbol,
pop input symbol, stay in state q
pop z from stack, move into f,
accept
Example:
L = {anbbn : n ≥ 0} S
3
state = s
push(S)
state = q
read(symbol)
while top-of-stack 6= z do
case top-of-stack of
S: if symbol == a then
{pop(); push(aSb)}
else if symbol == b then
{pop(); push(b)}
else error
a: if symbol 6= a, then error
else {pop(); read(symbol)}
b: if symbol 6= b, then error
else {pop(); read(symbol)}
end case
end while
pop()
if symbol 6= $ then error
state = f
4
LL Parse Table - 2-dim array
• rows - variables
• cols - terminals, $ (end of string
marker)
• LL[i,j]
Example: Parse table for
L = {anbbn : n ≥ 0}
S → aSb | b
5
A generic parsing routine
push(S)
read(symbol)
while stack not empty do
case top-of-stack of
terminal:
if top-of-stack == symbol
then {pop(); read(symbol)}
else
error
variable:
if LL[top-of-stack, symbol] 6= error
then {pop()
push(LL[top-of-stack,symbol])}
else
error
end case
end while
if symbol 6= $, then error
6
Example:
S → aSb
S→c
a b c $
S aSb error c error
Example:
S → Ac | Bc
A → aAb | λ
B→b
7
To construct an LL parse table
LL[rows,cols]:
1. For each rule A → w
(a) For each a in FIRST(w)
add w to LL[A,a]
(b) If λ is in FIRST(w)
add w to LL[A,b] for each b in
FOLLOW(A)
2. Each undefined entry is error.
8
Example:
S → aSc | B
B→b|λ
FIRST FOLLOW
S a,b,λ $,c
B b,λ $,c
To Compute the LL Parse Table for
this example:
• For S → aSc,
FIRST(aSc) =
• For S → B,
FIRST(B) = {b, λ}
FOLLOW(S) = {$, c}
• For B → b,
FIRST(b) =
• For B → λ
FIRST(λ) =
9
LL(1) Parse Table:
a b c $
Parse string: aacc
10
Trace aabcc
a
a S S B b
S S c c c c c
Stack: S c c c c c c c c
symbol: a a a’ a’ b b b c c’
11
Example: Construct Parse Table for:
S → AcB
A → aAb
A→λ
B → aBb
B→c
FIRST(A) =
FIRST(S) =
FIRST(B) =
FOLLOW(A) =
FOLLOW(S) =
FOLLOW(B) =
12
To compute the parse table:
• For S → AcB,
FIRST(AcB) =
• For A → aAb,
FIRST(aAb) =
• For A → λ,
FIRST(λ) =
• For B → aBb,
FIRST(aBb) =
• For B → c,
FIRST(c) =
• All other entries are errors.
13
LL(1) Parse Table:
a b c $
14
Example:
S → AcB
A → aAb | ab
B → aBb | acb
FIRST FOLLOW
a b c $
15
LL(2) Parse Table:
aa ab ac a$ b c $
S AcB AcB error error error error error
A aAb ab error error error error error
B aBb error acb error error error error
parse string: aabbcacb
a a
A A b b
A b b b b b a
c c c c c c c c c
Stack: S B B B B B B B B b b
symbol: aa aa aa ab ab bb bc ca ac ac cb
16
Example:
L = {an : n ≥ 0} ∪ {anbn : n ≥ 0}
S→A
S→B
A → aA
A→λ
B → aBb
B→λ
Example:
L = {an : 0 ≤ n ≤ 10} ∪ {anbn : 0 ≤ n ≤ 10}
Example:
L = {anbbn : n ≥ 0} ∪ {anbn : n ≥ 0}
S → aSb
S→b
S→λ
17