2

Click here to load reader

Building LR(1) itemsets , LR(1) and LALR parse table scourses.washington.edu/css448/zander/Notes/LRandLALR.pdf · Building LR(1) itemsets , LR(1) and LALR parse table s A, S, X: non-terminals

  • Upload
    vandat

  • View
    212

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Building LR(1) itemsets , LR(1) and LALR parse table scourses.washington.edu/css448/zander/Notes/LRandLALR.pdf · Building LR(1) itemsets , LR(1) and LALR parse table s A, S, X: non-terminals

Building LR(1) itemsets, LR(1) and LALR parse tables

A, S, X: non-terminals x,y, α, β: string of terminals and/or non-terminals C: one terminal or one non-terminal

start: [S --> .w , $] is the item associated with the start state.

read: Starting a new state (reading on one terminal or non-terminal, C) comes from [A --> x.Cy , w] then new state includes [A --> xC.y , w] .

complete: if [A --> x.Xα , u] is an item, then completing on X gives the item(s) [X --> .β , z] where z є FIRST(αu) .

Consider the augmented grammar G’: 0. S’ --> S$ 1. S --> CC 2. C --> eC 3. C --> d

LR(1) Itemsets State Item Notes

I0 S’ --> .S$ , $ complete on S; read on S goes to state 1 S --> .CC , $ complete on C; FIRST($$) is $ ; read on C goes to state 2 C --> .eC , e|d FIRST(C$) is e|d ; read on ‘e’ goes to state 3 C --> .d , e|d FIRST(C$) is e|d ; read on ‘d’ goes to state 4

I1 S’ --> S.$ , $ accept

I2 S --> C.C , $ read on C goes to state 5 C --> .eC , $ FIRST(λ$) is $; read on ‘e’ goes to state 6

C --> .d , $ FIRST(λ$) is $; read on ‘d’ goes to state 7

I3 C --> e.C , e|d read on C goes to 8 C --> .eC , e|d FIRST(λ(e|d)) is e|d; read on ‘e’ is to state 3 again

C --> .d , e|d FIRST(λ(e|d)) is e|d; read on ‘d’ is to state 4 again

I4 C --> d. , e|d reduce on rule 3

I5 S --> CC. , $ reduce on rule 1

I6 C --> e.C , $ read on C goes to 9 C --> .eC , $ FIRST(λ$) is $; read on ‘e’ is to state 6 again

C --> .d , $ FIRST(λ$) is $; read on ‘d’ is to state 7 again

I7 C --> d. , $ reduce on rule 3

I8 S --> eC. , e|d reduce on rule 2

I9 S --> eC. , $ reduce on rule 2

Page 2: Building LR(1) itemsets , LR(1) and LALR parse table scourses.washington.edu/css448/zander/Notes/LRandLALR.pdf · Building LR(1) itemsets , LR(1) and LALR parse table s A, S, X: non-terminals

The SLR parse table:

e d $ S C 0 s3 s4 1 2 1 accept 2 s3 s4 5 3 s3 s4 6 4 r3 r3 r3 5 r1 6 r2 r2 r2

The LR(1) parse table (same as before, except when you do a reduce – items with dot at end – instead of using the whole FOLLOW set, only use symbols after the comma):

e d $ S C 0 s3 s4 1 2 1 accept 2 s6 s7 5 3 s3 s4 8 4 r3 r3 5 r1 6 s6 s7 9 7 r3 8 r2 r2 9 r2

To create LALR table, merge states by their core sets (for state use either just first number, or use all the original state numbers to create a unique longer number):

e d $ S C 0 s36 s47 1 2 1 accept 2 s36 s47 5 36 s36 s47 89 47 r3 r3 r3 5 r1 89 r2 r2 r2