Shiftreduce parser 1 2 3 Bottomup parsing Aho
Shift-reduce parser 1
2
3
Bottom-up parsing (Aho, Sethi, & Ullman p 195) • Example. S a A B e, A A b c | b , B d The input string a b b c d e can be reduced to S by the following steps: a b b c d e // possible choice b, d; chosen b a A b c d e // possible choice Abc, b, d; a. Ade // possible choice d; a A B e // possible choice a A B e ; S S⇒ a A B e ⇒ a A d e ⇒ a A b c d e ⇒ abbcde rm rm 4
Bottom-up parsing –CFSM—SLR(1) yangfy Example. S a A B e, A A b c, A b, B d (Aho, Sethi, & Ullman p 195) S a. ABe$ 0 a S a ABe$ A Abc A b b A b 1 A 2 3 S a. A Be$ B d A A bc d B b A Ab c 4 c A Abc 5 B d 6 S a. AB e$ 7 e S a. ABe $ $ 8 S a. ABe$ 9 5
Bottom-up parsing –CFSM—SLR(1) yangfy Action State Lookhead a 0 b c d e 2 R 3 3 S S 4 4. B d FOLLOW(A)={b, d} FOLLOW(B)={e} S R 2 6 R 4 7 S 9 2. A A b c 3. A b S 8 $ S 1 5 1. S a A B e A 6
Bottom-up parsing –CFSM—SLR(1) yangfy goto State A B 0 1 a b c d e $ 1 3 2 2 3 4 7 4 6 5 5 6 7 8 8 9 9 7
Bottom-up parsing yangfy Example. 1. S a A B e $, A A b c, A b, B d Stack Input Action 1 0 abbcde$ shift 2 01 bbcde$ shift 3 012 bcde$ R 3 4 013 bcde$ shift (1, A) = 3 5 0134 cde$ shift 6 01345 de$ R 2 7 013 de$ shift (1, A) = 3 8 0136 e$ R 4 9 0137 e$ shift (3, B) = 7 10 0 1 3 7 8 $ Accept 8
Bottom-up parsing –CFSM—SLR(1) yangfy Example. S a A B e, A A b c, A b, A , B d (example of shift-reduce conflict occurs in LR(0)) S a. ABe$ 0 a S a ABe$ A Abc A b 1 A S a. A Be$ B d A A bc 3 d B B d 6 S a. AB e$ 7 e b A Ab c 4 c A Abc 5 2 S a. ABe $ $ 8 S a. ABe$ 9 9
- Slides: 9