Plan for Today and Thursday Bottom-Up Parsing with LR parse tables – JavaCUP debug information – using an LR parser Building the LR Parsing Table for LR(0) – items, closure, and goto, oh my! – why does it need to know input token if LR(0)? 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 LR Parse Tables 1 Example LR Parse Table [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 LR Parse Tables 2
Building the LR Parse Table for LR(0) [0] S -> ( S ) [1] S’ -> S EOF [2] S -> ID CS453 Lecture Building LR Parse Tables 3 LR(0) states for nested parens example CS453 Lecture Building LR Parse Tables 4
Building the Table from the State Diagram 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 LR Parse Tables 5 Building the LR Parse Table for LR(0), another example (0) S’ -> E $ (1) E -> E || B (2) E -> B (3) B -> t (4) B -> f CS453 Lecture Building LR Parse Tables 6
FIRST and FOLLOW sets FIRST( gamma ) – gamma is a string of terminals and nonterminals – FIRST(gamma) is any terminals that can start a string derived from gamma nullable(X) – X is a nonterminal – nullable(X) is true if X can derive the empty string FOLLOW( X ) – X is a nonterminal – FOLLOW(X) is all terminals that can immediately follow X in strings generated from the grammar CS453 Lecture Building LR Parse Tables 7 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 LR Parse Tables 8
Example LR Parse Table [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 LR Parse Tables 9 Pushdown Automata for Grammar CS453 Lecture Building LR Parse Tables 10
Recommend
More recommend