Professional Documents
Culture Documents
and
Introduction to Bottom-Up Parsing
Lecture 7
2
LL(1) vs. Recursive Descent
• 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 ® a to use
– Or no production to use (an error state)
4
Left-Factoring Example
5
LL(1) Parsing Table Example
• Left-factored grammar
E®TX X®+E|e
T ® ( E ) | int Y Y®*T|e
int * + ( ) $
E TX TX
X +E e e
T int Y (E)
Y *T e e e
leftmost non-terminal rhs of production to use 6
E®TX X®+E|e
T ® ( E ) | int Y Y®*T|e
LL(1) Parsing Table Example
int * + ( ) $
E TX TX
X +E e e
T int Y (E)
Y *T e e e
7
E®TX X®+E|e
T ® ( E ) | int Y Y®*T|e
LL(1) Parsing Tables. Errors
int * + ( ) $
E TX TX
X +E e e
T int Y (E)
Y *T e e e
8
E®TX X®+E|e
T ® ( E ) | int Y Y®*T|e
LL(1) Parsing Tables. Errors
int * + ( ) $
E TX TX
X +E e e
T int Y (E)
Y *T e e e
9
Using Parsing Tables
1. If A ® a ®* t b
– a can derive a t in the first position
– We say that t Î First(a)
2. If A ® a ®* e and S ®* γ A t d
– 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 e)
• 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 ®* ta} È {e | X ®* e}
Algorithm sketch:
1. First(t) = { t }
2. e Î First(X)
• if X ® e or
• if X ® A1 … An and e Î First(Ai) for all 1 £ i £ n
3. First(a) Í First(X)
• if X ® A1 … An a and e Î First(Ai) for all 1 £ i £ n
15
First Sets: Example
1. First(t) = { t }
2. e Î First(X)
– if X ® e or
– if X ® A1…An and e Î First(Ai) for all 1 £ i £ n
3. First(a) Í First(X)
– if X ® A1…An a and e Î First(Ai) for all 1 £ i £ n
E®TX X®+E|e
T ® ( E ) | int Y Y®*T|e
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 ) = {+, e }
First( + ) = { + } First( Y ) = {*, e }
First( * ) = { * }
17
Computing Follow Sets
• Definition:
Follow(X) = { t | S ®* b X t d }
• Intuition
– If X ® A B then First(B) Í Follow(A) and
Follow(X) Í Follow(B)
• if B ®* e then Follow(X) Í Follow(A)
18
Computing Follow Sets (Cont.)
Algorithm sketch:
1. $ Î Follow(S)
2. For each production A ® aXb
– First(b) - {e} Í Follow(X)
3. For each production A ® aXb where e Î First(b)
– Follow(A) Í Follow(X)
19
Follow Sets: Example
• Follow sets
Follow( + ) = { int, ( } Follow( * ) = { int, ( }
Follow( ( ) = { int, ( } Follow( E ) = {), $}
Follow( X ) = {$, ) } Follow( T ) = {+, ) , $}
Follow( ) ) = {+, ) , $} Follow( Y ) = {+, ) , $}
Follow( int) = {*, +, ) , $}
20
Computing the Follow Sets (for the Non-Terminals)
• $ Î Follow(E)
21
Computing the Follow Sets (for the Non-Terminals)
• $ Î Follow(E)
• First(X) Í Follow(T)
• Follow(E) Í Follow(X)
• Follow(E) Í Follow(T) because e Î First(X)
22
Computing the Follow Sets (for the Non-Terminals)
• $ Î Follow(E)
• First(X) Í Follow(T)
• Follow(E) Í Follow(X)
• Follow(E) Í Follow(T) because e Î First(X)
• ) Î Follow(E)
23
Computing the Follow Sets (for the Non-Terminals)
• $ Î Follow(E)
• First(X) Í Follow(T)
• Follow(E) Í Follow(X)
• Follow(E) Í Follow(T) because e Î First(X)
• ) Î Follow(E)
• Follow(T) Í Follow(Y)
24
Computing the Follow Sets (for the Non-Terminals)
• $ Î Follow(E)
• First(X) Í Follow(T)
• Follow(E) Í Follow(X)
• Follow(E) Í Follow(T) because e Î First(X)
• ) Î Follow(E)
• Follow(T) Í Follow(Y)
• Follow(X) Í Follow(E)
25
Computing the Follow Sets (for the Non-Terminals)
• $ Î Follow(E)
• First(X) Í Follow(T)
• Follow(E) Í Follow(X)
• Follow(E) Í Follow(T) because e Î First(X)
• ) Î Follow(E)
• Follow(T) Í Follow(Y)
• Follow(X) Í Follow(E)
• Follow(Y) Í Follow(T)
26
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)
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 the Non-Terminals)
$,)
$,) Follow(X)
$ Follow(E) $,),+
Follow(T)
) +
First(X)
$,),+
Follow(Y)
29
Computing the Follow Sets (for all symbols)
$,)
$,) Follow(X)
$ Follow(E) $,),+
Follow(T)
) +
(,int First(X)
(,int $,),+
Follow(+) $,),+
First(E) Follow())
Follow(Y)
(,int
Follow(() $,),+,*
*
(,int (,int First(Y) Follow(int)
First(T) Follow(*)
30
Constructing LL(1) Parsing Tables
31
Notes on LL(1) Parsing Tables
32
Bottom-Up Parsing
33
An Introductory Example
35
Observation
37
A Bottom-up Parse
E
int * int + int
int * T + int
T + int
T E
T+T
T T
T+E
E
int * int + int
38
A Bottom-up Parse in Detail (1)
39
A Bottom-up Parse in Detail (2)
40
A Bottom-up Parse in Detail (3)
41
A Bottom-up Parse in Detail (4)
T+T
T T
42
A Bottom-up Parse in Detail (5)
T+T
T T
T+E
43
A Bottom-up Parse in Detail (6)
E
int * int + int
int * T + int
T + int
T E
T+T
T T
T+E
E
int * int + int
44
A Trivial Bottom-Up Parsing Algorithm
45
Questions
46
Where Do Reductions Happen?
47
Notation
48
Shift-Reduce Parsing
Shift
Reduce
49
Shift
ABC|xyz Þ ABCx|yz
50
Reduce
Cbxy|ijk Þ CbA|ijk
51
The Example with Reductions Only
65
Conflicts
66