View
214
Download
0
Embed Size (px)
Citation preview
– 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
Today’s Lecture Today’s Lecture Sets of Items / Closure / GOTO (J, X)
LR(0) sets of items construction SLR parser table construction
Homework: Homework: LL(1) table for core (pdf email handout) grammar
Test 1 Feb 15 !!! Reference: ξ is the greek letter Xi
http://www.mathacademy.com/pr/prime/articles/greek/index.asp
– 3 – CSCE 531 Spring 2006
Slides in LectureSlides in Lecture
ReviewReview
1.1. Model of an LR parserModel of an LR parser
2.2. LR Parse table LR Parse table (Expressions)(Expressions)
3.3. Constructing SLR Parse Constructing SLR Parse TablesTables
4.4. Sets of Items / ClosureSets of Items / Closure
5.5. ExampleExample
6.6. Goto Operation and exampleGoto Operation and example
7.7. Canonical LR(0) sets-of-Canonical LR(0) sets-of-items (fig 4.35)items (fig 4.35)
8.8. Valid Items/Viable prefixesValid Items/Viable prefixes
9.9. SLR table constructionSLR table construction
10.10. ExampleExample
11.11. Example 4.38Example 4.38
12.12. Example 4.39Example 4.39
13.13. Bison/Flex Overview pictureBison/Flex Overview picture
14.14. Bison specification filesBison specification files
– 4 – CSCE 531 Spring 2006
Recall - Model of an LR ParserRecall - Model of an LR Parser
aa11 …… aaii …… aann$$
ssmm
XXmm
ssm-1m-1
XXm-1m-1
……
ss00
Stack
input
outputLR ParsingProgram
Parsing Table
Action goto
– 5 – CSCE 531 Spring 2006
LR Parsing AlgorithmLR Parsing Algorithm
– 6 – CSCE 531 Spring 2006
Expression LR-Parsing Table fig 4.31Expression LR-Parsing Table fig 4.31
StateState ActionAction gotogoto
IdId ++ ** (( )) $$ EE TT FF
00 S5S5 S4S4 11 22 33
11 S6S6 acceptaccept
22 R2R2 S7S7 R2R2
33 R4R4 R4R4 R4R4
44 S5S5 S4S4 88 22 33
55 R6R6 R6R6 R6R6
66 S5S5 S4S4 99 33
77 S5S5 S4S4 1010
88 S6S6 S11S11
99 R1R1 S7S7 R1R1 R1R1
1010 R3R3 R3R3 R3R3 R3R3
1111 R5R5 R5R5 R5R5 R5R5
– 7 – CSCE 531 Spring 2006
Constructing SLR Parse TablesConstructing SLR Parse TablesConstructing SLR Parse TablesConstructing SLR Parse Tables
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
Closure(AClosure(ABC.DE) = {ABC.DE) = {ABC.DE, DBC.DE, D.xyF, D.xyF, D . Bc, . Bc,BB.aB, B.aB, B.Dd }.Dd }
– 10 – CSCE 531 Spring 2006
Sets-of-items GOTO(J, X)Sets-of-items GOTO(J, X)Sets-of-items GOTO(J, X)Sets-of-items GOTO(J, X)
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
endend
– 13 – CSCE 531 Spring 2006
Examples LR(0) sets-of-itemsExamples LR(0) sets-of-itemsExamples LR(0) sets-of-itemsExamples LR(0) sets-of-items
Grammar 4.35 generates LR(0) items in fig 4.35 (p 225).Grammar 4.35 generates LR(0) items in fig 4.35 (p 225).
Example 4.39 (p229)Example 4.39 (p229)
ExampleExample
D D T ; | T R ; D T ; | T R ; D
T T int | float | char int | float | char
R R id | id [ const ] | * id id | id [ const ] | * id
First augment the grammar with S’ First augment the grammar with S’ D D
Then …Then …
– 14 – CSCE 531 Spring 2006
Valid Items/Viable prefixesValid Items/Viable prefixesValid Items/Viable prefixesValid Items/Viable prefixes
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
– 15 – CSCE 531 Spring 2006
Valid Items Parsing ActionsValid Items Parsing Actions
Valid items indicate possible parsing actionsValid items indicate possible parsing actions
If A If A ββ . Is valid . Is valid the action is reduce by A the action is reduce by A ββ
If [ AIf [ Aββ11 . . ββ2 2 ] is valid ] is valid the action is shift. the action is shift.
Contradictory actions yield conflicts.Contradictory actions yield conflicts.
– 16 – CSCE 531 Spring 2006
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
Example Grammar 4.42Example Grammar 4.42
S’ S’ S S
S S C C C C
C C c C | d c C | d
GrammarGrammar Sets-of-ItemsSets-of-Items Parse TableParse Table
Example 4.39, pp 229Example 4.39, pp 229 Fig 4.37, pp 229Fig 4.37, pp 229 Conflict bottom pp229Conflict bottom pp229
Example 4.38, pp 228Example 4.38, pp 228 Fig 4.35, pp 225Fig 4.35, pp 225 Fig 4.31, pp 219Fig 4.31, pp 219
– 18 – CSCE 531 Spring 2006
– 19 – CSCE 531 Spring 2006
LR(1) ParsersLR(1) Parsers
A table-driven LR(1) parser looks likeA table-driven LR(1) parser looks like
Tables Tables cancan be built by handbe built by hand
However, this is a perfect task to automateHowever, this is a perfect task to automate
ScannerTable-driven
Parser
ACTION & GOTOTables
ParserGenerator
sourcecode
grammar
IR
– 20 – CSCE 531 Spring 2006
YACCYACC
Yet Another Compiler Compiler Stephen Johnson 1976Yet Another Compiler Compiler Stephen Johnson 1976
Takes grammar specification and generates the Takes grammar specification and generates the Action and GOTO tablesAction and GOTO tables
– 21 – CSCE 531 Spring 2006
YACC Format and UsageYACC Format and Usage
First Bison = new and improved YACCFirst Bison = new and improved YACC
YACC FormatYACC Format
Definitions sectionDefinitions section
%%%%
productions / semantic actions sectionproductions / semantic actions section
%%%%
routinesroutines
– 22 – CSCE 531 Spring 2006
Example (grammar & sets)Example (grammar & sets)Simplified, Simplified, rightright recursive expression grammar recursive expression grammar
Is this what we want?Is this what we want?
Goal ExprExpr Term – ExprExpr TermTerm Factor * Term Term FactorFactor ident
– 23 – CSCE 531 Spring 2006
Simple0.y in web/Examples/SimpleYaccSimple0.y in web/Examples/SimpleYacc
%token DIGIT%token DIGIT
%%%%
line : expr '\n' line : expr '\n'
;;
expr : expr '+' term expr : expr '+' term
| term| term
;;
term : term '*' factor term : term '*' factor
| factor| factor
;;
factor : '(' expr ')' factor : '(' expr ')'
| DIGIT| DIGIT
;;
%%%%
– 24 – CSCE 531 Spring 2006
bison simple0.ybison simple0.y
deneb> deneb>
deneb> ls -lrtdeneb> ls -lrt
……
--rw-r--r-- 1 matthews faculty 28499 Jun 30 12:04 simple0.tab.crw-r--r-- 1 matthews faculty 28499 Jun 30 12:04 simple0.tab.c
deneb> wc simple0.tab.c deneb> wc simple0.tab.c
1084 4111 28499 simple0.tab.c1084 4111 28499 simple0.tab.c
– 25 – CSCE 531 Spring 2006
gcc simple0.tab.c -lygcc simple0.tab.c -ly
Undefined first referencedUndefined first referenced
symbol in filesymbol in file
yylex /var/tmp//ccW88jE5.oyylex /var/tmp//ccW88jE5.o
ld: fatal: Symbol referencing errors. No output written to ld: fatal: Symbol referencing errors. No output written to a.outa.out
collect2: ld returned 1 exit statuscollect2: ld returned 1 exit status
– 26 – CSCE 531 Spring 2006
deneb> more simple1.ydeneb> more simple1.y
%token DIGIT%token DIGIT
%%%%
expr : expr '+' expr expr : expr '+' expr
| expr '*' expr | expr '*' expr
| '(' expr ')' | '(' expr ')'
| DIGIT| DIGIT
;;
%%%%
– 27 – CSCE 531 Spring 2006
deneb> bison simple1.ydeneb> bison simple1.y
simple1.y contains 4 shift/reduce conflicts.simple1.y contains 4 shift/reduce conflicts.
bison -v simple1.ybison -v simple1.y
simple1.y contains 4 shift/reduce conflicts.simple1.y contains 4 shift/reduce conflicts.
deneb> ls -lrtdeneb> ls -lrt
……
--rw-r--r-- 1 matthews faculty 2311 Jun 30 12:10 simple1.outputrw-r--r-- 1 matthews faculty 2311 Jun 30 12:10 simple1.output
– 28 – CSCE 531 Spring 2006
.output.output
deneb> more simple1.output deneb> more simple1.output
State 8 contains 2 shift/reduce conflicts.State 8 contains 2 shift/reduce conflicts.
State 9 contains 2 shift/reduce conflicts.State 9 contains 2 shift/reduce conflicts.
GrammarGrammar
Number, Line, RuleNumber, Line, Rule
1 5 expr -> expr '+' expr1 5 expr -> expr '+' expr
2 6 expr -> expr '*' expr2 6 expr -> expr '*' expr
3 7 expr -> '(' expr ')'3 7 expr -> '(' expr ')'
4 8 expr -> DIGIT4 8 expr -> DIGIT
– 29 – CSCE 531 Spring 2006
Terminals, with rules where they appearTerminals, with rules where they appear
$ (-1)$ (-1)
'(' (40) 3'(' (40) 3
')' (41) 3')' (41) 3
'*' (42) 2'*' (42) 2
'+' (43) 1'+' (43) 1
error (256)error (256)
DIGIT (257) 4DIGIT (257) 4
Nonterminals, with rules where they appearNonterminals, with rules where they appear
expr (8)expr (8)
on left: 1 2 3 4, on right: 1 2 3on left: 1 2 3 4, on right: 1 2 3
– 30 – CSCE 531 Spring 2006
state 0state 0
DIGIT shift, and go to state 1DIGIT shift, and go to state 1
'(' shift, and go to state 2'(' shift, and go to state 2
expr go to state 3expr go to state 3
state 1state 1
expr -> DIGIT . (rule 4)expr -> DIGIT . (rule 4)
$default reduce using rule 4 (expr)$default reduce using rule 4 (expr)
44
– 31 – CSCE 531 Spring 2006
state 2state 2
expr -> '(' . expr ')' (rule 3)expr -> '(' . expr ')' (rule 3)
DIGIT shift, and go to state 1DIGIT shift, and go to state 1
'(' shift, and go to state 2'(' shift, and go to state 2
expr go to stateexpr go to state
– 32 – CSCE 531 Spring 2006
state 3state 3
expr -> expr . '+' expr (rule 1)expr -> expr . '+' expr (rule 1)
expr -> expr . '*' expr (rule 2)expr -> expr . '*' expr (rule 2)
$ go to state 10$ go to state 10
'+' shift, and go to state 5'+' shift, and go to state 5
'*' shift, and go to state 6'*' shift, and go to state 6
… … for states 4 through 7for states 4 through 7
– 33 – CSCE 531 Spring 2006
ConflictsConflicts
state 8state 8
expr -> expr . '+' expr (rule 1)expr -> expr . '+' expr (rule 1)
expr -> expr '+' expr . (rule 1)expr -> expr '+' expr . (rule 1)
expr -> expr . '*' expr (rule 2)expr -> expr . '*' expr (rule 2)
'+' shift, and go to state 5'+' shift, and go to state 5
'*' shift, and go to state 6'*' shift, and go to state 6
'+' [reduce using rule 1 (expr)]'+' [reduce using rule 1 (expr)]
'*' [reduce using rule 1 (expr)]'*' [reduce using rule 1 (expr)]
$default reduce using rule 1 (expr)$default reduce using rule 1 (expr)
– 34 – CSCE 531 Spring 2006
state 9state 9
expr -> expr . '+' expr (rule 1)expr -> expr . '+' expr (rule 1)
expr -> expr . '*' expr (rule 2)expr -> expr . '*' expr (rule 2)
expr -> expr '*' expr . (rule 2)expr -> expr '*' expr . (rule 2)
'+' shift, and go to state 5'+' shift, and go to state 5
'*' shift, and go to state 6'*' shift, and go to state 6
'+' [reduce using rule 2 (expr)]'+' [reduce using rule 2 (expr)]
'*' [reduce using rule 2 (expr)]'*' [reduce using rule 2 (expr)]
$default reduce using rule 2 (expr)$default reduce using rule 2 (expr)
– 35 – CSCE 531 Spring 2006
state 10state 10
$ go to state 11$ go to state 11
state 11state 11
$default accept$default accept
– 36 – CSCE 531 Spring 2006