LR1 parsing Given the following augmented grammar 0

  • Slides: 7
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. $

LALR(1) modify the FSM to represent LALR(1) FSM by combining states with common core

LALR(1) modify the FSM to represent LALR(1) FSM by combining states with common core 0 S′’ → ⋅ S S→⋅CC C→⋅a. C C→⋅d 1 $ S $ a , d S’ → S ⋅ C 36 C→a⋅C C → ⋅ a. C C→⋅d C $ 2 S→C⋅C C→⋅a. C C→⋅d a a $ 5 S → C C⋅ $ $ $ 89 $, a, d C C → a C⋅ $, a, d d $, a, d a d 47 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 LALR(1) ACTION 0 GOTO a d S

Parsing Table – build table from FSM LALR(1) ACTION 0 GOTO a d S 36 S 4 1 $ S C 1 2 ACC 2 S 36 S 47 5 36 S 47 89 47 C→d 5 89 C→d S → CC C → a. C

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

Parse off the table, just like before $0 $0 a 36 a 36 a 36 d 47 $0 a 36 a 36 C 89 $0 a 36 C 89 $0 C 2 d 47 $0 C 2 C 5 $0 S 1 ACCEPT aaadd$ d$ d$ d$ $ $ let’s parse this 0, a – shift 36 36, d – shift 47 47, d – reduce d to C 36, C – go to 89 89, d – reduce a. C to C 36, C – go to 89 89, D – reduce a. C to C 0, C – go to 2 2, d – S 47 47, $ - reduce d to C 2, C – go to 5 5, $ - reduce CC to S 0, S – go to 1

LALR(1) vs LR(1) for parsing capability Consider shift/reduce – if LR(1) does not have

LALR(1) vs LR(1) for parsing capability Consider shift/reduce – if LR(1) does not have a conflict, the item sets have to look like: S -> a. A. b m, n this is a shift D -> c. q this is a reduce– no conflict, above shift on b, here reduce on q Now another LR(1) item set with a common core S -> a. A. b p D -> c. r again no conflict, shift on b, reduce on r We can now merge the two item sets combining the follow items as is done for LALR(1) parsing. We get S -> a. A. b m, n, p D -> c. q, r Still no conflict. SO, if the LR(1) parsing machine did not have a shift/reduce conflict, neither will the LALR(1) machine. LALR(1) is just as powerful.

LALR(1) vs LR(1) for parsing capability Consider reduce/reduce in LR(1) S -> a. m

LALR(1) vs LR(1) for parsing capability Consider reduce/reduce in LR(1) S -> a. m A -> b. n both are reduce, first on m, second on n Now a second LR(1) with a common core S -> a. n S -> b. p both are reduce, first on n, second on p We can now merge the two item sets combining the follow items as is done for LALR(1) parsing. S -> a. m, n S -> b. n, p We now have a reduce/reduce conflict in the LALR(1) machine that didn’t exist in the LR(1) machine. Hence, LALR(1) isn’t quite as powerful as LR(1), but the follow items aer still, hopefully, a subset of the whole follow set, so LALR(1) is still more powerful than SLR(1). Now, consider the size of the FSM between LR(1) and LALR(1). We just saw an example of the machine reducing by 1/3. That’s a lot considering the few states. How many states will there be in an LALR(1) machine compared to SLR(1) which is equal to LR(0)? EQUAL!!!!! So, LALR(1) is relatively powerful which being a minimal parser. BISON, up to about 6 years ago generated a LALR(1) parser.