






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







05-0: Parsing
05-1: LR Parsing
05-2: LR Parsing Example (0) E′^ → E$ (1) E → E + T (2) E → T (3) T → T ∗ num (4) T → num
05-3: LR Parsing
05-4: LR Parsing Example
(0) E′ → E$ (1) E → E + T (2) E → T (3) T → T ∗ num (4) T → num
num + * $ E T 1 s2 g3 g 2 r(4) r(4) r(4) 3 s7 a 4 r(2) s5 r(2) 5 s 6 r(3) r(3) r(3) 7 s2 g 8 r(1) s(5) r(1) 05-5: LR Parsing
05-6: LR(0) Parsing
LR(0) parsers are the simplest of the LR parsers 05-7: LR Parsing Example (0) S′^ → S$ (1) S → AA (2) S → bc (3) A → baA (4) A → c 05-8: LR Parsing Example S′^ → S$ S → AA S → bc A → baA A → c a b c S′^ S′^ → S$ S′^ → S$ S S → bc S → AA S → AA A A → baA A → c
Not LL(1)! 05-9: LR(0) Items
05-10: LR(0) States & Transitions (0) S′^ → S$ (1) S → AA (2) S → bc (3) A → baA (4) A → c 05-11: LR(0) States & Transitions
05-16: Finding Transitions
05-17: Finding Transitions
05-18: Another LR(0) Example (0) E′^ → E$ (1) E → E + T (2) E → T (3) T → T * id (4) T → id 05-19: LR(0) States & Transitions
E’->. E$ E ->. E + T E ->. T T ->. T * id T ->. id
id
E
State 1
accept
$
id
E -> E + T. T -> T. * id
State 7
T -> T *. id
State 6 T -> T * id.
State 8
E’ -> E. $ E -> E. + T
State 2
T -> id.
State 4
E -> T. T -> T. * id
State 3
T
id E -> E +. T T ->. T * id T ->. id
State 5
T
05-20: LR(0) Parse Table id + * $ E T 1 s4 g2 g 2 s5 accept 3 r(2) r(2) r(2),s6 r(2) 4 r(4) r(4) r(4) r(4) 5 s4 g 6 s 7 r(1) r(1) r(1),s6 r(1) 8 r(3) r(3) r(3) r(3) 05-21: Shift-Reduce Conflict
05-22: Shift-Reduce Conflict
S → γ.
we should only reduce if the next terminal can follow S
05-23: SLR(1)
S’ -> S. $
State 2
S -> L = R.
State 10
R -> L.
State 9
L -> * R.
State 8
S -> L =. R R ->. L L ->. *R L ->. id
State 7
L -> *. R R ->. L L ->. * R L ->. id
State 5
S -> R.
State 4
S -> L. = R R -> L.
State 3
S’ ->. S$ S ->. L = R S ->. R L ->. *R L ->. id R ->. L
State 1
L -> id.
State 6
S accept
L
R
id *
id L
R
$
=
R
id
L
05-30: SLR(1) Parse Table id = * $ S L R 1 s6 s5 g2 g3 g 2 accept 3 r(5),s7 r(5) 4 r(2) 5 s6 s5 g9 g 6 r(4) r(4) 7 s6 s5 g9 g 8 r(3) r(3) 9 r(5) r(5) 10 r(1) 05-31: Why SLR(1) Fails S → L. = R R → L.
05-32: LR(1) Items
05-33: LR(1) Example
(4) L → id (5) R → L 05-34: LR(1) States and Transitions
id State 6 L -> id. =, $
State 1 S’ ->. S$S ->. L = R
State 2 S’ -> S. $
State 3 SR ->-> LL .. = R $$
S -> R. $ State 4
LR ->-> *. .L R =,=, $$ LL ->-> .. *id R =,=, $$
State 5
S -> L =. RR ->. L $$ L ->. *RL ->. id $$
State 7
L -> * R. =, $ State 8
State 9 R -> L. =, $ S -> L = R. $ State 10
Accept
L -> *. RR ->. L $$ L ->. * RL ->. id $$
State 12
R -> L. $ State 11 State 13 L -> id. $
L -> * R. $ State 14
S L R
$
=
id
R L
R * L id
id
R L
05-35: LR(1) Parse Table id = * $ S L R 1 s6 s5 g2 g3 g 2 accept 3 s7 r(5) 4 r(2) 5 s6 s5 g9 g 6 r(4) r(4) 7 s13 s12 g11 g 8 r(3) r(3) 9 r(5) r(5) 10 r(1) 11 r(5) 12 s13 s12 g11 g 13 r(4) 14 r(3) r(3) 05-36: More LR(1) Examples (0) S′^ → S$ (1) S → BC (2) S → b (3) B → bB (4) B → a (5) C → ǫ (6) C → cC 05-37: LR(1) States & Transitions
id State 6 L -> id. =, $
State 1 S’S ->-> .. LS$ = (^) R
State 2 S’ -> S. $
State 3 SR ->-> LL .. = R $$
State 4 S -> R. $
LR ->-> *. .L R =,=, $$ LL ->-> .. *id R =,=, $$
State 5
SR ->-> L. =L. R $$ LL ->-> .. *Rid $$
State 7
State 8 L -> * R. =, $
State 9 R -> L. =, $ State 10 S -> L = R. $
Accept
LR ->-> *. .L R $$ LL ->-> .. *id R $$
State 12
State 11 R -> L. $ State 13 L -> id. $
State 14 L -> * R. $
S L R
$
=
id
R L
R L *
id
id
R L
Can combine 5 & 12, 6 & 13, 8 & 14, 9 & 11 05-42: LALR Parser Example
id State 6- L -> id. =, $
State 1 S’ ->. S$
S’ -> S. $
State 2
S -> L. = R $ R -> L. $
State 3
S -> R. $
State 4
L -> *. R =, $ R ->. L =, $ L ->. * R =, $ L ->. id =, $
State 5-
S -> L =. R $ R ->. L $ L ->. *R $ L ->. id $
State 7
L -> * R. =, $
State 8-
R -> L. =, $
State 9-
S -> L = R. $
State 10
Accept
S L R
$
=
id
R
L
R * L id
05-43: LALR Parser Example id = * $ S L R 1 s6-13 s5-12 g2 g3 g 2 accept 3 s7 r(5) 4 r(2) 5-12 s6-13 s5-12 g9-11 g8- 6-13 r(4) r(4) 7 s6-13 s5-12 g9-11 g 8-14 r(3) r(3) 9-11 r(5) r(5) 10 r(1) 05-44: More LALR Examples
(1) S → Aa (2) S →bAc (3) S → Bc (4) S →bBa (5) A →d (6) B →d 05-45: More LALR Examples
S’ ->. S $ - S ->. Aa $ S ->. bAc $ S ->. Bc $ S ->. bBa $ A ->. d a B ->. d c
S’ -> S. $ -
S -> A. a $
S -> b. Ac $ S -> b. Ba $ A ->. d c S -> B. c $ B ->. d a
A -> d. a B -> d. c A -> d .B -> d.^ ca
S -> bA. c $
S -> bB. a $
S -> bAc. $
S -> bBa. $
State 1 State 4
State 5
State 6
State 2
State 3 (^) State 7
S -> Aa. $
State 8
State 12
State 10
State 9
State 11
d (^) B
S
A
b
d
a
A
B
c
a
05-46: More LALR Examples
S’ ->. S $ - S ->. Aa $ S ->. bAc $ S ->. Bc $ S ->. bBa $ A ->. d a B ->. d c
S’ -> S. $ -
S -> A. a $
S -> b. Ac $ S -> b. Ba $ A ->. d c S -> B. c $ B ->. d a
A -> d. a, c B -> d. a, c
S -> bA. c $
S -> bB. a $
S -> bAc. $
S -> bBa. $
State 1 State 4
State 5
State 6
State 2
State 3-
S -> Aa. $
State 8
State 12
State 10
State 9
State 11
d (^) B
S
A
b
d
a
A
B
c
a
05-47: More LALR Examples