LR1 parsing Given the following augmented grammar 0

  • Slides: 4
Download presentation
LR(1) parsing Given the following augmented grammar 0 S′ S C C → →

LR(1) parsing Given the following augmented grammar 0 S′ S C C → → S CC a. C d First S’ = { a, d } S = { a, d } C = { a, d } S′’ → ⋅ S, $ S → ⋅ C C, C → ⋅ a C, C → ⋅ d, $ a d the starting point, $ in follow S’. Before S, write S rules with follow empty $. Before C, write C rules with follow first C$ which is a $ and d $, which is a and d a C → ⋅ d, d This can be written shorter as: S′’ → ⋅ S, S → ⋅ C C, C → ⋅ a C, C → ⋅ d, $ $ a, d

LR(1) 0 S′’ → ⋅ S S→⋅CC C→⋅a. C C→⋅d a d 1 $

LR(1) 0 S′’ → ⋅ S S→⋅CC C→⋅a. C C→⋅d a d 1 $ S $ a , d 3 C→a⋅C C → ⋅ a. C C→⋅d S’ → S ⋅ C C 2 S→C⋅C C→⋅a. C C→⋅d a, d $ 8 C → a C⋅ C $ $ $ d a, d 5 S → C C⋅ 6 a C → a⋅ C C→⋅a. C C→⋅d 7 C → d⋅ $ 9 $ $ $ C C → a. C⋅ a d $ a, d a d 4 C → d⋅ a, d Comments: For item 0, S → ⋅ C C $ generates C’s rules with follow item the first of C$, the first of C is a and d so The follow item is a$, and d$, which is a, and d. For item set 3, C → a ⋅ C a, d generates C’s rules with follow item the first of empty a, and the first of empty d, which is a, d. Similarly, for other item sets. $

Parsing Table – build table from FSM In general S ->. a shift S->a.

Parsing Table – build table from FSM In general S ->. a shift S->a. reduce on the follow in the item set, not the whole follow set ACTION 0 GOTO a d S 3 S 4 1 $ S C 1 2 ACC 2 S 6 S 7 5 3 S 4 8 4 C→d 5 6 S → CC S 6 S 7 7 8 9 9 C→d C → a. C

Parse off the table, just like before $0 $0 a 3 a 3 a

Parse off the table, just like before $0 $0 a 3 a 3 a 3 d 4 $0 a 3 a 3 a 3 C 8 $0 a 3 C 8 $0 C 2 d 7 $0 C 2 C 5 $0 S 1 ACCEPT aaadd$ d$ d$ d$ $ let’s parse this 0, a – shift 3 3, d – shift 4 4, d – reduce d to C 3, C – go to 8 8, d – reduce a. C to C 0, C – go to 2 2, d – shift 7 7, $ - reduce d to C 2, C – go to 5 5, $ - reduce CC to S 0, S – go to 1 this is always our accept configuration