LR(0) Parsing ummary C412/C41 Introduction to Compilers Tim Teitelbaum Lecture 10: LR Parsing February 12, 2007 LR(0) item = a production with a dot in RH LR(0) state = set of LR(0) items valid for viable prefixes Compute LR(0) states and build DFA: tart state: V(ε) = { [. ] } * Other states: V(αX) = V(α) x * Build the LR(0) parsing table from the DFA Use the LR(0) parsing table to determine whether to reduce or to shift C 412/41 pring 2007 Introduction to Compilers 1 C 412/41 pring 2007 Introduction to Compilers 2 LR(0) Limitations An LR(0) machine only works if each state with a reduce action has only one possible reduce action and no shift action With some grammars, construction gives states with shift/reduce or reduce/reduce conflicts Need to use look-ahead to choose ok [ L L,. ] shift /reduce [ L L,. ] [.,L ] reduce / reduce [L,L. ] [L. ] LR(0) Parsing Table ( ) id, ε L 1 s s2 g4 2 id id id id id s s2 g7 g5 4 accept 5 s6 s8 6 (L) (L) (L) (L) (L) 7 L L L L L 8 s s2 g9 9 L L, L L, L L, L L, L L, C 412/41 pring 2007 Introduction to Compilers C 412/41 pring 2007 Introduction to Compilers 4 A Non-LR(0) Grammar Grammar for addition of bers: Left-associative version is LR(0) Right-associative version is not LR(0) [. ] [. ] [. ] [. ] LR(0) Parsing Table 1 2 What to do in state 2: shift or reduce? [. ] [. ] [. ] [. ] [. ] [. ] ε 2 s/ 4 [. ] 6 5 [. ] [. ] C 412/41 pring 2007 Introduction to Compilers 5 C 412/41 pring 2007 Introduction to Compilers 6 1
LR(1) Parsing LR Parsing = easy extension of LR(0) For each reduction A β, look at the next symbol c Apply reduction only if c is in FOLLOW(A), or c= ε and * γa LR parsing table eliminates some conflicts ame as LR(0) table except reduction rows Adds reductions A βonly in the columns of symbols in FOLLOW(A) xample: FOLLOW()={} but * γ ε 2 s LR Parsing Table Reductions do not fill entire rows Otherwise, same as LR(0) ε 2 s s4 g2 g5 4 5 6 s7 7 accept C 412/41 pring 2007 Introduction to Compilers 7 C 412/41 pring 2007 Introduction to Compilers 8 LR(k) Use the LR(0) machine states as rows of table Let Q be a state and u be a lookahead string Action(Q,u) = shift Goto(Q,b) if Q contains an item of the form [A β 1.bβ ], with u FIRT k (bβ FOLLOW k (A)) Action(Q,u) = accept if Q = { [. ] } and u=ε Action(Q,u) = reduce i if Q contains the item [A β.], where A βis the ith production of G and u FOLLOW k (A), or u=ε and * γa Action(Q,u) = error otherwise G is LR(k) iff the Action function given above is single-valued for all Q and u, i.e, there are no shift-reduce or reduce-reduce conflicts. C 412/41 pring 2007 Introduction to Compilers 9 LR(1) Parsing Get as much power as possible out of 1 lookahead symbol parsing table LR(1) grammar = recognizable by a shift/reduce parser with 1-symbol look-ahead LR(1) parsing uses similar concepts as LR(0) Parser states = sets of items LR(1) item = LR(0) item look-ahead symbol following the production LR(0) item : [. ] LR(1) item : [. ] C 412/41 pring 2007 Introduction to Compilers 10 LR(1) tates LR(1) state = set of LR(1) items LR(1) item = [ A α.β b], where b in Σ {ε} Meaning: α already matched at top of the stack; next expect to see βb horthand notation [ A α. B b 1,, b n ] means: [ A α. β b 1 ] [ A α. β b n ] xtend closure and goto operations [.,ε] [. ] LR(1) Closure LR(1) closure operation on set of items For each item in : [A α.bβ b] and for each production B γ, add the following item to : [B.γ FIRT(βb)], or [B.γ ε] if FIRT(βb)={} Repeat until nothing changes imilar to LR(0) closure, but also keeps track of the look-ahead symbol C 412/41 pring 2007 Introduction to Compilers 11 C 412/41 pring 2007 Introduction to Compilers 12 2
LR(1) tart tate Initial state: start with [. apply the closure operation xample: sum grammar ['. ε] closure ε], then ['. ε] [. ε] [.,ε] LR(1) Goto Operation LR(1) goto operation = describes transitions between LR(1) states Algorithm: for a state and a symbol Y ={[A αy.β b] [A α.yβ b] } Goto(, Y) = Closure( ) 1 [. ε] [. ε] Goto( 1, ) 2 Closure({ [. ε] }) C 412/41 pring 2007 Introduction to Compilers 1 C 412/41 pring 2007 Introduction to Compilers 14 LR(1) DFA Construction If = Goto(,X) then add an edge labeled X from to [ '. ε] [. ε] [.,ε] [. ε] [. ε] [.,ε] [. ε] [.,ε] [. ε] [. ε] [.,ε] LR(1) Reductions Reductions correspond to LR(1) items of the form [A β. x] [ '. ε] [. ε] [.,ε] [. ε] [. ε] [.,ε] [. ε] [.,ε] [. ε] [. ε] [.,ε] C 412/41 pring 2007 Introduction to Compilers 15 C 412/41 pring 2007 Introduction to Compilers 16 LR(1) Parsing Table Construction LR(1) Parsing Table xample ame as construction of LR(0) parsing table, except for reductions If [A β. b] state Q, then: Action(Q,b) is Reduce(A β) 1 2 [. ε] [. ε] [.,ε] [. ε] [. ε] [. ε] [. ε] [.,ε] Fragment of the Parsing table: ε 1 2 2 s C 412/41 pring 2007 Introduction to Compilers 17 C 412/41 pring 2007 Introduction to Compilers 18
LR(1) but not LR(1) Let G have productions aab Ac A a ε V(a) = { [ a.ab ] [ A a. ] [ A.a ] [ A. ] } FOLLOW(A) = {b,c} reduce-reduce conflict C 412/41 pring 2007 Introduction to Compilers 19 LALR(1) Grammars Problem with LR(1): too many states LALR(1) Parsing (Look-Ahead LR) Construct LR(1) DFA and then merge any two LR(1) states whose items are identical except look-ahead Results in smaller parser tables Theoretically less powerful than LR(1) [ id. ] [ id. ε] =? [. ε] [. ] LALR(1) Grammar = a grammar whose LALR(1) parsing table has no conflicts C 412/41 pring 2007 Introduction to Compilers 20 Classification of Grammars LL(1) LR(1) LALR(1) LR(1) LR(0) LR(k) LR(k1) LL(k) LL(k1) LL(k) LR(k) LR(0) LR(1) LALR(1) LR(1) C 412/41 pring 2007 Introduction to Compilers 21 Automate the Parsing Process Can automate: The construction of LR parsing tables The construction of shift-reduce parsers based on these parsing tables Automatic parser generators: yacc, bison, CUP LALR(1) parser generators Not much difference compared to LR(1) in practice maller parsing tables than LR(1) Augment LALR(1) grammar specification with declarations of precedence, associativity output: LALR(1) parser program C 412/41 pring 2007 Introduction to Compilers 22 Associativity hift/reduce Conflict [. ] [.,ε] What happens if we run this grammar through LALR construction? shift/reduce conflict shift: 1(2) reduce: (12) 12 ^ C 412/41 pring 2007 Introduction to Compilers 2 C 412/41 pring 2007 Introduction to Compilers 24 4
Grammar in CUP nonterminal ; terminal PLU, LPARN... precedence left PLU; when shifting a conflicts with reducing a production, choose reduce ::= PLU LPARN RPARN NUMBR ; Precedence CUP can also handle operator precedence T T T T ( ) ( ) C 412/41 pring 2007 Introduction to Compilers 25 C 412/41 pring 2007 Introduction to Compilers 26 Conflicts without Precedence ( ) [. ] [. ] [. ] [. ] C 412/41 pring 2007 Introduction to Compilers 27 Predecence in CUP precedence left PLU; precedence left TIM; // TIM > PLU ::= PLU TIM... RUL: in conflict, choose reduce if last terminal of production has higher precedence than symbol to be shifted; choose shift if vice-versa. In tie, use associativity (left or right) given by precedence rule [. ] [. ] [. ] [. ] reduce hift C 412/41 pring 2007 Introduction to Compilers 28 ummary Look-ahead information makes LR(1), LALR(1), LR(1) grammars expressive Automatic parser generators support LALR(1) grammars Precedence, associativity declarations simplify grammar writing C 412/41 pring 2007 Introduction to Compilers 29 5