– 1 – CSCE 531 Spring 2006 Lecture 9 SLR Parse Table Construction Topics Topics SLR Parse Table Construction Sets of Items Closure(I) Readings: 4.7 Readings: 4.7 Homework: Test 1 – Feb 15 Homework: Test 1 – Feb 15 February 8, 2006 CSCE 531 Compiler Construction
36
Embed
– 1 – CSCE 531 Spring 2006 Lecture 9 SLR Parse Table Construction Topics SLR Parse Table Construction Sets of Items Closure(I) Readings: 4.7 Homework:
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
– 1 – CSCE 531 Spring 2006
Lecture 9 SLR Parse Table Construction
Lecture 9 SLR Parse Table Construction
Topics Topics SLR Parse Table Construction Sets of Items Closure(I)
Readings: 4.7Readings: 4.7
Homework: Test 1 – Feb 15Homework: Test 1 – Feb 15
February 8, 2006
CSCE 531 Compiler Construction
– 2 – CSCE 531 Spring 2006
Last TimeLast Time Panic mode error recovery in Predictive parsing Overview Bottom-Up Parsing Handles Shift-reduce parsing
As with LL(1) or Predictive Parsing table construction As with LL(1) or Predictive Parsing table construction we will use FIRST and FOLLOW. we will use FIRST and FOLLOW.
We will construct an automata for recognizing handles.We will construct an automata for recognizing handles.
This will be similar to subset construction, NFA This will be similar to subset construction, NFA DFADFA
We use “items” to represent partially matched We use “items” to represent partially matched productions.productions.
The sets of items are sort of the collection of The sets of items are sort of the collection of productions we are working on matching.productions we are working on matching.
Grammars Grammars For top-down we avoid left recursion and left factor For LR we avoid right recursion (in some cases we will ignore)
– 8 – CSCE 531 Spring 2006
ItemsItemsItemsItems
An item is a production with a “dot” somewhere on the right hand An item is a production with a “dot” somewhere on the right hand side.side.
Examples:Examples:
A A B C D E B C D E
Yields the following itemsYields the following items
A A . B C D E . B C D E
A A B . C D E B . C D E
A A B C . D E B C . D E
A A B C D . E B C D . E
A A B C D E . B C D E .
Also N Also N εε generates the itemgenerates the item N N . .
– 9 – CSCE 531 Spring 2006
Sets of Items: Closure fig 4.33Sets of Items: Closure fig 4.33Sets of Items: Closure fig 4.33Sets of Items: Closure fig 4.33
If J is a set of items for a grammar G, then closure(J) is the If J is a set of items for a grammar G, then closure(J) is the set of items constructed from J by the two rules:set of items constructed from J by the two rules:
1.1. Initially, every item in J is added to closure(J)Initially, every item in J is added to closure(J)
2.2. If A If A αα . B . Bββ is in closure(J) and B is in closure(J) and B ηη is a production is a production then add B then add B . . ηη
Apply this rule until no new items can be Apply this rule until no new items can be added to the set.added to the set.
Example:Example:
Assume DAssume D xyF | Bc and B xyF | Bc and BaB | Dd are the productions aB | Dd are the productions then then
If J is a set of items and X is a grammar symbol then If J is a set of items and X is a grammar symbol then
GOTO(J, X) is the closure of the set of all items of the GOTO(J, X) is the closure of the set of all items of the form [Aform [AααX .X .ββ] such that [A] such that [Aαα . X . Xββ] is an item in J.] is an item in J.
GOTO(J, X) = closure ({AGOTO(J, X) = closure ({AααX .X .ββ | A | Aαα . X . Xββ is in J} ) is in J} )
Example given E Example given E E + E | E * E | id E + E | E * E | id
If J = { [E If J = { [E E . + E ], [E E . + E ], [E . id] } then . id] } then
GOTO (J, +) = closure({[E GOTO (J, +) = closure({[E E + . E ] } ) E + . E ] } )
= {[E = {[E E + . E ], … E + . E ], …
– 11 – CSCE 531 Spring 2006
Augmentation and Kernel ItemsAugmentation and Kernel ItemsAugmentation and Kernel ItemsAugmentation and Kernel Items
To facilitate recognition of the sucessful end of a To facilitate recognition of the sucessful end of a parse we will “augment the grammar” adding parse we will “augment the grammar” adding
1.1. a new start symbol S’, anda new start symbol S’, and
2.2. a new production S’ a new production S’ S S
Items with the dot not at the left end and the initial Items with the dot not at the left end and the initial item S’item S’.S are called kernel items..S are called kernel items.
All others will be referred to as non-kernel. All others will be referred to as non-kernel.
Note non-kernel items will have the dot on the left.Note non-kernel items will have the dot on the left.
– 12 – CSCE 531 Spring 2006
LR(0) Sets-of-Items ConstructionLR(0) Sets-of-Items ConstructionLR(0) Sets-of-Items ConstructionLR(0) Sets-of-Items Construction
Figure 4.34Figure 4.34
Procedure items(G’)Procedure items(G’)
BeginBegin
C = { closure({ S’C = { closure({ S’ . S} ) } . S} ) }
repeat repeat
for each set of items J in C and each grammar symbol Xfor each set of items J in C and each grammar symbol X
such that GOT(J, X) is nonempty and not in Csuch that GOT(J, X) is nonempty and not in C
add GOT(J, X) to Cadd GOT(J, X) to C
until no more sets of items can be added to Cuntil no more sets of items can be added to C
The set of prefixes of right sentential forms that can The set of prefixes of right sentential forms that can appear on the stack are called appear on the stack are called viable prefixesviable prefixes..
Alternately it is one that is capable of being extended to Alternately it is one that is capable of being extended to a handle.a handle.
Alternately it is a prefix of a right sentential form that Alternately it is a prefix of a right sentential form that does not extend beyond the right end of the does not extend beyond the right end of the rightmost handle.rightmost handle.
An item [ AAn item [ Aββ11 . . ββ2 2 ] is valid for a viable prefix ] is valid for a viable prefix αβαβ if if there is a derivationthere is a derivation
S’ S’ αα A w A w α βα β11 ββ2 2 ww
An item may be valid for many viable prefixesAn item may be valid for many viable prefixes
SLR table construction Alg 4.8SLR table construction Alg 4.8SLR table construction Alg 4.8SLR table construction Alg 4.8
1.1. Augment G to obtain G’.Augment G to obtain G’.
2.2. Construct C = {IConstruct C = {I00, I, I11, …I, …Inn} the LR(0) items for G’.} the LR(0) items for G’.
3.3. State i corresponds to IState i corresponds to Iii. The parsing actions for state i . The parsing actions for state i are determined as follows:are determined as follows:
a. If [ Aα . a β ] is in Ii and GOTO(Ii, a) = Ij then set action[i, a] = “shift and goto state j” or just “shift j”
b. If [ Aα . ] is in Ii then set action[i, a] = “reduce Aα” for all a in FOLLOW(A). (here A !=S’)
c. If [ S’S . ] is in Ii then set action[i, $] = “accept.”
– 17 – CSCE 531 Spring 2006
Examples of SLR Parse Table ConstructionExamples of SLR Parse Table Construction