ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών · ΕΠΛ323 -...
Transcript of ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών · ΕΠΛ323 -...
Operator-precedenceParsing
• Aclassofshift-reduceparsersthatcanbewrittenbyhand
• Noε-productions,notwoadjacentnon-terminalsontherightside
E èEAE|(E)|-E|idE è+|-|*|/|^
OperatorGrammarE èE+E|E-E|E*E|E/E|E^E|(E)|-E|id
x
✓
OperationRelationTableRELATION MEANING
α <·β α “yieldsprecedenceto”β
α =β α “hasthesameprecedence”β
α ·> β α “takesprecedenceover”β
·
id + * $
id ·> ·> ·>
+ <· ·> <· ·>
* <· ·> ·> ·>
$ <· <· <·
E èE+E|E*E|id
ExampleSTACK INPUT ACTION
$ id+id*id$ shift (push)
$id +id*id$ reduce (pop)
$ +id*id$ shift (push)
$+ id*id$ shift (push)
$+id *id$ reduce (pop)
$+* id$ shift (push)
$+*id $ shift (push)
$+* $ reduce (pop)
$+ $ reduce (pop)
$ $ reduce (pop)
id + * $
id ·> ·> ·>
+ <· ·> <· ·>
* <· ·> ·> ·>
$ <· <· <·
stack <· input: shift (push)stack ·> input: reduce (pop)
CompressionofParsingTable
gid
f*
g+
f$
fid
g*
f+
g$
f \ g id + * $
id ·> ·> ·>
+ <· ·> <· ·>
* <· ·> ·> ·>
$ <· <· <·
+ * id $
f 2 4 4 0
g 1 3 5 0
LR(k)parsers
Left-to-rightscanningoftheinput.
Constructionofarightmostderivationinreverse
Numberofsymbolsfortakingadecision(lookahead).
LRparsers• LRparserscanbeconstructedtorecognizevirtuallyallprogramming-languageconstructsforwhichcontext-freegrammarscanbewritten.
• TheLRparsingmethodisthemostgeneralnonbacktracking shift-reduceparsingmethodknown,yetitcanbeimplementedasefficientlyasothershift-reducemethods.
• TheclassofgrammarsthatcanbeparsedusingLRmethodsisapropersupersetoftheclassofgrammarsthatcanbeparsedwithpredictiveparsers(e.g.,LL(1)).
• AnLRparsercandetectasyntacticerrorassoonasitispossibletodosoonaleft-to-rightscanoftheinput.
RecallLL(1)
PredictiveParsingProgramX
Y
Z
$
STACK
ParsingTableM
a + b $INPUT
OUTPUT
$:endsymbolX,Y,Z:non-terminalsorterminals
parsingtable
LRparser
LRParsingProgramsm
STACK
actions
… ai … $INPUT
OUTPUT
a1 an
s0
Sm-1
Xm-1
...
Xm
goto
Algorithm(foraction[sm,ai])1.shifts,wheres isstate2.reducebyagrammarproduction3.accept,and4.error.
LR(0)item
A èXYZ
A è·XYZA èX·YZA èXY·ZA èXYZ·
A èε
A è·
fouritems
WehopetoseeastringderivablefromXYZ nextontheinput.
WehavejustseenontheinputastringderivablefromXandthatwehopenexttoseeastringderivablefromYZ.
Wesawallinput.
Closure
IfI isasetofitemsforagrammarG,thenclosure(I)isthesetofitemsconstructedfromI bythetworules:• Initially,everyiteminI isaddedtotheclosure(I).• IfAèa·Bb isinclosure(I)andBèC isaproduction,thenaddtheitemBè·C toI,ifitisnotalreadythere.Weapplythisruleuntilnomorenewitemscanbeaddedtoclosure(I).
Example
GrammarE’àEE àE+T|TT àT*F|FF à(E)|id
I = {[E’à·E]}closure(I)E’à·EE à·E+TE à·TT à·T*FT à·FF à·(E)F à·id
Goto
goto(I,X)isdefinedtobetheclosure ofthesetofallitems[AàαX·β]suchthat[Aàα·Xβ]isinI.
I = {[E’àE·], [EàE·+T]}goto(I,+)E àE+·TT à·T*FT à·FF à·(E)F à·id
GrammarE’àEE àE+T|TT àT*F|FF à(E)|id
Howgoto(I,+)iscomputed?Wecomputedgoto(I,+)byexaminingI foritemswith+immediatelytotherightofthedot.E’àE· isnotsuchanitem,but EàE·+T is.Wemovedthedotoverthe+toget{EàE+·T} andthentooktheclosureofthisset.
CanonicalcollectionofLR(0)items• Augmentthegrammarwithanewsymbolthatproducesthestartingsymbolofthegrammar:S’àS
• Computetheclosureofthenewproduction,C:=closure({[S’à·S]})
• ForeachsetofitemsI inC,andeachgrammarsymbolX,addgoto(I,X)toC
I0E’à·EE à·E+TE à·TT à·T*FT à·FF à·(E)F à·id
I1E’àE·E àE·+T
I2E àT·T àT·*F
I3T àF·
I4F à(·E)E à·E+TE à·TT à·T*FT à·FF à·(E)F à·id
I5F àid·
I6E àE+·TT à·T*FT à·FT à·(E)T à·id
I7T àT*·FF à·(E)F à·id
I8F à(E·)E àE·+T
I9E àE+T·T àT·+F
I10T àT*F·
I11F à(E)·
CanonicalcollectionofLR(0)items
TransitionDiagram
I1I0E
I6+
I9Τ
I2
Τ
I3F
I4(
I5id
I7
I8E I11
)
id
I10F
(id toI4
toI5
* toI7
toI3toI4
toI5
F(id
*
( F
TtoI2
toI6+
SLRParsingTableSTATE
action goto
id + * ( ) $ E T F
0 s5 s4 1 2 3
1 s6 acc
2 r2 s7 r2 r2
3 r4 r4 r4 r4
4 s5 s4 8 2 3
5 r6 r6 r6 r6
6 s5 s4 9 3
7 s5 s4 10
8 s6 s11
9 r1 s7 r1 r1
10 r3 r3 r3 r3
11 r5 r5 r5 r5
ParsingAlgorithmset ip to point the first symbol of w$;repeat forever begin
let s be the state on top of the stack and a the symbol pointed to by ip;if (action[s, a] = shift s’ then begin
push a then s’ on top of the stack;advance ip to the next input symbol
endelse if action[s, a] = reduce Aàb then begin
pop 2x|b| symbols off the stack;let s’ be the state now on top of the stack;push A then goto[s’, A] on top of the stack;
output the production Aàbendelse if action[s, a] = accept then
returnelse error()
end
id*id+idSTACK INPUT ACTION
(1)0 id*id+id$ shift
(2)0id 5 *id+id$ reduce by Fàid
(3)0F 3 *id+id$ reduce byTàF
(4)0 T 2 *id+id$ shift
(5)0 T 2*7 id+id$ shift
(6)0 T 2*7id 5 +id$ reduce byFàid
(7)0T 2*7F 10 +id$ reduce byTàT*F
(8) 0T 2 +id$ reduce byEàT
(9)0E 1 +id$ shift
(10)0E 1+6 id$ shift
(11)0 E 1+6id 5 $ reduce byFàid
(12)0 E 1+6F 3 $ reduce byTàF
(13)0 E 1+6T 9 $ EàE+T
(14)0E 1 $ accept
Productions(1) E àE+T(2) E àT(3) T àT*F(4) T àF(5) F à(E)(6) F àid