Documente Academic
Documente Profesional
Documente Cultură
! 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 !
$!
output!
Tabela de parsare!
goto!
DFA!
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 !
goto!
T! 2! F! 3!
5 !
6 !
7 !
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] }!
10 !
11 !
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] }!
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 !
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 !
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!
$! 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 !
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!