Bottom-Up Parsing LR Parsing Maryam Siahbani 2/19/2016 1
What we need for LR parsing LR0) states: Describe all possible states in which parser can be Parsing table ransition between LR0) states Actions to take when transiting: shift, reduce, accept, error 2/19/2016 2
LR0) items For any production we can build a set of items by placing somewhere on the rhs he items for ): ) ) ) ) he only item for is 2/19/2016 3
Viable Prefixes γ is a viable prefix if there is some ω such that γ ω is a state of a shift-reduce parser γ ω stack rest of input Important fact: A viable prefix is a prefix of a handle An LR0) item [X ] says that is on top of the stack is a suffix of γ) he parser is looking for an X xpects to find input string derived from We can recognize viable prefixes via a NfA DFA) States of NFA are LR0) items States of DFA are sets of LR0) items LR0) states) 2/19/2016 4
Recognizing Viable Prefixes 1. Add a dummy production S S to G 2. States of NFA are LR0) items of G including dummy production 3. ransition between items: [ X ] X [ X ] X can be terminal or nonterminal) For item [ X ] and production X γ, add: [ X ] 4. very state is accepting state 5. Start from [S S] [X γ] X is non-terminal) Note: NFA reads states from bottom of the stack to the top 2/19/2016 5
) S S NFA for Recognizing VP ) ) S + * ) 2/19/2016 6 ) ) + + + + + * * * * *
S S NFA for Recognizing VP + S + * ) 2/19/2016 7
) S S NFA for Recognizing VP + * S + * ) 2/19/2016 8
) S S NFA for Recognizing VP + + * S + * ) 2/19/2016 9
) S S NFA for Recognizing VP ) + + * S + * ) 2/19/2016 10
) S S NFA for Recognizing VP ) + + * ) S + * ) 2/19/2016 11
) S S NFA for Recognizing VP ) + + * ) ) ) S + * ) 2/19/2016 12
) S S NFA for Recognizing VP ) + + * ) ) ) + S + + * ) 2/19/2016 13
) S S NFA for Recognizing VP ) + + * ) ) ) + S + + * ) + 2/19/2016 14
) S S NFA for Recognizing VP ) + + + * ) ) ) S + + * ) + 2/19/2016 15
) S S NFA for Recognizing VP ) + + + * ) ) ) * S + + * ) + 2/19/2016 16
) S S NFA for Recognizing VP ) + + + * ) ) S + * ) 2/19/2016 17 * * ) * + +
) S S NFA for Recognizing VP ) ) ) S + * ) 2/19/2016 18 ) + + + + + * * * * *
) S S NFA for Recognizing VP ) ) S + * ) 2/19/2016 19 ) ) + + + + + * * * * *
S + * ) S S + * ) DFA for recognizing VP + + * * * ) * + + * ) * + 2/19/2016 20 ) ) ) ) + * )
LR0) states he states of DFA are LR0) states which are also called: Canonical collection of items Canonical collection of LR0) items Dragon book gives a different way of constructing LR0) states Section 4.6, 4.7) 2/19/2016 21
Valid Items An item I is valid for a viable prefix if DFA recognizing viable prefixes terminates) terminates on input in a state s containing I he items in s describe what the top of the item stack might be after reading input 2/19/2016 22
Valid Items An item is often valid for many prefixes xample: the item ) is valid for prefixes:... 2/19/2016 23
S + * ) S S + * ) DFA for recognizing VP + + * * * ) * + + * ) * + 2/19/2016 24 ) ) ) ) + * )
Assume: Stack contains Next input is t LR0) Parsing DFA on input terminates in state s Reduce by X if S contains item X Shift if s contains item X tω quivalent to saying s has a transition labeled t 2/19/2016 25
LR0)-conditions LR0) has a reduce-reduce conflicts if: Any state has two reduce items: X and Y ω LR0) has shift-reduce conflicts if: Any state has a reduce item and a shift item: X and Y ω t γ If there is any conflict, grammar is not LR0) 2/19/2016 26
S + * ) S S + * ) Shift-reduce conflict LR0) Parsing + + * * * ) * + + * ) * + 2/19/2016 27 ) ) ) ) + * )
Assume: Stack contains Next input is t SLR Parsing DFA on input terminates in state s Reduce by X if s contains item X t FollowX) Shift if If there is still conflicts under hese rules, grammar is not SLR s contains item X tω 2/19/2016 28
S + * ) S S + * ) Follow)={$,)} Follow)={$,),+} + SLR Parsing + * * * ) * + + * ) * + 2/19/2016 29 ) ) ) ) + * )
SLR Parsing Let M be DFA for viable prefixes of G Let x 1 x n $ be initial configuration Repeat until configuration is S $ Let ω be current configuration Run M on current stack If M rejects, report parsing error Stack is not a viable prefix If M accepts with items I, let a be the next input Shift [X a γ] I Reduce if [X ] I and a FollowX) If there is any conflict in the last step more than two valid action), grammar is not SLRk) in practice k=1 Report parsing error if neither applies 2/19/2016 30
race * configuration Stack input) * $ DFA halt state Action 2/19/2016 31
S + * ) 2 S S 1 + * ) 11 5 + 3 * * * ) * * $ + 6 4 + + * ) * 2/19/2016 32 ) 10 ) ) 9 + 8 ) + * ) 7
race * configuration Stack input) * $ * $ DFA halt state Action 1 Shift 2/19/2016 33
S + * ) 2 S S 1 + * ) Follow)={$,),+} * $ 11 5 + + 3 * * * ) * 6 4 + + * ) * 2/19/2016 34 ) 10 ) ) 9 + 8 ) + * ) 7
race * configuration Stack input) * $ * $ * $ DFA halt state 1 3 * Follow) Action Shift Shift 2/19/2016 35
S + * ) 2 S S 1 + * ) 11 5 + + 3 * * * ) * 6 4 * $ + + * ) * 2/19/2016 36 ) 10 ) ) 9 + 8 ) + * ) 7
race * configuration Stack input) * $ * $ * $ * $ DFA halt state 1 3 * Follow) 11 Action Shift Shift Shift 2/19/2016 37
S + * ) 2 S S 1 + * ) Follow)={$,),+} * $ 11 5 + + 3 * * * ) * 6 4 + + * ) * 2/19/2016 38 ) 10 ) ) 9 + 8 ) + * ) 7
race * configuration Stack input) * $ * $ * $ * $ * $ DFA halt state 1 3 * Follow) 11 3 $ Follow) Action Shift Shift Shift Reduce 2/19/2016 39
S + * ) 2 S S 1 + * ) Follow)={$,),+} * $ 11 5 + + 3 * * * ) * 6 4 + + * ) * 2/19/2016 40 ) 10 ) ) 9 + 8 ) + * ) 7
race * configuration Stack input) * $ * $ * $ * $ * $ $ DFA halt state 1 3 * Follow) 11 3 $ Follow) 4 $ Follow) Action Shift Shift Shift Reduce Reduce * 2/19/2016 41
S + * ) 2 S S 1 + * ) Follow)={$,)} 11 5 + + 3 * * * ) * 6 4 $ + + * ) * 2/19/2016 42 ) 10 ) ) 9 + 8 ) + * ) 7
race * configuration Stack input) * $ * $ * $ * $ * $ $ $ DFA halt state 1 3 * Follow) 11 3 $ Follow) 4 $ Follow) 5 $ Follow) Action Shift Shift Shift Reduce Reduce * Reduce 2/19/2016 43
race * configuration Stack input) * $ * $ * $ * $ * $ $ $ DFA halt state 1 3 * Follow) 11 3 $ Follow) 4 $ Follow) 5 $ Follow) Action Shift Shift Shift Reduce Reduce * Reduce Accept 2/19/2016 44
S + * ) 2 S S 1 + * ) 11 5 + + 3 * * * ) * 6 4 + + * ) * 2/19/2016 45 ) 10 ) ) 9 + 8 ) + * ) 7
Constructing SLR states Begin with item S S, calculate related items closure) Determine following states: what states can be reached on a single input token or non-terminal GOO) Construct closure of each resulting states 2/19/2016 46
Closure ClosureI, G): C=I repeat /* I : set of items */ for each [X Y ] C for each Y γ G /* for each production Y*/ if [Y γ] C add [Y γ] to C until no change in C return C 2/19/2016 47
Closure Initial item: [S ] C= {[S ]} First pass: C= {[S ], [ ], [ +]} Second pass: S + C= {[S ], [ ], [ +], [ ], [ *], [ )]} * ) 2/19/2016 48
Closure example Closure results in a new state in DFA S 1 + * ) S + * ) 2/19/2016 49
New states GOO function o determine possible states reachable from existing states use goto function GOOI, X, G): /* X is an input token or a nonterminal*/ C={} for each [Y X ] I /* can be empty*/ add [Y X ] to C return ClosureC, G) 2/19/2016 50
S 1 + * ) GOO example 3 * ) 8 + * ) S + * ) 2/19/2016 51
Sets-of-Items Construction LR0) states function itemsg ) /*G augmented grammar*/ C = { closure{s S}, G) } repeat foreach I C do foreach X N U ) do /*N: non-terminals*/ /*: input tokens*/ C = C U { GOOI, X,G) } until no change to C return C /*set of all LR0) states*/ 2/19/2016 52
S + * ) 2 S S 1 + * ) 11 5 + + 3 * * * ) * 6 + + * ) 4 * 2/19/2016 53 ) 10 ) ) 9 + 7 8 ) + * )
Constructing SLR Parsing able Rows are LR0) states Columns are input tokens and non-terminals and $) 1. Construct LR0) states the initial state is the one containing S S) 2. For each transition A X B in the set of states, add an action shift B in column X for row A 3. For each item [Y α] A, add an action reduce Y α in columns X FollowY) for row A 2/19/2016 54
1) + 2) 3) 4) * 5) ) R5: reduce to rule 5 S8: - if on top of the stack: shift and go to state 8 - else: go to state 8 1. Grammar is not SLR if there is more than one action in any table entry 2. mpty entries are errors SLR Parsing able Action/Goto able * + ) $ 1 S8 S3 5 2 2 ACC! 3 S11 R3 R3 R3 4 R4 R4 R4 5 S6 R2 R2 6 S8 S3 5 7 7 R1 R1 8 S8 S3 5 9 9 S10 10 R5 R5 R5 11 S8 S3 4 2/19/2016 55
SLR1) Conditions A grammar is SLR1) if for each LR0) state item sets): For any item [A x ], x there is no item [B ], x FollowB) For any two items [A ] and [B ] FollowA) FollowB) = LR0) Grammars SLR1) Grammars 2/19/2016 56
S S S AxB S B A yb A z B A S S 1 S AxB S B A yb A z B A S S S 2 Is this grammar SLR1)? S A xb B A B FollowB)={$,x} A z S B y 4 3 S y B B A A yb A z z A z S Ax B B A A yb A z 2/19/2016 57 y 6 x 5 y B A z A 8 B S AxB B A A yb 9 7 10
1) S AxB 2) S B 3) A yb 4) A z 5) B A Grammar is not SLR Reduce is a bad choice SLR Parsing able x y z $ S A B 1 S5 S6 2 3 4 2 ACC! 3 S8,R5 R5 4 R2 5 S5 S6 7 9 6 R4 R4 7 R5 R5 8 S5 S6 7 10 9 R3 R3 10 R1 2/19/2016 58
LR1) Parsing Limit roduced by SLR parsing in using Follow set to decide reductions Idea: augment LR items with 1 character lookahead [B A, $] making an LR1) item Reduce to B only if lookahead token is $ More accurate than just Follow set Similar to SLR parsing just use LR1) items rather than LR0) items 2/19/2016 59