lr parsing
play

LR Parsing Compiler Design CSE 504 Shift-Reduce Parsing 1 LR - PDF document

LR Parsing Compiler Design CSE 504 Shift-Reduce Parsing 1 LR Parsers 2 SLR and LR(1) Parsers 3 Last modifled: Fri Mar 06 2015 at 13:50:06 EST Version: 1.7 16:58:46 2016/01/29 Compiled at 12:57 on 2016/02/26 Compiler Design LR Parsing


  1. LR Parsing Compiler Design CSE 504 Shift-Reduce Parsing 1 LR Parsers 2 SLR and LR(1) Parsers 3 Last modifled: Fri Mar 06 2015 at 13:50:06 EST Version: 1.7 16:58:46 2016/01/29 Compiled at 12:57 on 2016/02/26 Compiler Design LR Parsing CSE 504 1 / 32 Shift-Reduce Parsing Leftmost and Rightmost Derivations E − → E + T E − → T − → T id Derivations for id + id : = ⇒ = ⇒ E E + T E E + T = ⇒ T + T = ⇒ E +id = ⇒ id+ T = ⇒ T +id = ⇒ = ⇒ id+id id+id LEFTMOST RIGHTMOST Compiler Design LR Parsing CSE 504 2 / 32

  2. Shift-Reduce Parsing Bottom-up Parsing Given a stream of tokens w , reduce it to the start symbol. − → E E + T E − → T − → T id Parse input stream: id + id : id + id T + id E + id E + T E Reduction ≡ Derivation − 1 . Compiler Design LR Parsing CSE 504 3 / 32 Shift-Reduce Parsing Shift-Reduce Parsing: An Example − → E E + T E − → T T − → id Stack Input Stream Action $ id + id $ shift $ id + id $ reduce by T − → id $ T + id $ reduce by E − → T $ E + id $ shift $ E + id $ shift $ E + id $ reduce by T − → id $ E + T $ reduce by E − → E + T $ E $ ACCEPT Compiler Design LR Parsing CSE 504 4 / 32

  3. Shift-Reduce Parsing Handles “A structure that furnishes a means to perform reductions” E − → E + T − → E T T − → id Parse input stream: id + id : id + id T + id E + id E + T E Compiler Design LR Parsing CSE 504 5 / 32 Shift-Reduce Parsing Handles Handles are substrings of sentential forms: 1 A substring that matches the right hand side of a production 2 Reduction using that rule can lead to the start symbol 3 The rule forms one step in a rightmost derivation of the string = ⇒ E E + T = ⇒ E + id = ⇒ T + id = ⇒ id + id Handle Pruning: replace handle by corresponding LHS. Compiler Design LR Parsing CSE 504 6 / 32

  4. Shift-Reduce Parsing Shift-Reduce Parsing Bottom-up parsing Shift: Construct leftmost handle on top of stack Reduce: Identify handle and replace by corresponding RHS Accept: Continue until string is reduced to start symbol and input token stream is empty Error: Signal parse error if no handle is found. Compiler Design LR Parsing CSE 504 7 / 32 Shift-Reduce Parsing Implementing Shift-Reduce Parsers Stack to hold grammar symbols (corresponding to tokens seen thus far). Input stream of yet-to-be-seen tokens. Handles appear on top of stack. Stack is initially empty (denoted by $). Parse is successful if stack contains only the start symbol when the input stream ends. Compiler Design LR Parsing CSE 504 8 / 32

  5. Shift-Reduce Parsing Preparing for Shift-Reduce Parsing 1 Identify a handle in string. Top of stack is the rightmost end of the handle. What is the leftmost end? 2 If there are multiple productions with the handle on the RHS, which one to choose? Construct a parsing table, just as in the case of LL(1) parsing. Compiler Design LR Parsing CSE 504 9 / 32 Shift-Reduce Parsing Shift-Reduce Parsing: Derivations Stack Input Stream Action $ id + id $ shift $ id + id $ reduce by T − → id $ T + id $ reduce by E − → T $ E + id $ shift $ E + id $ shift $ E + id $ reduce by T − → id $ E + T $ reduce by E − → E + T $ E $ ACCEPT L eft to Right Scan of input R ightmost Derivation in reverse. Compiler Design LR Parsing CSE 504 10 / 32

  6. LR Parsers A Simple Example of LR Parsing − → S BC B − → a C − → a Stack Input Stream Action $ a a $ shift $ a a $ reduce by B − → a $ B a $ shift $ B a $ reduce by C − → a $ B C $ reduce by S − → BC $ S $ ACCEPT Compiler Design LR Parsing CSE 504 11 / 32 LR Parsers A Simple Example of LR Parsing: A Detailed Look Stack Input State Action S ′ − → • S $ a a $ S − → • BC shift S ′ − → S B − → • a − → S B C $ a a $ B − → a • reduce by 3 − → B a S − → B • C $ B a $ shift − → C a C − → • a C − → a • $ B a $ reduce by 4 $ B C $ S − → BC • reduce by 2 S ′ − $ S $ → S • ACCEPT Compiler Design LR Parsing CSE 504 12 / 32

  7. LR Parsers LR Parsing: Another Example Stack Input State Action E ′ − → • E E − → • E + T $ id + id $ shift E − → • T T − → • id $ id + id $ T − → id • reduce by 4 E ′ − → E $ T + id $ E − → T • reduce by 3 − → E E + T E ′ − → E • − → E T $ E + id $ shift E − → E • + T T − → id E − → E + • T $ E + id $ shift T − → • id T − → id • $ E + id $ reduce by 4 $ E + T $ E − → E + T • reduce by 2 E − → E • + T $ E $ ACCEPT E ′ − → E • Compiler Design LR Parsing CSE 504 13 / 32 LR Parsers States of an LR parser E ′ − → • E E − → • E + T I 0 : E − → • T T − → • id Item: A production with “ • ” somewhere on the RHS. Intuitively, grammar symbols before the “ • ” are on stack; grammar symbols after the “ • ” represent symbols in the input stream. Item set: A set of items; corresponds to a state of the parser. Compiler Design LR Parsing CSE 504 14 / 32

  8. LR Parsers States of an LR parser (contd.) E ′ − → • E E − → • E + T Initial State I 0 = closure ( { E ′ − E − → • T → • E } ) T − → • id Closure: What other items are “equivalent” to the given item? Given an item A − → α • B β , closure ( A − → α • B β ) is the smallest set that contains the item A − → α • B β , and 1 every item in closure ( B − → • γ ) for every production B − → γ ∈ G 2 Compiler Design LR Parsing CSE 504 15 / 32 LR Parsers States of an LR parser (contd.) E ′ − → • E E − → • E + T Initial State I 0 = closure ( { E ′ − E − → • T → • E } ) T − → • id T − → id • I 3 = goto ( I 0 , id ) Goto: goto ( I , X ) specifies the next state to visit. X is a terminal: when the next symbol on input stream is X . X is a nonterminal: when the last reduction was to X . goto ( I , X ) contains all items in closure ( A − → α X • β ) for every item A − → α • X β ∈ I . Compiler Design LR Parsing CSE 504 16 / 32

  9. LR Parsers Collection of LR(0) Item Sets The canonical collection of LR(0) item sets, C = { I 0 , I 1 , . . . } is the smallest set such that closure ( { S ′ − → • S } ) ∈ C . I ∈ C ⇒ ∀ X , goto ( I , X ) ∈ C . Compiler Design LR Parsing CSE 504 17 / 32 LR Parsers Canonical LR(0) Item Sets: An Example E ′ − → E E − → T − → − → E E + T T id E ′ − → • E E − → • E + T = closure ( { E ′ − → • E } ) I 0 E − → • T T − → • id E ′ − → E • = goto ( I 0 , E ) I 1 E − → E • + T = goto ( I 0 , T ) E − → T • I 2 I 3 = goto ( I 0 , id ) T − → id • E − → E + • T I 4 = goto ( I 1 , + ) T − → • id I 5 = goto ( I 4 , T ) E − → E + T • Compiler Design LR Parsing CSE 504 18 / 32

  10. LR Parsers LR Action Table E ′ − → E E − → T − → − → E E + T T id id + $ 0 S , 3 1 S , 4 A 2 R 3 R 3 R 3 3 R 4 R 4 R 4 4 S , 3 5 R 2 R 2 R 2 Compiler Design LR Parsing CSE 504 19 / 32 LR Parsers LR Goto Table E ′ − → E E − → T − → − → E E + T T id E T 0 1 2 1 2 3 4 5 5 Compiler Design LR Parsing CSE 504 20 / 32

  11. LR Parsers LR Parsing: States and Transitions Action Table: id + $ E ′ − → − → E E T 0 S , 3 E − → E + T T − → id S , 4 A 1 2 R 3 R 3 R 3 State Symbol 3 R 4 R 4 R 4 Input Action Stack Stack S , 3 4 $ 0 $ id + id $ shift, 3 5 R 2 R 2 R 2 $ 0 3 $ id + id $ reduce by 4 $ 0 2 $ T + id $ reduce by 3 Goto Table: $ 0 1 $ E + id $ shift, 4 E T $ 0 1 4 $ E + id $ shift, 3 0 1 2 $ 0 1 4 3 $ E + id $ reduce by 4 1 2 $ 0 1 4 5 $ E + T $ reduce by 2 3 $ 0 1 $ E $ ACCEPT 4 5 5 Compiler Design LR Parsing CSE 504 21 / 32 LR Parsers LR Parser while ( true ) { switch ( action ( state stack . top (), current token )) { case shift s ′ : symbol stack . push ( current token ); state stack . push ( s ′ ); next token (); case reduce A − → β : pop | β | symbols off symbol stack and state stack ; symbol stack . push ( A ); state stack . push ( goto ( state stack . top (), A )); case accept : return ; default : error ; }} Compiler Design LR Parsing CSE 504 22 / 32

  12. LR Parsers LR Parsing: A review E ′ − → − → E E T − → − → E E + T T id Table-driven shift reduce parsing: Shift Move terminal symbols from input stream to stack. Reduce Replace top elements of stack that form an instance of the RHS of a production with the corresponding LHS Accept Stack top is the start symbol when the input stream is exhausted Table constructed using LR(0) Item Sets. Compiler Design LR Parsing CSE 504 23 / 32 SLR and LR(1) Parsers Conflicts in Parsing Table Grammar: S ′ − → S S − → a S Action Table: S − → ǫ a $ S, 2 0 Item Sets: R 3 R 3 S ′ − → • S 1 A = closure ( { S ′ − → • S } ) S − → • a S I 0 S, 2 2 S − → • R 3 R 3 S ′ − = goto ( I 0 , S ) → S • I 1 3 R 2 R 2 S − → a • S Shift-Reduce = goto ( I 0 , a ) S − → • a S I 2 Conflict S − → • I 3 = goto ( I 2 , S ) S − → a S • Compiler Design LR Parsing CSE 504 24 / 32

Recommend


More recommend