Plan for Today SLR (Simple LR) Problem with LR(0) SLR - - PDF document

plan for today
SMART_READER_LITE
LIVE PREVIEW

Plan for Today SLR (Simple LR) Problem with LR(0) SLR - - PDF document

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


slide-1
SLIDE 1

CS453 Lecture Building SLR and LR(1) Parse Tables 1

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

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: E E+T E  T T  T*F In state 1: we reduce (ET.) AND we shift (TT . *F) What should we do?

CS453 Lecture Building SLR and LR(1) Parse Tables 2

E.E+T E.T T.T*F T ET. TT . *F 1 * TT * . F 2

slide-2
SLIDE 2

LR(0) shift reduce conflict

We can resolve the conflict by looking at a right most derivation: ETT*FT*idF*idid*id Stack input action id*id$ S id *id$ R: Fid F *id$ R: FT T *id$ S We should shift T* id$ S T*id $ R: Fid T*F $ R: TT*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:!SE$!!!1:EE+T!!!2:ET!!!3:TT*F!!!4:TF!!!5:F(E)!!!6:Fid! !!!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

slide-3
SLIDE 3

More realistic example: Expression grammar EE+T | T TT*F | F F (E) | id SE$ input: a*(b+c)$ !

!Stack!!!!!!!!input!!!!!!ac+on! !!!!!!!!!!!!!!!!a*(b+c)$!!S! !!!Stack!!!!!!!!input!!!!ac+on! !!!!T*(E+!!!!!!!c)$!!!!!!!S! !!!!T*(E+c!!!!!)$!!!!!!!!!R:!Fid! !!!!T*(E+F!!!!!)S!!!!!!!!!R:!TF! !!!!T*(E+T!!!!!)$!!!!!!!!!R:!EE+T! !!!!T*(E!!!!!!!!!)$!!!!!!!!!S! !!!!T*(E)!!!!!!!!$!!!!!!!!!!R:!F(E)! !!!!T*F!!!!!!!!!!!$!!!!!!!!!!R:!TT*F! !!!!T!!!!!!!!!!!!!!!$!!!!!!!!!!R:!ET! !!!!E!!!!!!!!!!!!!!!$!!!!!!!!!accept!

SE$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!

!!!a!!!!!!!!!!*(b+c)$!!!!R:!Fid! !!!F!!!!!!!!!!*(b+c)$!!!!R:!TF! !!!T!!!!!!!!!!*(b+c)$!!!!S! !!!T*!!!!!!!!(b+c)$!!!!!!S! !!!T*(!!!!!!!b+c)$!!!!!!!S! !!!T*(b!!!!!+c)$!!!!!!!!!R:!Fid! !!!T*(F!!!!!+c)$!!!!!!!!!R:!TF! !!!T*(T!!!!!+c)$!!!!!!!!!R:!ET! !!!T*(E!!!!!+c)$!!!!!!!!!S!

CS453 Lecture Building SLR and LR(1) Parse Tables 5

State Transition Diagram

!0:!SE$!!!1:EE+T!!!2:ET!!!3:TT*F!!!4:TF!!!5:F(E)!!!6:Fid

CS453 Lecture Building SLR and LR(1) Parse Tables 6

slide-4
SLIDE 4

!!!0:!SE$!!!1:EE+T!!!2:ET!!!3:TT*F!!!4:TF!!!5:F(E)!!!6:Fid!

!!!!SLR!parse!table!(reduces!only!for!follows)! State!!!!!!id!!!!!+!!!!!*!!!!!(!!!!!)!!!!!$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(4E8!!!!!!!!!!+c)$!!!!!s6! 0T3*7(4E8+6!!!!!!!!!c)$!!!!s5! 0T3*7(4E8+6c5!!!!!!!)$!!!!r6:!Fid! 0T3*7(4E8+6F3!!!!!!!)$!!!!r4:!TF! 0T3*7(4E8+6T9!!!!!!!)$!!!!r1:!EE+T! 0T3*7(4E8!!!!!!!!!!!!!!!!)$!!!!S11! 0T3*7(4E8)11!!!!!!!!!!!$!!!!r5:!F(E)! 0T3*7F10!!!!!!!!!!!!!!!!!!$!!!!r3:!TT*F! 0T2!!!!!!!!!!!!!!!!!!!!!!!!!!!!!$!!!!r2:!ET!! 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

slide-5
SLIDE 5

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

slide-6
SLIDE 6

LR(1) Closure, Goto, and Reduce Modifications

CS453 LR(1) Parse Tables 11

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 if X==$ return {} // no gotos for $ J = {} // new state for any item Aα . Xβ, z in I J+= AαX . β, z return Closure(J) // close it Reduce(T): R = {} for each state I in T for each item Aα . , z R += (I, Aα ) on terminal z

CS453 Lecture Building SLR and LR(1) Parse Tables 12

(0) S’ -> E $ (1) E -> T + E (2) E -> T (3) T -> x

Building the LR Parse Table for LR(1)

Symbol FIRST x x + + T x E x S x

slide-7
SLIDE 7

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 CS453 Lecture Building SLR and LR(1) Parse Tables 14

(0) S’ -> E $ (1) E -> T + E (2) E -> T (3) T -> x

Building the LR Parse Table for LR(1)

Symbol FIRST x x + + T x E x S x Action Goto State x + $ T E s3 g2 g1 1 accept 2 s4 r2 3 r3 r3 4 s3 g2 g5 5 r1

slide-8
SLIDE 8

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 CS453 Lecture Building SLR and LR(1) Parse Tables 16

Example LR Parse Table to show DFA correspondence

[0] S -> ( S ) [1] S’ -> S EOF [2] S -> ID

Action Goto State ( ) $/EOF ID S s3 s1 2 1 r2 r2 r2 r2 2 accept 3 s3 s1 4 4 s5 5 r0 r0 r0 r0

slide-9
SLIDE 9

CS453 Lecture Building SLR and LR(1) Parse Tables 17

Pushdown Automata for Grammar