No Slide Title

Download Report

Transcript No Slide Title

LL(1) Parsing
CPSC 388
Ellen Walker
Hiram College
Adding Prediction
• No lookahead: case statement based
on first token (e.g. “exp” in book)
• Adding lookahead: consider two sets
– FIRST(S): All possible first tokens of S (can
include )
– FOLLOW (S): All possible tokens that can
follow S (can include $, for end of string)
Finding First(X)
• If there is a rule X->x…, where x is a terminal,
then x is in First(X)
• If there is a rule X-> , or, if S can derive 
through a sequence of rules, then  is in
First(S)
• If there is a rule X->A…, where A is a nonterminal, then everything in First(A) except 
is in First(S)
• If there is a rule X->AB… and  is in First(A),
then everything in First(B) except  is in
First(X)
Finding Follow(X)
• $ is in Follow(S), where S is the start nonterminal.
• If there is a rule A->…Xx… , where x is a
terminal, then x is in Follow(X)
• If there is a rule A->…XB…, where B is a
non-terminal, then everything in First(B)
except  is in Follow(X)
• If there is a rule A-> …X, then everything in
Follow(A) is in Follow(X)
• If there is a rule A-> …XB… and First(B) is ,
then re-evaluate the rule as if B weren’t there
First & Follow Example
• S->abSXbY | 
• X-> cX | 
• Y -> aY | 
•
•
•
•
•
•
First(S) =
First(X) =
First(Y) =
Follow(S) =
Follow(X) =
Follow(Y) =
Using First & Follow Sets
• Use an explicit stack as in the CFL to
PDA algorithm.
• If the current character is in First (S),
then it’s OK to pop S and push its rule
beginning with this character
• If the current character is in Follow (S)
then it’s OK to pop S without pushing
(i.e. use S->  rule)
LL(1) Example
• Grammar:
S-> aSb | 
• First(S):
{a, }
• Follow(S):
{b, $}
STRING
aabb
aabb
abb
abb
bb
bb
b
STACK
#S
#bSa
#bS
#bbSa
#bbS
#bb
#b
#
Note
Push start
S->aSb
match
S->aSb
match
S->e
match
match
LL Parse Table
• Table of Non-Terminals vs. Terminals
• If S->rhs and a is in FIRST(S) and a is
in FIRST(rhs) put S->rhs in M[S,a]
• If S-> and a is in FOLLOW(S), put
S->  in M[S,a]
• If there is only one entry in each cell, it
is LL(1)
LL(1) Table Example
S
a
S->aSb
STRING
aabb
aabb
abb
abb
bb
bb
b
b
S->e
STACK
#S
#bSa
#bS
#bbSa
#bbS
#bb
#b
#
Note
Push start
S->aSb
match
S->aSb
match
S->e
match
match
$
S->e
Left Factoring
•
•
•
•
Two rules start with the same RHS
Make a new rule to distinguish them
S-> if ( exp ) S
S-> if ( exp ) S else S
becomes
• S-> if ( exp ) S S’
• S’ -> else S | 
Left Recursion Removal
• Rewrite rules to avoid left recursion
S->Sx | y
becomes
S-> yS’
and
S’ -> xS’ | 
• (Note: x and y can be arbitrary strings)
Example:
• E -> E + T | T
• T -> T * F | F
• F -> n | (E)
n stands for “number”
• Left-recursion removal?
• LL(1) table?
LL(k) Grammars
• Look at k terminals instead of 1 terminal
– First(S) is all sequences of k terminals that
can begin S
– Follow(S) is all sequences of k terminals
that can follow S
– Col. headers of table are sequences of k
terminals instead of single terminals
• First & Follow computations get messy!