LR(0) and SLR(1) Parsers
Total Page:16
File Type:pdf, Size:1020Kb
LR(0) and SLR(1) Parsers C.Naga Raju B.Tech(CSE),M.Tech(CSE),PhD(CSE),MIEEE,MCSI,MISTE Professor Department of CSE YSR Engineering College of YVU Proddatur Prof.C.NagaRaju YSRCE of YVU CSE 9949218570 1 Contents • Introduction to bottom up parsers • LR(0) Parser • Example problem • Gate Questions and solutions • SLR(1) Parser • Example problem • Gate Questions and solutions Prof.C.NagaRaju YSRCE of YVU CSE 9949218570 Bottom up Parser • Construction of parse tree for any given input string beginning at the bottom and working towards the root is called bottom up parser For example the given input string : id*id E -> E + T | T id*id F * id T * id T * F T E T -> T * F | F F -> (E) | id id F F id T * F T id id F id T * F id F id id Prof.C.NagaRaju YSRCE of YVU CSE 9949218570 Shift-Reduce Parsing • The general idea is to shift some symbols of input to the stack until a reduction can be applied • At each reduction step, if a specific substring is matched then the body of a production is replaced by the Non Terminal at the head of the production A—>ac/b • The key decisions during bottom-up parsing are about when to reduce and what production should apply • A reduction is a reverse of a step in a derivation • The goal of a bottom-up parser is to construct a derivation in reverse: – E=>T=>T*F=>T*id=>F*id=>id*id Prof.C.NagaRaju YSRCE of YVU CSE 9949218570 LR Parsers LR(k)LR k Left-to-Right Rightmost Derivation In Number Of Input scan Reverse Symbols Of Look Ahead Prof.C.NagaRaju YSRCE of YVU 5 CSE 9949218570 ❖ Types of LR Parsers 1.LR(0) Parser 2.Simple LR-Parser (SLR) 3.Canonical LR Parser (CLR) 4.LALR Parser. Prof.C.NagaRaju YSRCE of YVU 6 CSE 9949218570 Comparison of LL & LR Methods LL(1) LR(1) LALR SLR LR(0) LL(0) Prof.C.NagaRaju YSRCE of YVU 7 CSE 9949218570 ▪ Advantages of LR Parsers ▪ LR parsers are constructed to recognize all Programming Languages ▪ The LR-parsing is Non-Backtracking Shift- Reduce Parser ▪ An LR parser can detect a syntactic errors ▪ It scans input string from left-to-right and use left most derivation in reverse Prof.C.NagaRaju YSRCE of YVU 8 CSE 9949218570 •The LR Parsing Algorithm Input a1 a2 … ai … an $ Scanner sm LR Parsing Engine Xm s m-1 Compiler Construction Xm-1 … s Parser 0 Action Goto Grammar Generator Stack LR Parsing Tables Prof.C.NagaRaju YSRCE of YVU 9 CSE 9949218570 ❖ The LR parser consists of 1) Input 2)Output 3)Stack 4) Driver Program 5) Parsing Table ❖ The Driver Program is same for all LR Parsers. ❖ Only the Parsing Table changes from one parser to the other. ❖ In CLR method the stack holds the states from the LR(0)automation and canonical LR and LALR methods are same Prof.C.NagaRaju YSRCE of YVU 10 CSE 9949218570 ❖ The Driver Program uses the Stack to store a string s0X1s1X2…Xmsm ✓ Where sm is the Top of the Stack. ✓ The Sk‘s are State Symbols ✓ The Xi‘s are Grammar Symbols. ✓ Together State and Grammar Symbols determine a Shift-reduce Parsing Decision. Prof.C.NagaRaju YSRCE of YVU 11 CSE 9949218570 ❖ The Parsing Program reads characters from an Input Buffer one at a time ❖ The Current Input Symbols are used to index the parsing table and determine the shift-reduce parsing decision ❖ In an implementation, the grammar symbols need not appear on the stack Prof.C.NagaRaju YSRCE of YVU 12 CSE 9949218570 Parse Table ❖ The LR Shift-Reduce Parsers can be efficiently implemented by computing a table to guide the processing ❖ The Parsing Table consists of two parts: 1. A Parsing Action Function and 2. A GOTO function. Prof.C.NagaRaju YSRCE of YVU 13 CSE 9949218570 ❖ The Action Table ❖ The Action Table specifies the actions of the parser (e.g., shift or reduce), for the given parse state and the next token ✓ Rows are State Names; ✓ Columns are Terminals Prof.C.NagaRaju YSRCE of YVU 14 CSE 9949218570 LR Driver Program ❖ The LR driver Program determines Sm, the state on top of the stack and ai, the Current Input symbol. ❖ It then consults Action[ Sm, ai ] which can take one of four values: ✓ Shift ✓ Reduce ✓ Accept ✓ Error Prof.C.NagaRaju YSRCE of YVU 15 CSE 9949218570 ❖ If Action[ Sm, ai ] = Shift S ✓ Where S is a State, then the Parser pushes ai and S on to the Stack. ❖ If Action[ Sm, ai ] = Reduce A → β, ✓ Then ai and Sm are replaced by A ✓ if S was the state appearing below ai in the Stack, then GOTO[S, A] is consulted and the state pushed onto the stack. Prof.C.NagaRaju YSRCE of YVU 16 CSE 9949218570 ❖ If Action[ Sm, ai ] = Accept, ✓ Parsing is completed ❖ If Action[ Sm, ai ] = Error, ✓ The Parser discovered an Error. Prof.C.NagaRaju YSRCE of YVU 17 CSE 9949218570 ❖ GOTO Table ❖ The GOTO table specifies which state to put on top of the stack after a reduce ✓Rows are State Names; ✓Columns are Non-Terminals 18 Prof.C.NagaRaju YSRCE of YVU CSE 9949218570 ❖ The GOTO Table is important to find out the next state after every reduction. ❖ The GOTO Table is indexed by a state of the parser and a Non Terminal (Grammar Symbol) ex : GOTO[S, A] ❖ The GOTO Table simply indicates what the next state of the parser if it has recognized a certain Non Terminal. Prof.C.NagaRaju YSRCE of YVU 19 CSE 9949218570 ❖ Right Sentential Form is a Sentential Form in a Rightmost Derivation Example: (S)S , ( (S)S) ❖ Viable Prefix is a sequence of symbols on the parsing stack Example: ✓ (S)S, (S), (S, (, ✓ ((S)S, ((S), ((S , ((, ( Prof.C.NagaRaju YSRCE of YVU 20 CSE 9949218570 LR(0) Parser ❖ The LR Parser is a Shift-reduce Parser that makes use of a Deterministic Finite Automata, recognizing the Set Of All Viable Prefixes by reading the stack from Bottom To Top. ❖ if a Finite-State Machine that recognizes viable prefixes of the right sentential forms is constructed, it can be used to guide the handle selection in the Shift-reduce Parser. Prof.C.NagaRaju YSRCE of YVU 21 CSE 9949218570 ❖ Handle: Handle is a substring that matches the body of a production ❖ Handle is a Right Sentential Form + position where reduction can be performed + production used for reduction Example ( S ) S. With S → Є ( S ) .S With S → S ( ( S ) S . ) With S → ( S ) S Prof.C.NagaRaju YSRCE of YVU 22 CSE 9949218570 • Handle pruning : Handle pruning specifies that the reduction represents one step along the reverse of a rightmost derivation Right sentential form Handle Reducing production id*id id F->id F*id F T->F T*id id F->id T*F T*F E->T*F Prof.C.NagaRaju YSRCE of YVU CSE 9949218570 Augmented Grammar ❖ If G is a Grammar with Start Symbol S, the Augmented Grammar G’ is G with a New Start Symbol S`, and New Production S` →S$. ❖ The Purpose of the Augmented Grammar is to indicate to the parser when it should stop parsing and announce acceptance of the input Prof.C.NagaRaju YSRCE of YVU 24 CSE 9949218570 LR(0) Items An LR(0) Item of a Grammar G is a Production of G with a Dot ( ) at some position of the right side. .Production A → XYZ yields the Four items: 1. A→•XYZ We hope to see a string derivable from XYZ next on the input. 2. A→X•YZ We have just seen on the input a string derivable from X and that we hope next to see a string derivable from YZ next on the Prof.C.NagaRaju YSRCE of YVU 25 input. CSE 9949218570 3. A→XY•Z 4. A→XYZ• ❖ The production A→ generates only one item, A→•. ❖ Each of this item is a Viable prefixes ❖ Closure Item : An Item created by the closure operation on a state. ❖ Complete Item : An Item where the Item Dot is at the end of the RHS. Prof.C.NagaRaju YSRCE of YVU 26 CSE 9949218570 LR(0) Example Context Free Grammar: S → E rule1: (S→S’) E → T Augumented Grammar E → E + T T → i S → •E T → ( E ) E → •T E → •E+T T → •i T → •(E) Prof.C.NagaRaju YSRCE of YVU 27 CSE 9949218570 Construction of LR(0) Closure Items S6 E → T• T T S7 S0 S → •E$ T → (•E) E → •T E → •T ( E → •E+T E → •E+T T → •i T → •i T → •(E) ( T → •(E) i S5 i E T → i• E ( S8 S1 S → E•$ T → (E•) E → E•+T i E → E•+T + + ) $ E → E+•T S3 T → •i S9 S2 T → •(E) T → (E)• S → E$• T S4 Prof.C.NagaRaju YSRCE of YVU 28 E → E+T• CSE 9949218570 Construction of LR(0) Items I : I1:Goto(I0,E) 0 I2 :Goto(I1,$ ) I :Goto(I ,+ ) I :Goto(I ,T ) S → E • $ 3 1 4 3 S → •E$ S → E $ • E → E + • T E →E+T • E → E • + T E → • T T → • i E → • E + T T → • (E) I7 :Goto(I0,( ) T → • i I5 :Goto(I0,i) I5 :Goto(I3,i) T → i • T → i • T →( •E) T → • (E) I6 :Goto(I0,T) E → • T E → T • E → • E + T I :Goto(I ,E) I :Goto(I , ) ) 8 7 9 7 T → • i T →( E•) T →( E )• T → • (E) T → E•+ T Prof.C.NagaRaju YSRCE of YVU 29 CSE 9949218570 Construction of DFA for LR(0) Items $ I I1 2 + E T i I3 I I 4 i 5 I T 0 i ( I6 T ( I9 I I7 8 S E ) Prof.C.NagaRaju YSRCE of YVU 30 CSE 9949218570 Construction of LR(0) Parsing Table Input States Action Part (Terminals) Goto Part (Non- Terminals) i + ( ) $ E T 0 5 7 1 6 Shift 1 3 2 Shift 2 S→E$ Reduce 3 5 7 4 Shift 4 E→E+T Reduce 5 T→i Reduce 6 E→T Reduce 7 5 7 8 6 Shift 8 3 9 Shift 9 T→(E) Prof.C.NagaRaju YSRCE of YVU Reduce 31 CSE 9949218570 String Acceptance Input States Action Part (Terminals) Goto Part (Non- Terminals) i + ( ) $ E T 0 5 7 1 6 Shift 1 3 2 Shift 2 S→E$ Reduce 3 5 7 4 Shift 4 E→E+T Reduce 5 T→i Reduce 6 E→T Reduce 7 5 7 8 6 Shift 8 3 9 Shift 9 T→(E) Reduce Stack Input Action S0 i + (i + i) $ Shift S0 i S5 + (i + i) $ Reduce by T→i S0 T S6 + (i + i) $ Reduce by E→T S0 E S1 + (i + i) $ Shift Prof.C.NagaRaju YSRCE of YVU 32 S0 E S1 + S3 CSE (i9949218570 + i) $ Shift String Acceptance Input States Action Part (Terminals) Goto Part (Non- Terminals) i + ( ) $ E T 0 5 7 1 6 Shift 1 3 2 Shift 2 S→E$ Reduce 3 5 7 4 Shift 4 E→E+T Reduce 5 T→i Reduce 6 E→T Reduce 7 5 7 8 6 Shift 8 3 9 Shift 9 T→(E) Reduce Stack Input Action S0 E S1 + S3 ( i + i) $ shift S0 E S1 + S3 ( S7 i + i) $ shift