Plan for Today SLR (Simple LR) – Problem with LR(0) – SLR modification, follow sets strike again Building the LR Parsing Table for LR(1) – First and Follow sets – building the table Debugging shift/reduce and reduce/reduce errors LR parse tables and pushdown automata CS453 Lecture Building SLR and LR(1) Parse Tables 1 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 Lecture Building SLR and LR(1) Parse Tables 2
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 T* id$ S T*id $ R: F id T*F $ R: T T*F T $ R:E E $ accept CS453 Lecture Building SLR and LR(1) Parse Tables 3 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) Let’s 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! !!!We!need!to!build!the!transiAon!diagram! !!!!!!!!!!!!!!!!!!!!!and!follow!sets!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!to!decide!where!to!put!the!reduce!acAons!in!the!SLR!table CS453 Lecture Building SLR and LR(1) Parse Tables 4
More realistic example: Expression grammar E E+T | T T T*F | F F (E) | id S E$ input: a*(b+c)$ ! !Stack!!!!!!!!input!!!!!!ac+on! !!!Stack!!!!!!!!input!!!!ac+on! !!!!!!!!!!!!!!!!a*(b+c)$!!S! !!!!T*(E+!!!!!!!c)$!!!!!!!S! !!!a!!!!!!!!!!*(b+c)$!!!!R:!F id! !!!!T*(E+c!!!!!)$!!!!!!!!!R:!F id! !!!F!!!!!!!!!!*(b+c)$!!!!R:!T F! !!!!T*(E+F!!!!!)S!!!!!!!!!R:!T F! !!! T!!!!!!!!!!*(b+c)$!!!!S! !!!!T*(E+T!!!!!)$!!!!!!!!!R:!E E+T! !!!T*!!!!!!!!(b+c)$!!!!!!S! !!!!T*(E!!!!!!!!!)$!!!!!!!!!S! !!!T*(!!!!!!!b+c)$!!!!!!!S! !!!!T*(E)!!!!!!!!$!!!!!!!!!!R:!F (E)! !!!T*(b!!!!!+c)$!!!!!!!!!R:!F id! !!!!T*F!!!!!!!!!!!$!!!!!!!!!!R:!T T*F! !!!T*(F!!!!!+c)$!!!!!!!!!R:!T F! !!!!T!!!!!!!!!!!!!!!$!!!!!!!!!!R:!E T! !!!T*(T!!!!!+c)$!!!!!!!!!R:!E T! !!!!E!!!!!!!!!!!!!!!$!!!!!!!!!accept! !!!T*(E!!!!!+c)$!!!!!!!!!S! S E$ T$ T*F$ T*(E)$ T*(E+T)$ T*(E+F)$ !T*(E+id)$ T*(T+id)$ !! T*(F+id)$ T*(id+id)$ !F*(id+id)$ id*(id+id)$ ! Rightmost!deriva3on!in!reverse! CS453 Lecture Building SLR and LR(1) Parse Tables 5 State Transition Diagram !0:!S E$!!!1:E E+T!!!2:E T!!!3:T T*F!!!4:T F!!!5:F (E)!!!6:F id CS453 Lecture Building SLR and LR(1) Parse Tables 6
!!!0:!S E$!!!1:E E+T!!!2:E T!!!3:T T*F!!!4:T F!!!5:F (E)!!!6:F id ! !!!!SLR!parse!table!(reduces!only!for!follows)! Stack!!!!!!!!!!input!!!!!!!!!!!ac+on! 0!!!!!!!!!!!!!!!!!!a*(b+c)$!!!!s5! 0a5!!!!!!!!!!!!!!!!*(b+c)$!!!!r6:!F id! State!!!!!!id!!!!!+!!!!!*!!!!!(!!!!!)!!!!!$!!!!!E!!!!!T!!!!!F!!! 0F3!!!!!!!!!!!!!!!!*(b+c)$!!!!r4:!T F! !!!!0!!!!!!!!!s5!!!!!!!!!!!!!!!!!!s4!!!!!!!!!!!!!!!!g1!!!g2!!!g3! 0T2!!!!!!!!!!!!!!!!*(b+c)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5!!!!!!!!!r5!!!r5! 0T2!!!!!!!!!!!!!!!!!!!!!!!!!!!!!$!!!!r2:!E T!! 0E1!!!!!!!!!!!!!!!!!!!!!!!!!!!!!$!!!!a!!!!!!!!!!!!!!!! CS453 Lecture Building SLR and LR(1) Parse Tables 7 LR(1) Parse Tables Sometimes lookahead is necessary – When in a particular state want to shift or reduce based on the lookahead token(s) for an item. – Will use FIRST and FOLLOW information to track these overheads Example to show need for LR(1) S -> epsilon | b S CS453 Lecture Building SLR and LR(1) Parse Tables 8
FIRST and FOLLOW Sets Pretty useful – Both are needed to construct a predictive parser – For LR(1) parsers, used to determine what tokens can start the string after a nonterminal – Extended item: (A -> alpha . X beta, z) – A is a nonterminal – alpha and beta are strings of terminals and nonterminals – X is a terminal or nonterminal – z is a lookahead terminal/token – Want to know FIRST of string “beta z” CS453 Lecture Building SLR and LR(1) Parse Tables 9 Review First and Follow S -> ( S ) | ID | S’ -> S $ CS453 Lecture Building SLR and LR(1) Parse Tables 10
LR(1) Closure, Goto, and Reduce Modifications Closure(I): // state I repeat for any item A α . X β , z for any X γ I+= X . γ , w where w in FIRST (beta z) until I does not change return I Goto(I, X): // state I , symbol X Reduce(T): if X==$ return {} // no gotos for $ R = {} J = {} // new state for each state I in T for any item A α . X β , z in I for each item A α . , z J+= A α X . β , z R += (I, A α ) on terminal z return Closure(J) // close it CS453 LR(1) Parse Tables 11 Building the LR Parse Table for LR(1) (0) S’ -> E $ (1) E -> T + E (2) E -> T (3) T -> x Symbol FIRST x x + + T x E x S x CS453 Lecture Building SLR and LR(1) Parse Tables 12
Producing the LR(1) Parse Table (1) Edge X: (I, J) where X is a terminal – Put shift J at (I,X) (2) Edge X: (I, J) where X is a non-terminal – Put goto J at (I, X) (3) If S’ -> S . $ in I – Put accept at (I, $) (4) Only change from LR(0) table creation – (k) (A -> gamma . , w) – Reduce k for lookahead token w CS453 Lecture Building SLR and LR(1) Parse Tables 13 Building the LR Parse Table for LR(1) Action Goto (0) S’ -> E $ (1) E -> T + E State x + $ T E (2) E -> T 0 s3 g2 g1 (3) T -> x 1 accept Symbol FIRST 2 s4 r2 x x 3 r3 r3 + + 4 s3 g2 g5 T x 5 r1 E x S x CS453 Lecture Building SLR and LR(1) Parse Tables 14
JavaCUP Debugging Commands for debugging and general approach Look at zero+ lists example Talk about missing paren in while example CS453 Lecture Building SLR and LR(1) Parse Tables 15 Example LR Parse Table to show DFA correspondence [0] S -> ( S ) [1] S’ -> S EOF [2] S -> ID Action Goto State ( ) $/EOF 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 SLR and LR(1) Parse Tables 16
Pushdown Automata for Grammar CS453 Lecture Building SLR and LR(1) Parse Tables 17
Recommend
More recommend