lr 0 and slr parse table construction
play

LR(0) and SLR parse table construction Wim Bohm and Michelle Strout - PowerPoint PPT Presentation

LR(0) and SLR parse table construction Wim Bohm and Michelle Strout CS, CSU CS453 Lecture Building LR Parse Tables 1 parse (x,(x))$ Parse table for List grammar stack input action 0: S S$ 1: S ( L


  1. LR(0) and SLR parse table construction Wim Bohm and Michelle Strout CS, CSU CS453 Lecture Building LR Parse Tables 1

  2. parse (x,(x))$ Parse table for List grammar stack input action 0: S ’ à à S$ 1: S à à ( L ) 2: S à à x 0 (x,(x))$ s2 3: L à à S 4: L à à L , S 0(2 x,(x))$ s1 0(2x1 ,(x))$ r2: S à x ( ) x , $ S L 0(2S6 ,(x))$ r3: L à S 0 s2 s1 g3 0(2L4 ,(x))$ s7 1 r2 r2 r2 r2 r2 0(2L4,7 (x))$ s2 2 s2 s1 g6 g4 0(2L4,7(2 x))$ s1 3 a 0(2L4,7(2x1 ))$ r2: S à x 4 s5 s7 0(2L4,7(2S6 ))$ r3: L à S 5 r1 r1 r1 r1 r1 0(2L4,7(2L4 ))$ s5 6 r3 r3 r3 r3 r3 0(2L4,7(2L4)5 )$ r1: S à (L) 0(2L4,7S8 )$ r4: L à L,S 7 s2 s1 g8 0(2L4 )$ s5 8 r4 r4 r4 r4 r4 0(2L4)5 $ r1:S à (L) 03S $ a CS453 Shift-reduce Parsing 2

  3. LR(0) table construction Example grammar for Nested Lists: 0: S ’ à à S$ 1: S à à ( L ) 2: S à à x 3: L à à S 4: L à à L , S We start with an empty stack and with a complete S$ sentence on input We indicate this as follows: S ’ à à . S$ this (a rule with a dot in it) is called an item, it indicates what is in the stack (left of .) and what is to be expected on input (right of .) The input can start with anything S can start with, eg an x or a ( We indicate this as follows: S ’ à à .S$ (we are making a DFA S à à .x through another sub-set closure S à à .(S) remember the NFA à à DFA ) We call this a state: state 0, the start state with an empty prefix (V[ ε ]) CS453 Shift-reduce Parsing 3

  4. Shift, reduce, goto actions in LR(0) table construction goto action: S ’ à .S$ S ’ à S.$ S à x. Also in state 0 S à .x 3:V[S] 1:V[x] we could have S à .(L) reduced to S 0:V[ ε ] S à (.L) shift action: In state 1 we are at the end L à .L,S In state 0 we of an item. This will give rise L à .S can shift an x to a reduce action S à .x or shift a ( S à .(L) 2:V[(] Transitions: the shifts and gotos explicitly connect the states. The reduces implicitly move to another state by popping the rhs off the stack, after which a goto with the lhs will produce a new next state CS453 Shift-reduce Parsing 4

  5. LR(0) states and transitions L à L,.S x x S L à L,S. S ’ à .S$ S à x. S à .x 8:V[(L,S] S à .x 1:V[x] S à .(L) S à .(L) 7:V[(L] , ( x 0:V[ ε ] ( S à (L.) S S à (.L) L List grammar ( L à L.,S L à .L,S 0: S ’ à S$ S ’ à S.$ L à .S 1: S à ( L ) 3:V[S] 4:V[(L] S à .x 2: S à x S ) S à .(L) 3: L à S 2:V[(] 4: L à L , S S à (L). L à S. 5:V[(L)] 6:V[(S] CS453 Shift-reduce Parsing 5

  6. LR(0) Closure, Goto, State Diagram, Reduce Goto(I,X): // state I , symbol X Closure(I): // state I if X==$ return {} // no gotos for $ repeat J = {} // new state for any item A à α . X β for any item A à α . X β in I for any X à γ J+= A à α X . β I+= X à . γ return Closure(J) // close it until I does not change State Diagram construction Reduce(T): T = Closure({ S ’ à .S$ } ); // states R = {} E = {} // edges (gotos and shifts) for each state I in T repeat until no change in E or T for each item A à α . for each state I in T R += (I, A à α ) for each item A à α . X β in I J = Goto(I,X); T +=J ; E += (X: (I,J)) // the edge (I,J) labeled X CS453 Shift-reduce Parsing 6

  7. Applying the Algorithm to the Nested Lists Example List grammar 0: S ’ à S$ S ’ à .S$ 1: S à ( L ) 2: S à x 3: L à S 0:V[ ε ] 4: L à L , S CS453 Shift-reduce Parsing 7

  8. LR(0) states and transitions L à L,.S x x S L à L,S. S’ à .S$ S à x. S à .x 8:V[(L,S] S à .x 1:V[x] S à .(L) S à .(L) 7:V[(L] , ( x 0:V[ ε ] ( S à (L.) S S à (.L) L List grammar ( L à L.,S L à .L,S 0: S’ à S$ S’ à S.$ L à .S 1: S à ( L ) 3:V[S] 4:V[(L] S à .x 2: S à x S ) S à .(L) 3: L à S 2:V[(] 4: L à L , S S à (L). L à S. 5:V[(L)] 6:V[(S] CS453 Shift-reduce Parsing 8

  9. LR(0) parse table construction Parse table rows: states columns: terminals (for shift and reduce actions) non-terminals (for goto actions) For each edge (X: (I, J)) if X is terminal, put shift J at (I, X) if X is non-terminal, put goto J at (I, X) if I contains S ’ à S . $ , put accept at (I, $) if I contains A à α . where A à α . has grammar rule number n for each terminal x, put reduce reduce n at (I, x) CS453 Shift-reduce Parsing 9

  10. parse (x,(x))$ Parse table for List grammar stack input action 0: S’ à S$ 1: S à ( L ) 2: S à x 0 (x,(x))$ s2 3: L à S 4: L à L , S 0(2 x,(x))$ s1 0(2x1 ,(x))$ r2: S à x ( ) x , $ S 0(2S6 ,(x))$ r3: L à S L 0(2L4 ,(x))$ s7 0 s2 s1 g3 0(2L4,7 (x))$ s2 1 r2 r2 r2 r2 r2 0(2L4,7(2 x))$ s1 2 s2 s1 g6 g4 0(2L4,7(2x1 ))$ r2: S à x 3 a 0(2L4,7(2S6 ))$ r3: L à S 0(2L4,7(2L4 ))$ s5 4 s5 s7 0(2L4,7(2L4)5 )$ r1: S à (L) 5 r1 r1 r1 r1 r1 0(2L4,7S8 )$ r4: L à L,S 6 r3 r3 r3 r3 r3 0(2L4 )$ s5 7 s2 s1 g8 0(2L4)5 $ r1:S à (L) 8 r4 r4 r4 r4 r4 03S $ a CS453 Shift-reduce Parsing 10

  11. Building the LR Parse Table for LR(0), nested parens example [0] S -> ( S ) [1] S ’ -> S EOF [2] S -> ID CS453 Lecture Building LR Parse Tables 11

  12. LR(0) states for nested parens example CS453 Lecture Building LR Parse Tables 12

  13. Building the Table from the State Diagram Action Goto State ( ) $ ID S 0 s3 s1 2 1 r2 r2 r2 r2 2 accept 3 s3 s1 4 4 s5 5 r0 r0 r0 r0 CS453 Lecture Building LR Parse Tables 13

  14. Suggested Exercise: Building the LR Parse Table for LR(0) (0) S ’ -> E $ (1) E -> E || B (2) E -> B (3) B -> t (4) B -> f CS453 Lecture Building LR Parse Tables 14

  15. Problem with LR(0): shift reduce conflict If there is an item A à à α . item in I , we reduce for all terminals This can cause CONFLICTS: T * T à T * . F E à à E+T E à .E+T E à T. E à à T E à .T T à T . *F T à à T*F T à .T*F 0 1 2 In state 1: we reduce (E à à T.) AND we shift (T à à T . *F) What should we do? CS453 Shift-reduce Parsing 15

  16. LR(0) shift reduce conflict We can resolve the conflict by looking at a right most derivation: E à à T à à T*F à à T*id à à F*id à à id*id Stack input action id*id$ S id *id$ R: F à à id F *id$ R: F à à T T *id$ S We should shift. WHY? T* id$ S T*id $ R: F à à id T*F $ R: T à à T*F T $ R:E à à E $ accept CS453 Shift-reduce Parsing 16

  17. SLR parsing SLR parsing is LR(0) parsing, but with a different reduce rule: For each edge (X: (I, J)) if X is terminal, put shift J at (I, X) if I contains A à α . where A à α . has rule number n for each terminal x in Follow(A), put reduce reduce n at (I, x) Build an SLR parser for our expression grammar ¡0: ¡S à à E$ ¡ ¡ ¡1:E à à E+T ¡ ¡ ¡2:E à à T ¡ ¡ ¡3:T à à T*F ¡ ¡ ¡4:T à à F ¡ ¡ ¡5:F à à (E) ¡ ¡ ¡6:F à à id ¡ ¡ ¡Need ¡to ¡build ¡the ¡transiAon ¡diagram ¡and ¡follow ¡sets ¡ ¡ ¡ ¡ ¡ ¡ ¡to ¡decide ¡where ¡to ¡put ¡the ¡reduce ¡acAons ¡in ¡the ¡SLR ¡table CS453 Shift-reduce Parsing 17

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend