0% found this document useful (0 votes)
21 views33 pages

Course 9

The document summarizes LR(k) parsing techniques, including LR(0), SLR, and LR(1) parsers. It discusses how LR(k) parsers are constructed by first defining parsing items, constructing a set of parser states using closure operations, and then using the states to construct a parsing table to guide the shift-reduce parsing process. The key aspects of SLR and LR(1) parsers are that they add lookahead symbols to items to resolve shift/reduce conflicts that occur in LR(0) parsing.

Uploaded by

Veko Boy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views33 pages

Course 9

The document summarizes LR(k) parsing techniques, including LR(0), SLR, and LR(1) parsers. It discusses how LR(k) parsers are constructed by first defining parsing items, constructing a set of parser states using closure operations, and then using the states to construct a parsing table to guide the shift-reduce parsing process. The key aspects of SLR and LR(1) parsers are that they add lookahead symbols to items to resolve shift/reduce conflicts that occur in LR(0) parsing.

Uploaded by

Veko Boy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Course 9

LR(k) Parsing (cont.)

S.Motogna - FL&CD
LR(k) parsing:
LR(0), SLR, LR(1), LALR
• Define item
• Construct set of states Executed 1 time
• Construct table

• Parse sequence based on moves between configurations

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->𝜶.𝜷]

INPUT: I-element de analiză; G’- gramatica ı̂mbogăţită


OUTPUT: C = closure(I);
C := {I}; //initialize Closure with the LR(0) item
repeat
for 8[A ! Æ.BØ] 2 C do //search productions with dot in front of nonterminal
for 8B ! ∞ 2 P do //search productions of that nonterminal
if [B ! .∞] 2/ C then
C = C [ [B ! .∞] //adds item formed from production with dot in
end if //front of right hand side of the production
end for
end for
until C nu se mai modifică

Pentru a determina stărileS.Motogna


şi cum se deplasează automatul dintr-o stare
- FL&CD
Function goto
goto : P(ℰ0) × (N ∪ Σ) → P(ℰ0) //creates new states

where ℰ0 = set of LR(0) items

goto(s, X) = closure({[A → αX.β]|[A → α.Xβ] ∈ s})

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:

• define item LR(0)


• Construct set of states LR(0)
• Construct table
• Parse sequence based on moves between configurations

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

2. if [A → β.] ∈ si and A ≠ Sʹ then action(si,u)=reduce l, where l –


number of production A → β, ∀u ∈ FOLLOW(A)
//dot is at the end, but not for S’

3. if [Sʹ → S.] ∈ si then action(si,$)=acc


// dot is at the end, prod. of S’

4. if goto(si, X) = sj then goto(si, X) = sj , ∀X ∈N


5. otherwise error

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]

1. Define item Kernel prediction


2. Construct set of states
3. Construct table
4. Parse sequence based on moves between configurations

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

Goto_LR1(s, X) = Closure_LR1({[A → αX.β,u]|[A → α.Xβ,u] ∈ s})

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ă

Definiţia funcţiei goto se actualizează ı̂n:


S.Motogna - FL&CD
Construct LR(1) table
• Structure – SLR
• Rules:
1. if [A → α.β,u] ∈ si and goto(si,a) = sj then action(si,a)=shift sj
2. if [A → β.,u] ∈ si and A ≠ Sʹ then action(si,u)=reduce l, where l –
number of production A → β
3. if [Sʹ → S.,$] ∈ si then action(si,$)=acc
4. if goto(si, X) = sj then goto(si, X) = sj , ∀X ∈N
5. otherwise = error

S.Motogna - FL&CD
Remarks
1. A grammar is LR(1) if the LR(1) table does not contain conflicts

2. Number of states – significantly increase

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

• Merge states with the same kernel, conserving all predictions, if no


conflict is created

S.Motogna - FL&CD
LALR Parsing
• Same as LR(1)
• Number of LALR states = number of SLR / LR(0) states

• How? - LR(1) 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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy