Sunteți pe pagina 1din 22

Curs 11 - plan!

! Modelul unui parser LR! ! Automatul LR(0)! ! Articole (Items) LR(0)! ! Construc"ia mul"imii de articole LR(0)! ! Prexe viabile, Articole valide! ! Parsare LR(0)!

! Parsare SLR(1)!

2 !

Modelul unui parser LR!


input! a1! a2! ! ai! ! an! stack! s m! Xm! sm-1! Xm-1! ! s0! action!
shift# reduce# accept# error!

$!

LR Parsing Program# (driver)!

output!
Tabela de parsare!

goto!
DFA!

Construit$ cu ! metode LR(0),! SLR,! LR(1), sau! LALR(1)!

3 !

Parsare LR (Driver)!
Congura"ie ( = starea parserului LR):! (s0 X1 s1 X2 s2 Xm sm, ai ai+1 an $)! stack! input!

If action[sm,ai] = shift s then push ai, push s, and advance input:# !(s0 X1 s1 X2 s2 Xm sm ai s, ai+1 an $)! If action[sm,ai] = reduce A ! " and goto[sm-r,A] = s with r=|"| then# pop 2r symbols, push A, and push s:# !(s0 X1 s1 X2 s2 Xm-r sm-r A s, ai ai+1 an $)! If action[sm,ai] = accept then stop! If action[sm,ai] = error then attempt recovery!

4 !

Exemplu: tabela de parsare!


Gramatica:# 1. E ! E + T" 2. E ! T" 3. T ! T * F# 4. T ! F# 5. F ! ( E )# 6. F ! id! Shift & goto 5! Reducere cu# regula #1! action! state! id!
0! s5! 1! 2! 3! 4! s5! 5! 6! s5! 7! s5! 8! 9! 10! 11! s6! r1! r3! r5! s7! r3! r5! r6! r6! s4! s4! s11! r1! r3! r5! r1! r3! r5! s6! r2! r4! s7! r4! s4! r6! r6! 9! 3! 10! r2! r4! +! *! (! s4! )! $! acc! r2! r4! 8! 2! 3! E! 1!

goto!
T! 2! F! 3!

5 !

Exemplu: Parsare LR!


Stack! $ 0" $ 0 id 5" $ 0 F 3" $ 0 T 2" $ 0 T 2 * 7" $ 0 T 2 * 7 id 5" $ 0 T 2 * 7 F 10" $ 0 T 2# $ 0 E 1# $ 0 E 1 + 6# $ 0 E 1 + 6 id 5# $ 0 E 1 + 6 F 3# $ 0 E 1 + 6 T 9# $ 0 E 1! Input! id*id+id$" *id+id$" *id+id$" *id+id$" id+id$" +id$" +id$" +id$" +id$" id$" $" $" $" $! Action# shift 5# reduce 6 goto 3# reduce 4 goto 2# shift 7# shift 5# reduce 6 goto 10# reduce 3 goto 2# reduce 2 goto 1# shift 6# shift 5# reduce 6 goto 3# reduce 4 goto 9# reduce 1 goto 1# accept!

Gramatica:# 1. E ! E + T" 2. E ! T" 3. T ! T * F# 4. T ! F# 5. F ! ( E )# 6. F ! id!

6 !

Articole (Items) LR(0)!


! Un articol (item ) LR(0) pentru G este o produc"ie a lui G cu un ntr-o anume pozi"ie a p$r"ii drepte! ! Produc"ia# !A ! X Y Z" are 4 articole:# ![A ! X Y Z]" ![A ! X Y Z] " ![A ! X Y Z] " ![A ! X Y Z ]! ! Produc"ia A ! # are un articol [A ! ]!

7 !

Construc"ia mul"imii de articole LR(0)!


1.! Se adaug$ la gramatic$ simbolul S %i produc"ia S!S! 2.! Ini"ial C = closure({[S!S]})# (starea ini"ial$ a DFA)! 3.! Pentru ecare mul"ime de articole I $ C %i ecare X $ (N%T) astfel ca goto(I,X) & C %i goto(I,X) ' (, se adaug$ la C goto(I,X)! 4.! Repet$ 3 pn$ ce nu se mai pot ad$uga noi mul"imi la C!

8 !

Opera"ia closure!
1.! closure(I) = I! 2.! If [A!)B"] $ closure(I) then for each B!* in P, add [B!*] to I! 3.! Repeat 2 until no new items can be added!

9 !

Exemplu!
closure({[E ! E]}) = ! { [E ! E] }! { [E ! E]" [E ! E + T ]" [E ! T] }! { [E ! E]" { [E ! E]" [E ! E + T ]" [E ! E + T ]" [E ! T ]" [E ! T ]" [T ! T * F]# [T ! T * F]# [T ! F]# [T ! F] }! [F ! ( E )]# Add [T!*]! Add [F!*]! [F ! id] }!

Add [E!*]! Gramatica:# E ! E + T | T" T ! T * F | F# F ! ( E )# F ! id!

10 !

Opera"ia Goto (tranzi"ii n DFA)!


1.! For each [A!)X"] $ I, add closure ({[A!)X"]}) to goto(I,X) if not already there! 2.! Repeat step 1 until no more items can be added to goto(I,X)!

11 !

Prexe viabile, Articole valide!


! Prex viabil: orice prex al unui cuvnt )" dac$ S +*rm )Au +rm )"u! ! Articol valid: Dac$ " = "1"2 &i , = )"1 spunem c$ articolul A ! "1!"2 este valid pentru prexul viabil ,. ! ! Dac$ I este mul"imea articolelor valide pentru prexul viabil * atunci goto(I,X) este mul"imea articolelor valide pentru prexul viabil *X !

12 !

Exemplu!
I = { [E ! E ], [E ! E + T] }! goto(I,+) = closure({[E ! E + T]}) =! { [E ! E + T]# [T ! T * F]# [T ! F]# [F ! ( E )]# [F ! id] }!

Gramatica:# E ! E + T | T" T ! T * F | F# F ! ( E )# F ! id!

Gramatici LR(0)!
! G este o gramatic$ LR(0) dac$ pentru orice prex viabil * au loc simultan:!
! Nu exist$ dou$ articole complete (cu punctul la sfr%it) valide pentru * (altfel conict reduce/ reduce)! ! Dac$ * are un articol complet valid atunci nici un articol cu terminal dup$ punct nu este valid pentru * (altfel conict shift/reduce)!

13 !

! Echivalent: Orice stare I a afd-ului LR(0) este liber$ de conicte!

14 !

Parsare LR(0)!
! Se construie%te automatul LR(0)! ! Tabela de parsare (action, goto) coincide cu automatul LR(0)! ! Exemplu: ! ! ! S ! A, A ! aAa | bAb | c !

15 !

Exemplu!
Gramatica:# 1. E ! E + T" 2. E ! T" 3. T ! T * F# 4. T ! F# 5. F ! ( E )# 6. F ! id! nu este LR(0)!

16 !

Gramatici SLR!
! SLR (Simple LR): extensie a pars$rii LR(0)! ! SLR elimin$ conictele prin construirea unei tabele de parsare n care reducerea cu A!) se face pentru simbolurile din FOLLOW(A)#
State I : # 2 State I0:# goto(I0,id)! E ! id+ E" goto(I3,+)! S ! E # E ! id ! E ! id + E" E ! id! FOLLOW(E)={$}" thus reduce on $! Shift on +!

S ! E" E ! id + E# E ! id!

17 !

Parsare SLR!
! Se construie%te automatul LR(0)! ! Dac$ gramatica nu este LR(0) ( exist$ st$ri cu conicte) atunci se veric$ condi"ia ca ea s$ e SLR(1):!
! Dac# ntr-o stare exist# dou# articole de forma A ! )a", B ! * (conict shift/reduce) atunci a&FOLLOW(B) ! ! Dac# ntr-o stare exist# dou# articole de forma A ! ), B ! " (conict reduce/reduce) atunci FOLLOW(A)-FOLLOW(B) = .!

18 !

Construc"ia tabele de parsare SLR!


1.! Se adaug$ la gramatic$ regula S!S! 2.! Se construie%te mul"imea de articole LR(0) C= {I0,I1,,In}! 3.! Dac$ [A!)a"] $ Ii %i goto(Ii,a)=Ij atunci action [i,a]=shift j! 4.! Dac$ [A!)] $ Ii atunci action[i,a]=reduce A!), pentru orice a $ FOLLOW(A) (A!S)! 5.! Dac$ [S!S] $ Ii atunci action[i,$]=accept! 6.! Dac$ goto(Ii,A)=Ij atunci goto[i,A]=j! 7.! Se repet$ 3-6 ct timp se fac modic$ri! 8.! Starea ini"ial$ este cea care con"ine [S!S]!

19 !

Exemplu!
Augmented# grammar:# 1. C ! C# 2. C ! A B" 3. A ! a# 4. B ! a! start! I0 = closure({[C ! C]})# I1 = goto(I0,C) = closure({[C ! C]})# ! State I1:# State I4:# nal ! C ! C! C ! A B! goto(I0,C)! goto(I2,B)! State I0:# State I : # 2 C ! C # goto(I0,A)! C ! AB" C ! A B" B ! a! goto(I ,a)! 2 A ! a! goto(I0,a)! State I :# 3 A ! a! State I5:# B ! a !

20 !

Exemplu (cont)!
State I0:# C ! C # C ! A B" A ! a! State I1:# C ! C! State I2:# C ! AB" B ! a! State I3:# A ! a! State I4:# State I5:# C ! A B! B ! a!

1! C!
start!

a! 0! s3! B! 4! 1! 2! s5! a! 5! 3! r3! 4! 5!

$! acc!

C! 1!

A! 2!

B!

0!

A! a!

2!

4! r2! r4!

3!

Gramatica:# 1. C ! C# 2. C ! A B" 3. A ! a# 4. B ! a!

21 !

Exemplu: tabela de parsare SLR!


Gramatica:# 1. E ! E + T" 2. E ! T" 3. T ! T * F# 4. T ! F# 5. F ! ( E )# 6. F ! id! Shift & goto 5! Reducere cu# regula #1! action! state! id!
0! s5! 1! 2! 3! 4! s5! 5! 6! s5! 7! s5! 8! 9! 10! 11! s6! r1! r3! r5! s7! r3! r5! r6! r6! s4! s4! s11! r1! r3! r5! r1! r3! r5! s6! r2! r4! s7! r4! s4! r6! r6! 9! 3! 10! r2! r4! +! *! (! s4! )! $! acc! r2! r4! 8! 2! 3! E! 1!

goto!
T! 2! F! 3!

22 !

SLR vs Ambiguitate!
! Orice gramatic$ SLR este neambigu$ dar nu orice gramatic$ neambigu$ este SLR! ! Exemplu:# !S ! L = R | R# !L ! * R | id# !R ! L"
I0:# S ! S # S ! L=R" S ! R" L ! *R # L ! id" R ! L ! I1:# S ! S! I2:# S ! L=R" R ! L ! I3:# S ! R! I4:# L ! *R # R ! L ! L ! *R # L ! id! I5:# I6:# L ! id! S ! L=R" R ! L " L ! *R # L ! id! I7:# L ! *R! I8:# R ! L ! I9:# S ! L=R!

action[2,=]=s6# action[2,=]=r5!

S-ar putea să vă placă și