Course 9
Course 9
S.Motogna - FL&CD
LR(k) parsing:
LR(0), SLR, LR(1), LALR
• Define item
• Construct set of states Executed 1 time
• Construct table
S.Motogna - FL&CD
Algorithm ColCan_LR(0)
Algoritmul 3.9 Col stariLR0
INPUT: G’- gramatica ı̂mbogăţită
OUTPUT: C - colecţia canonică de stări
C := ;;
s0 := closure({[S 0 ! .S]}) // state corresponding to prod. of S’ = initial state
C := C [ {s0 }; //initialize collection with s0
repeat
for 8s 2 C do
for 8X 2 N [ ß do
if goto(s, X) 6= ; and goto(s, X) 2 / C then
C = C [ goto(s, X) //add new state
end if
end for
end for
until C nu se mai modifică
S.Motogna - FL&CD
A!c
2. closure(I) = I [ {[B ! .±]|[A ! Æ.BØ] 2 I}, conform observaţiei
din paragraful anterior.
Algorithm Closure
Algoritmul 3.8 ClosureLR0
I = LR(0) item of the form [A->𝜶.𝜷]
goto(s,X): in state s, search LR(0) item that has dot in front of symbol X.
Move the dot after symbol X and call closure for this new item.
S.Motogna - FL&CD
SLR Parser
Prediction = next symbols on
input sequence
• SLR = Simple LR
• Remark:
LR(0) – lots of conflicts – solved if considering prediction
=>
1. LR(0) canonical collection of states– prediction of length 0
2. Table and parsing sequence – prediction of length 1
S.Motogna - FL&CD
SLR Parsing:
S.Motogna - FL&CD
Construct SLR table
Remarks:
1. Prediction = next symbol from input sequence => FOLLOW
- see LL(1)
2. Structure – LR(k):
• Lines - states
• action + goto Optimize table structure:
merge action and goto
action – a column for each prediction ∈𝞢 columns for Σ
goto – a column for each symbol X ∈N∪𝞢
Remark (LR(0) table):
• if s is accept state then goto(s, X) = ∅, ∀X ∈ N ∪ Σ.
• If in state s action is reduce then goto(s, X) = ∅, ∀X ∈ N ∪ Σ.
S.Motogna - FL&CD
SLR table And goto
Action GOTO
a1 … an B1 … Bm
a1,…,an ∈𝞢
s0 B1,...,Bm ∈N
s0,…,sk - states
s1
…
sk
S.Motogna - FL&CD
Rules for SLR table
1. If [A → α.β] ∈ si and goto(si,a) = sj then action(si,a)=shift sj
// dot is not at the end
S.Motogna - FL&CD
Remarks
1. Similarity with LR(0)
2. A grammar is SLR if the SLR table does not contain conflicts (more
than one value in a cell)
S.Motogna - FL&CD
Parsing sequences
• INPUT:
• Grammar G’ = (NU{S’}, 𝚺, P U {S’->S},S’)
• SLR table
• Input sequence w =a1…an
• OUTPUT:
if (w ∈L(G)) then string of productions
else error & location of error
S.Motogna - FL&CD
SLR = LR(0) configurations
Initial configuration:
($s0,w$,𝜀)
(𝛼 , 𝛽 , 𝜋 )
where:
• 𝛼 = working stack
• 𝛽 = input stack
• 𝜋 = output (result) Final configuration:
($sacc, $, 𝜋)
S.Motogna - FL&CD
Moves
head(𝛽) = prediction
1. Shift
if action(sm,ai)= shift sj then
($s0x1 ...xmsm,ai ...an$, 𝜋) ⊢ ($s0x1 ...xmsmaisj,ai+1 ...an$, 𝜋)
2. Reduce
if action(sm,ai) = reduce t AND (t) A → xm−p+1 ...xm AND goto(sm−p,A) = sj
then
($s0 ...xmsm,ai ...an$, 𝜋) ⊢ ($s0 ...xm−psm−pAsj,ai ...an$,t 𝜋)
3. Accept
if action(sm,$) = accept then ($sm,$, 𝜋)=acc
4. Error - otherwise
S.Motogna - FL&CD
LR(1) Parser
[A→𝜶.𝜷,u]
S.Motogna - FL&CD
Construct LR(1) set of states
• Alg ColCan_LR1
• Function goto_LR1
• Alg Closure_LR1
S.Motogna - FL&CD
INPUT: G’ – enhanced grammar
Algorithm ColCan_LR1 OUTPUT: C1 – cannonical collection of states
C1 =∅
S0 = Closure_LR1({[S’→.S,$]})
C1 := C1 U {s0}
Repeat
for ∀s ∊ C1 do
for ∀ X ∊ N U𝛴 do
T = goto_LR1(s,X)
if T≠ ∅ and T ∉ C1 then
C1 = C1 U T
endif
endfor
endfor
Until C1 unchanged
S.Motogna - FL&CD
Function goto_LR1
Goto_LR1 : P(ℰ1) × (N ∪ Σ) → P(ℰ1)
where ℰ1 = set ofLR(1) items
S.Motogna - FL&CD
• tranziţia ı̂n altă stare.
De
De aceea
aceea tabelele
tabelele dede analiză
analiză LR(k)
LR(k) au
au două
două componente:
componente: dede acţ
acţ
Algorithm Closure_LR1
de deplasare, numită ”goto“.
de deplasare, numită ”goto“.
Care sunt şi cum se determină aceste stări? Pentru a răspu
Care sunt şi cum se determină aceste stări? Pentru a răspun
considerăm elementul de analiză [A ! Æ.BØ, u] care, conform defi
considerăm
• [A → α.Bβ,u] valid elementul
for live de γα
prefix analiză
=> [A ! Æ.BØ, u] care, conform defi
implică:
implică:
§
S) § dr ∞Aw )dr ∞ÆBØw şi
S )dr ∞Aw )dr ∞ÆBØw şi
u = F IRSTk (w) valabil pentru prefixul viabil ∞Æ.
u = F IRSTk (w) valabil pentru prefixul viabil ∞Æ.
Dacă ı̂n gramatică există o producţie B ! ± atunci elementul de a
Dacă ı̂n gramatică există o producţie B ! ± atunci elementul de a
[B ! .±, u] are, de asemenea,
§ u valid pentru prefixul viabil ∞Æ:
• [B → .δ,
[B smth]∈P
! .±, u] =>
are, deS) asemenea,
∞Aw )udrvalid pentru)
∞ÆBØw prefixul viabil ∞Æ:
dr ∞ƱØw.
Această observaţie sugerează faptul că elem
punzătoare
=> [B → .δ,b] valid for unui acelaşi prefix viabil ar trebui gr
live prefix γα,
∀b ∊ FIRST(𝛽u)această mulţime
// First(𝛽w)caracterizează
= First(𝛽u) un pas al analiz
Mulţimea care va conţine toate elementele de ana
prefix viabil va forma o stare a automatului.
S.Motogna - FL&CD
8[A ! Æ.BØ, a] 2 closure(C), 8B ! ± 2 P, [B ! .±, b] 2 closure(C)
pentru 8b 2 F IRST (Øa)
Algorithm Closure_LR1
Algoritmul 3.11 ClosureLR1
INPUT: I-element de analiză; G’- gramatica ı̂mbogăţită;
F IRST (X), 8X 2 N [ ß;
OUTPUT: C1 = closure(I);
C1 := {I};
repeat
for 8[A ! Æ.BØ, a] 2 C1 do
for 8B ! ∞ 2 P do
for 8b 2 F IRST (Øa) do
if [B ! .∞, b] 2
/ C1 then
C1 = C1 [ [B ! .∞, b]
end if
end for
end for
end for
until C1 nu se mai modifică
S.Motogna - FL&CD
Remarks
1. A grammar is LR(1) if the LR(1) table does not contain conflicts
S.Motogna - FL&CD
4. Define configurations and moves
• INPUT:
• Grammar G’ = (NU{S’}, 𝚺, P U {S’->S},S’)
• LR(1) table
• Input sequence w =a1…an
• OUTPUT:
if (w ∈L(G)) then string of productions
else error & location of error
S.Motogna - FL&CD
LR(1) configurations
Initial configuration:
($s0,w$,𝜀)
(𝛼 , 𝛽 , 𝜋 )
where:
• 𝛼 = working stack
• 𝛽 = input stack
• 𝜋 = output (result) Final configuration:
($sacc, $, 𝜋)
S.Motogna - FL&CD
Moves
head(𝛽) = prediction
1. Shift
if action(sm,ai)= shift sj then
($s0x1 ...xmsm,ai ...an$, 𝜋) ⊢ ($s0x1 ...xmsmaisj,ai+1 ...an$, 𝜋)
2. Reduce
if action(sm,ai) = reduce t AND (t) A → xm−p+1 ...xm AND goto(sm−p,A) = sj
then
($s0 ...xmsm,ai ...an$, 𝜋) ⊢ ($s0 ...xm−psm−pAsj,ai ...an$,t 𝜋)
3. Accept
if action(sm,$) = accept then ($sm,$, 𝜋)=acc
4. Error - otherwise
S.Motogna - FL&CD
LALR Parser
• LALR = Look Ahead LR(1)
• why?
S.Motogna - FL&CD
LALR principle [A → αβ.,u] ∈ si apply reduce (k) then goto(si,A) =sm
[A → αβ.,v] ∈ sj apply reduce (k) then goto(sj,A) =sn
[A → α.β,u] ∈ si
=> [A → α.β,u|v] ∈ si,j
[A → α.β,v] ∈ sj
S.Motogna - FL&CD
LALR Parsing
• Same as LR(1)
• Number of LALR states = number of SLR / LR(0) states
S.Motogna - FL&CD
LR(k) Parsers
• LR(0):
• Items ignore prediction
• Reduce can be applied only in singular states (contain one item)
• Lot of conflicts
• SLR:
• Use same items as LR(0)
• When reduce consider prediction
• Eliminate several LR(0) conflicts (not all)
• LR(1):
• Performant algorithm for set of states
• Generate few conflicts
• Generate lot of states
• LALR:
• Merge LR(1) states ccorresponding to same kernel
• Most used algorithm (most performant)
S.Motogna - FL&CD
Quiz time
S.Motogna - FL&CD
Parsing - recap
Descendent Ascendent
Recursive Descendent recursive Ascendent recursive parser
parser
Linear LL(1) LR(0), SLR, LR(1), LALR
S.Motogna - FL&CD
Eliminarea conflictelor nu este ı̂ntotdeauna uşor de realizat şi de aceea se
doreşte evitarea lor. Cea mai puţin restrictivă clasă este cea a gramaticilor
LR(1), dar analizorul sintactic are alte dezavantaje, asupra cărora vom
reveni. Figura 3.4 ilustrează incluziunea dintre tipurile de gramatici luate
Parsing - recap
ı̂n considerare ı̂n analiza sintactică. Se observă că nu există o corelaţie
evidentă ı̂ntre gramatici LL(1) şi gramaticile LR(k), o gramatică LL(1)
poate să fie LR(1), LALR, SLR sau chiar LR(0), dar orice gramatică LL(1)
este LR(1).
LR(1)
LL(1) LALR(1)
SLR
LR(0)
Figura 3.5: Relaţia dintre diferite clase de gramatici ı̂n funcţie de metoda
de analiză sintactică S.Motogna - FL&CD
Structure of compiler
Source program
analysis
scanning
parsing
Sequence of
tokens
semantic analysis
Parse tree
generate intermediary
Adnotated syntax code synthesis
tree
optimize
Intermediary intermediary code
code
Optimized generate object Object
code program
intermediary
code
S. Motogna - LFTC