Lecture 07
Lecture 07
CS143
Lecture 7
2
Recursive Descent vs. LL(1)
• In recursive-descent,
– At each step, many choices of production to use
– Backtracking used to undo bad choices
• In LL(1),
– At each step, only one choice of production
– That is
• When a non-terminal A is leftmost in a derivation
• And the next input symbol is t
• There is a unique production A → α to use
– Or no production to use (an error state)
3
Predictive Parsing and Left Factoring
4
Left-Factoring Example
5
LL(1) Parsing Table Example
• Left-factored grammar
E→TX X→+E|ε
T → int Y | ( E ) Y→*T|ε
int * + ( ) $
E TX TX
X +E ε ε
T int Y (E)
Y *T ε ε ε
int * + ( ) $
E TX TX
X +E ε ε
T int Y (E)
Y *T ε ε ε
7
E→TX X→+E|ε
T → int Y | ( E ) Y→*T|ε
LL(1) Parsing Tables. Errors
int * + ( ) $
E TX TX
X +E ε ε
T int Y (E)
Y *T ε ε ε
8
E→TX X→+E|ε
T → int Y | ( E ) Y→*T|ε
LL(1) Parsing Tables. Errors
int * + ( ) $
E TX TX
X +E ε ε
T int Y (E)
Y *T ε ε ε
9
Using Parsing Tables
10
LL(1) Parsing Algorithm (using the table)
11
LL(1) Parsing Algorithm $ marks bottom of stack
12
E→TX X→+E|ε
T → int Y | ( E ) Y→*T|ε
LL(1) Parsing Example
13
Constructing Parsing Tables: The Intuition
2. Add T[A,t] = ε
if A → α →* ε and S →* γ A t δ
– Useful if stack has A, input is t, and A cannot derive t
– In this case only option is to get rid of A (by deriving ε)
• Can work only if t can follow A in at least one derivation
– We say t ∈ Follow(A)
14
Computing First Sets
Definition
First(X) = { t | X →* tα} ∪ {ε | X →* ε}
Algorithm sketch:
1. First(t) = { t }
2. ε ∈ First(X)
• if X → ε or
• if X → A1 … An and ε ∈ First(Ai) for all 1 ≤ i ≤ n
3. First(α) ⊆ First(X)
• if X → α or
• if X → A1 … An α and ε ∈ First(Ai) for all 1 ≤ i ≤ n
15
First Sets: Example
1. First(t) = { t }
2. ε ∈ First(X)
– if X → ε or
– if X → A1…An and ε ∈ First(Ai) for all 1 ≤ i ≤ n
3. First(α) ⊆ First(X)
– if X → α or
– if X → A1…An α and ε ∈ First(Ai) for all 1 ≤ i ≤ n
E→TX X→+E|ε
T → int Y | ( E ) Y→ *T|ε
First( E ) = First( X ) =
First( T ) = First( Y ) = 16
First Sets: Example
• First sets
First( ( ) = { ( } First( T ) = {int, ( }
First( ) ) = { ) } First( E ) = {int, ( }
First( int) = { int } First( X ) = {+, ε }
First( + ) = { + } First( Y ) = {*, ε }
First( * ) = { * }
17
Computing Follow Sets
• Definition:
Follow(X) = { t | S →* β X t δ }
• Intuition
– If X → A B then First(B) ⊆ Follow(A) and
Follow(X) ⊆ Follow(B)
• if B →* ε then Follow(X) ⊆ Follow(A)
18
Computing Follow Sets (Cont.)
Algorithm sketch:
1. $ ∈ Follow(S)
2. For each production A → αXβ
– First(β) - {ε} ⊆ Follow(X)
3. For each production A → αXβ where ε ∈ First(β)
– Follow(A) ⊆ Follow(X)
19
Computing the Follow Sets (for the Non-Terminals)
• $ ∈ Follow(E)
20
Computing the Follow Sets (for the Non-Terminals)
• $ ∈ Follow(E)
• First(X) ⊆ Follow(T)
• Follow(E) ⊆ Follow(X)
• Follow(E) ⊆ Follow(T) because ε ∈ First(X)
21
Computing the Follow Sets (for the Non-Terminals)
• $ ∈ Follow(E)
• First(X) ⊆ Follow(T)
• Follow(E) ⊆ Follow(X)
• Follow(E) ⊆ Follow(T) because ε ∈ First(X)
• ) ∈ Follow(E)
22
Computing the Follow Sets (for the Non-Terminals)
• $ ∈ Follow(E)
• First(X) ⊆ Follow(T)
• Follow(E) ⊆ Follow(X)
• Follow(E) ⊆ Follow(T) because ε ∈ First(X)
• ) ∈ Follow(E)
• Follow(T) ⊆ Follow(Y)
23
Computing the Follow Sets (for the Non-Terminals)
• $ ∈ Follow(E)
• First(X) ⊆ Follow(T)
• Follow(E) ⊆ Follow(X)
• Follow(E) ⊆ Follow(T) because ε ∈ First(X)
• ) ∈ Follow(E)
• Follow(T) ⊆ Follow(Y)
• Follow(X) ⊆ Follow(E)
24
Computing the Follow Sets (for the Non-Terminals)
• $ ∈ Follow(E)
• First(X) ⊆ Follow(T)
• Follow(E) ⊆ Follow(X)
• Follow(E) ⊆ Follow(T) because ε ∈ First(X)
• ) ∈ Follow(E)
• Follow(T) ⊆ Follow(Y)
• Follow(X) ⊆ Follow(E)
• Follow(Y) ⊆ Follow(T)
25
Computing the Follow Sets (for the Non-Terminals)
• $ ∈ Follow(E)
• First(X) ⊆ Follow(T)
Follow(X)
• Follow(E) ⊆ Follow(X)
$ Follow(E)
• Follow(E) ⊆ Follow(T)
Follow(T)
• ) ∈ Follow(E) )
First(X)
• Follow(T) ⊆ Follow(Y)
• Follow(X) ⊆ Follow(E)
Follow(Y)
• Follow(Y) ⊆ Follow(T)
26
Computing the Follow Sets (for the Non-Terminals)
Follow(X)
$ Follow(E)
Follow(T)
) +
First(X)
Follow(Y)
27
Computing the Follow Sets (for the Non-Terminals)
$,)
$,) Follow(X)
$ Follow(E) $,),+
Follow(T)
) +
First(X)
$,),+
Follow(Y)
28
Computing the Follow Sets (for all symbols)
$,)
$,) Follow(X)
$ Follow(E) $,),+
Follow(T)
) +
First(X)
(,int $,),+
(,int
Follow(+) $,),+
First(E) Follow())
Follow(Y)
(,int
$,),+,*
Follow(() *
First(Y) Follow(int)
(,int (,int
First(T) Follow(*)
29
Follow Sets: Example
• Follow sets
Follow( + ) = { int, ( } Follow( * ) = {int, ( }
Follow( ( ) = { int, ( } Follow( E ) = {$, )}
Follow( X ) = {$, ) } Follow( T ) = {$, +, )}
Follow( ) ) = {+, ) , $} Follow( Y ) = {$, +, )}
Follow( int) = {*, +, ) , $}
30
Constructing LL(1) Parsing Tables
31
Notes on LL(1) Parsing Tables
32