Upload
vananh
View
220
Download
0
Embed Size (px)
Citation preview
LRParsing
CMPT379:CompilersInstructor:AnoopSarkar
anoopsarkar.github.io/compilers-class
LR4:LR(1)Parsing
2
SLRlimitation:lackofcontext
0: S’ → • S �S → • L = R�S → • R�L → • * R�L → • id�R → • L
S’ → S �S → L = R | R�L → *R | id�R → L
1: L → id •
2: S → L • = R�R → L •
id
L 3: S → L = • R�R → • L �L → • * R�L → • id
=
Follow(R) = ?{ =, $ }
Input: id = id
3
S’ → S �S → L = R | R�L → *R | id�R → L
S’
S
R
L
id
S’
S
id
L = R
2: S → L • = R�R → L •
Follow(R) = { =, $ }
L
id
S’
S
L = R
L
idL
id
* R
S’
S
R
L
L
id
* R
Find all lookaheadsfor reduce R → L •
$
=
$
$
$No! R → L • reduce and S → L • = R do not co-occur due to the L → *R rule
Problem?
4
Solution:CanonicalLR(1)
• Extenddefinitionofconfiguration– Rememberlookahead
• Newclosuremethod• ExtenddefinitionofSuccessor
5
LR(1)Configurations
• [A→α•β,a]fora∈Tisvalidforaviableprefixδαifthereisarightmostderivation
S⇒*δAη⇒*δαβηand(η=aγ)or(η=εanda=$)
• Notation:[A→α•β,a/b/c]– if[A→α•β,a],[A→α•β,b],[A→α•β,c]arevalidconfigurations
6
LR(1)Configurations
S→BBB→aB|b
• S⇒*rmaaBab⇒rmaaaBab• Item[B→a•B,a]isvalidforviableprefixaaa
• S⇒*rmBaB⇒rmBaaB• Also,item[B→a•B,$]isvalidforviableprefixBaa
S ⇒ BB ⇒ BaB ⇒ Bab ⇒ aBab ⇒ aaBab ⇒ aaaBab
S ⇒ BB ⇒ BaB ⇒ BaaB
In BaB ⇒ BaaB the string aB is the handle (rhs of B)
7
LR(1)Closure
Closureproperty:• If[A→α•Bβ,a]isinset,then[B→•γ,b]isinsetifb∈First(βa)
• Computeasfixedpoint• OnlyincludecontextuallyvalidlookaheadstoguidereducingtoB
8
StartingConfiguration
• AugmentGrammarwithS’justlikeforLR(0),SLR(1)
• InitialconfigurationsetisI=closure([S’→•S,$])
9
Example:closure([S’→•S,$])
[S’→•S,$][S→•L=R,$][S→•R,$][L→•*R,=][L→•id,=][R→•L,$][L→•*R,$][L→•id,$]
S’ → S �S → L = R | R�L → *R | id�R → L
S’ → • S, $ S → • L = R, $ S → • R, $L → • * R, =/$ L → • id, =/$ R → • L, $
concisely written as:
10
LR(1)Successor(C,X)
• LetI=[A→α•Bβ,a]or[A→α•bβ,a]• Successor(I,B)=closure([A→αB•β,a])• Successor(I,b)=closure([A→αb•β,a])
11
LR(1)Example
0: S’ → • S, $ �S → • L = R, $ �S → • R, $�L → • * R, =/$ �L → • id, =/$ �R → • L, $
1: L → id •, $/=
2: S → L • = R, $ �R → L •, $
3: S → L = • R, $ �R → • L, $�L → • *R, $ �L → • id, $
4: L → id •, $
id
L
=
id
5: R → L •, $L
6: S → L = R•, $R 7: S’ → S •, $
S
*
12
LR(1)Example(contd)
3: S → L = • R, $ �R → • L, $�L → • *R, $ �L → • id, $
4: L → id •, $
5: R → L •, $
8: L → * • R, $ �R → • L, $
L → • *R, $ L → • id, $
id
L
*
9: L → * R•, $
R
*
13
LR(1)Example(contd)
0: S’ → • S, $ �S → • L = R, $ �S → • R, $�L → • * R, =/$ �L → • id, =/$ �R → • L, $ 10: L → * • R, =/$
R → • L, =/$ L → • *R, =/$ L → • id, =/$
1: L → id •, =/$id
11: R → L •, =/$ L
12: L → *R •, =/$R
*
*
13: S → R•, $R
14
id = * $ S L R0 S1 S10 7 2 131 R4 R42 S3 R53 S4 S8 5 64 R45 R56 R17 Acc8 S4 5 99 R310 S1 S10 11 1211 R5 R512 R3 R313 R2
Productions1 S → L = R2 S → R3 L → * R4 L → id5 R → L
15
LR(1)Construction
1. ConstructF={I0,I1,…In}2. a)if[A→α•,a]∈IiandA!=S’
thenaction[i,a]:=reduceA→αb)if[S’→S•,$]∈Ii
thenaction[i,$]:=acceptc)if[A→α•aβ,b]∈IiandSuccessor(Ii,a)=Ij
thenaction[i,a]:=shiftj3. ifSuccessor(Ii,A)=Ijthengoto[i,A]:=j
16
LR(1)Construction(cont’d)
4. Allentriesnotdefinedareerrors5. MakesureI0istheinitialstate
• Note:LR(1)onlyreducesusingA→αfor[A→α•,a]ifafollows• LR(1)statesremembercontextbyvirtueof
lookahead• Possiblymanystates!
– LALR(1)combinessomestates
17
LR(1)Conditions
• AgrammarisLR(1)ifforeachconfigurationset(itemset)thefollowingholds:– Foranyitem[A→α•xβ,a]withx∈Tthereisno
[B→γ•,x]– Foranytwocompleteitems[A→γ•,a]and
[B→β•,b]thena!=b.• Grammars:
– LR(0)⊂SLR(1)⊂LR(1)⊂LR(k)• Languagesexpressiblebygrammars:
– LR(0)⊂SLR(1)⊂LR(1)=LR(k)
18
Set-of-itemswithEpsilonrulesS → AaAbS → BbBaA → εB → ε
S
B
b
B
a
Aa
A
b
S → A•aAb,$6:
S’ → S•,$1:
S → Aa•Ab,$A → ε•,b
7:
S’ → •S,$S → •AaAb,$S → •BbBa,$A → ε•,aB → ε•,b
0:
S → AaA•b,$8:
S → AaAb•,$9: S → BbBa•,$
5:
S → BbB•a,$4:
S → Bb•Ba,$B → ε•,a
3:
S → B•bBa,$2:
19
CanonicalLR(1)Recap
• LR(1)usesleftcontext,currenthandleandlookaheadtodecidewhentoreduceorshift
• Mostpowerfulparsersofar• LALR(1)ispracticalsimplificationwithfewerstates
20
MergingStatesinLALR(1)
• S’→SS→XXX→aXX→b
• SameCoreSet
• Differentlookaheads
6: X → a • X, $ �X → • a X, $ �X → • b, $
3: X → a • X, a/b �X → • a X, a/b �X → • b, a/b
36: X → a • X, a/b/$ � X → • a X, a/b/$ � X → • b, a/b/$
21
R/Rconflictswhenmerging
• B→dB→fXgX→…
• IfR/Rconflictsareintroduced,grammarisnotLALR(1)!
4: B → d •, g �B → f X g •, c �
2: B → d •, c�B → f X g •, e
24: B → d •, c/g� B → f X g •, c/e
22
LALR(1)
• LALR(1)Condition:– Merginginthiswaydoesnotintroducereduce/
reduceconflicts– Shift/reducecan’tbeintroduced
• Mergingbruteforceorstep-by-step• MorecompactthancanonicalLR,likeSLR(1)• MorepowerfulthanSLR(1)
– NotalwaysmergetofullFollowSet