PPT

advertisement
Top-Down parsing
LL(1) parsing
Overview of Top-Down
 There are only two actions
1. Replace
2. Match
Parsing stack
Start-Symbol$
...
...
$
Input stack
Input-Token$
...
...
$
Actions
accept
Overview of Top-Down
 e.g. with grammar
S  (S)S|
S$
(S)S$
S)S$
)S$
S$
$
( )$
( )$
)$
)$
$
$
S  (S)S
match
S
match
S
accept
S
1.replace
(
S
2.match
)
S
4.match

3.replace

5.replace
LL(k) parsing
 L stands for left-to-right parse of input
 L stands for leftmost-derivation
 k stands for k-token look-ahead
LL(1) Parsing
 At top of Parsing stack
 For Non-terminal
 Replace: a decision must be made, based on the
current input token.
 For terminal
 Match: no decision is to be made but need to see
if it is the same as the current input token, if not,
an error occurs.
LL(1) Parsing : First Sets
 First Sets
 Let X be a grammar symbol (a terminal or non-terminal) or
ε. Then the set First(X) consisting of terminals, and possibly
ε, is defined as follows
1. If X is a terminal or  , then First(X )=  X  .
2. If X is a nonterminal, then for each production choice X  X 1 X 2 ... X n ,
First(X ) contains First(X ) contains First(X 1 )    .
If also for some i < n, all the sets First(X 1 ),..., First(X i ) contains  ,
then First(X ) contains First(X i+1 )    .
If all the sets First(X 1 ),...,First(X n ) contains  , then First(X ) contains 
LL(1) Parsing : First Sets
 e.g. for the grammar
S (
S
) S
| 
S  X1 X 2 X 3 X 4 | X 5
First(S)  First(X 1 )   ( 
First(S)  First(X 5 )   
thus
First(S)   ( , 


LL(1) Parsing : Follow Sets
 Follow Sets
 Given a non-terminal A, the set Follow(A),
consisting of terminals, and possibly $, is defined
as follows.
1. If A is the start symbol, then $ is in Follow(A).
2. If there is a production B   A , then First( )-   is in Follow(A).
3. If there is a production B   A such that  is in First( ),
then Follow(A) contains Follow(B).
 ε is never an element of Follow set
LL(1) Parsing : Follow Sets
 e.g. for the grammar
S  (S)S | 
S is the start symbol , thus Follow(S)   $ 
S  ( S )S
S   S
Follow(S)  First( )      $ , ) 
S1  (S)S
S1   S
  is in First( )
Follow(S)   $ , ) 
 Follow(S) contains Follow(S1 )
First & Follow set
The First set and Follow set for the grammar
S  (S)S | 
is
First(S)   ( , 

Follow(S)   $ , ) 
Non-terminal
nullable
First
Follow
S
Yes
(
$,)
LL(1) Parsing table
 How to construct LL(1) parsing table
For each production rule A  
1. for each terminal t  First( )
M[A,t]  
2. if   First( ) and t  Follow(A)
M[A,t]  
LL(1) Parsing table
The First set and Follow set for the grammar

Follow(S)   $ , ) 
S  (S)S |  is First(S)
 ( , 
for production S  (S)S
(  First  ( S ) S
 , thus M[ S , ( ] 
(S)S
for production S  
  First( ) and )  Follow(S) , thus M[ S , ) ]  
  First( ) and $  Follow(S) , thus M[ S , $ ]  
M[N,T]
(
)
$
S
(S)S
ε
ε
LL(1) Parsing table
S  (S)S|
S$
(S)S$
S)S$
)S$
S$
$
( )$
( )$
)$
)$
$
$
S  (S)S
match
S
match
S
accept
M[N,T]
(
)
$
S
(S)S
ε
ε
LL(1) grammar
 A grammar is LL(1) if the following
conditions are satisfied.
1. For every production A  1| 2 |...| n , First( i )  First( j ) is empty
for all i and j, 1  i,j  n, i  j.
2. For every nontermianl A such that First(A) contains  ,
First(A)  Follow(A) is empty.
 That is the LL(1) parsing table has at most
one production in each table entry.
Mentioned issues
 Left Recursion removal
 Left Factoring
References
 COMPILER CONSTRUCTION:
Principles and Practice,
 Kenneth C. Louden
 Chapter 4
 http://www.cs.sjsu.edu/faculty/louden/cm
ptext/
 http://www.course.com/isbn=0-53493972-4
Download