lr parsing - anoop sarkar · rm aabab ⇒ rm aaabab • item [b → a • b, a] is valid for viable...

22
LR Parsing CMPT 379: Compilers Instructor: Anoop Sarkar anoopsarkar.github.io/compilers-class LR4: LR(1) Parsing

Upload: vananh

Post on 20-May-2019

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: LR Parsing - Anoop Sarkar · rm aaBab ⇒ rm aaaBab • Item [B → a • B, a] is valid for viable prefix aaa • S ⇒* rm BaB ⇒ rm BaaB • Also, item [B → a • B, $] is

LRParsing

CMPT379:CompilersInstructor:AnoopSarkar

anoopsarkar.github.io/compilers-class

LR4:LR(1)Parsing

Page 2: LR Parsing - Anoop Sarkar · rm aaBab ⇒ rm aaaBab • Item [B → a • B, a] is valid for viable prefix aaa • S ⇒* rm BaB ⇒ rm BaaB • Also, item [B → a • B, $] is

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

Page 3: LR Parsing - Anoop Sarkar · rm aaBab ⇒ rm aaaBab • Item [B → a • B, a] is valid for viable prefix aaa • S ⇒* rm BaB ⇒ rm BaaB • Also, item [B → a • B, $] is

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?

Page 4: LR Parsing - Anoop Sarkar · rm aaBab ⇒ rm aaaBab • Item [B → a • B, a] is valid for viable prefix aaa • S ⇒* rm BaB ⇒ rm BaaB • Also, item [B → a • B, $] is

4

Solution:CanonicalLR(1)

• Extenddefinitionofconfiguration– Rememberlookahead

• Newclosuremethod• ExtenddefinitionofSuccessor

Page 5: LR Parsing - Anoop Sarkar · rm aaBab ⇒ rm aaaBab • Item [B → a • B, a] is valid for viable prefix aaa • S ⇒* rm BaB ⇒ rm BaaB • Also, item [B → a • B, $] is

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

Page 6: LR Parsing - Anoop Sarkar · rm aaBab ⇒ rm aaaBab • Item [B → a • B, a] is valid for viable prefix aaa • S ⇒* rm BaB ⇒ rm BaaB • Also, item [B → a • B, $] is

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)

Page 7: LR Parsing - Anoop Sarkar · rm aaBab ⇒ rm aaaBab • Item [B → a • B, a] is valid for viable prefix aaa • S ⇒* rm BaB ⇒ rm BaaB • Also, item [B → a • B, $] is

7

LR(1)Closure

Closureproperty:• If[A→α•Bβ,a]isinset,then[B→•γ,b]isinsetifb∈First(βa)

• Computeasfixedpoint• OnlyincludecontextuallyvalidlookaheadstoguidereducingtoB

Page 8: LR Parsing - Anoop Sarkar · rm aaBab ⇒ rm aaaBab • Item [B → a • B, a] is valid for viable prefix aaa • S ⇒* rm BaB ⇒ rm BaaB • Also, item [B → a • B, $] is

8

StartingConfiguration

• AugmentGrammarwithS’justlikeforLR(0),SLR(1)

• InitialconfigurationsetisI=closure([S’→•S,$])

Page 9: LR Parsing - Anoop Sarkar · rm aaBab ⇒ rm aaaBab • Item [B → a • B, a] is valid for viable prefix aaa • S ⇒* rm BaB ⇒ rm BaaB • Also, item [B → a • B, $] is

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:

Page 10: LR Parsing - Anoop Sarkar · rm aaBab ⇒ rm aaaBab • Item [B → a • B, a] is valid for viable prefix aaa • S ⇒* rm BaB ⇒ rm BaaB • Also, item [B → a • B, $] is

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])

Page 11: LR Parsing - Anoop Sarkar · rm aaBab ⇒ rm aaaBab • Item [B → a • B, a] is valid for viable prefix aaa • S ⇒* rm BaB ⇒ rm BaaB • Also, item [B → a • B, $] is

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

*

Page 12: LR Parsing - Anoop Sarkar · rm aaBab ⇒ rm aaaBab • Item [B → a • B, a] is valid for viable prefix aaa • S ⇒* rm BaB ⇒ rm BaaB • Also, item [B → a • B, $] is

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

*

Page 13: LR Parsing - Anoop Sarkar · rm aaBab ⇒ rm aaaBab • Item [B → a • B, a] is valid for viable prefix aaa • S ⇒* rm BaB ⇒ rm BaaB • Also, item [B → a • B, $] is

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

Page 14: LR Parsing - Anoop Sarkar · rm aaBab ⇒ rm aaaBab • Item [B → a • B, a] is valid for viable prefix aaa • S ⇒* rm BaB ⇒ rm BaaB • Also, item [B → a • B, $] is

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

Page 15: LR Parsing - Anoop Sarkar · rm aaBab ⇒ rm aaaBab • Item [B → a • B, a] is valid for viable prefix aaa • S ⇒* rm BaB ⇒ rm BaaB • Also, item [B → a • B, $] is

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

Page 16: LR Parsing - Anoop Sarkar · rm aaBab ⇒ rm aaaBab • Item [B → a • B, a] is valid for viable prefix aaa • S ⇒* rm BaB ⇒ rm BaaB • Also, item [B → a • B, $] is

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

Page 17: LR Parsing - Anoop Sarkar · rm aaBab ⇒ rm aaaBab • Item [B → a • B, a] is valid for viable prefix aaa • S ⇒* rm BaB ⇒ rm BaaB • Also, item [B → a • B, $] is

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)

Page 18: LR Parsing - Anoop Sarkar · rm aaBab ⇒ rm aaaBab • Item [B → a • B, a] is valid for viable prefix aaa • S ⇒* rm BaB ⇒ rm BaaB • Also, item [B → a • B, $] is

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:

Page 19: LR Parsing - Anoop Sarkar · rm aaBab ⇒ rm aaaBab • Item [B → a • B, a] is valid for viable prefix aaa • S ⇒* rm BaB ⇒ rm BaaB • Also, item [B → a • B, $] is

19

CanonicalLR(1)Recap

• LR(1)usesleftcontext,currenthandleandlookaheadtodecidewhentoreduceorshift

• Mostpowerfulparsersofar• LALR(1)ispracticalsimplificationwithfewerstates

Page 20: LR Parsing - Anoop Sarkar · rm aaBab ⇒ rm aaaBab • Item [B → a • B, a] is valid for viable prefix aaa • S ⇒* rm BaB ⇒ rm BaaB • Also, item [B → a • B, $] is

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/$

Page 21: LR Parsing - Anoop Sarkar · rm aaBab ⇒ rm aaaBab • Item [B → a • B, a] is valid for viable prefix aaa • S ⇒* rm BaB ⇒ rm BaaB • Also, item [B → a • B, $] is

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

Page 22: LR Parsing - Anoop Sarkar · rm aaBab ⇒ rm aaaBab • Item [B → a • B, a] is valid for viable prefix aaa • S ⇒* rm BaB ⇒ rm BaaB • Also, item [B → a • B, $] is

22

LALR(1)

•  LALR(1)Condition:–  Merginginthiswaydoesnotintroducereduce/

reduceconflicts–  Shift/reducecan’tbeintroduced

• Mergingbruteforceorstep-by-step• MorecompactthancanonicalLR,likeSLR(1)• MorepowerfulthanSLR(1)

–  NotalwaysmergetofullFollowSet