curs 8 - cs.ubbcluj.romotogna/lftc/course8.pdf1. s0)§ dr Æaw )dr ÆØw; 2. s0)§ dr ∞bx )dr...

24
Curs 8 LR(k) parsing S.Motogna - FL&CD

Upload: others

Post on 21-Oct-2019

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Curs 8 - cs.ubbcluj.romotogna/LFTC/Course8.pdf1. S0)§ dr ÆAw )dr ÆØw; 2. S0)§ dr ∞Bx )dr ÆØy; 3. FIRSTk (w)=FIRSTk (y) rezult˘a c˘a Æ = Ø,A= B,x = y. Pornind de la defini¸tia

Curs 8LR(k) parsing

S.Motogna - FL&CD

Page 2: Curs 8 - cs.ubbcluj.romotogna/LFTC/Course8.pdf1. S0)§ dr ÆAw )dr ÆØw; 2. S0)§ dr ∞Bx )dr ÆØy; 3. FIRSTk (w)=FIRSTk (y) rezult˘a c˘a Æ = Ø,A= B,x = y. Pornind de la defini¸tia

Terms

• Prediction – see LL(1)• Handle = symbols from the head of the working stack that form (in order)

a rhp

• Shift – reduce parser:• shift symbols to form a handle• When a rhp is formed – reduce to the corresponding lhp

S.Motogna - FL&CD

Reminder:rhp = right handside of productionlhp = left handside of production

Page 3: Curs 8 - cs.ubbcluj.romotogna/LFTC/Course8.pdf1. S0)§ dr ÆAw )dr ÆØw; 2. S0)§ dr ∞Bx )dr ÆØy; 3. FIRSTk (w)=FIRSTk (y) rezult˘a c˘a Æ = Ø,A= B,x = y. Pornind de la defini¸tia

LR(k)

• L = left – sequence is read from left to right

• R = right – use rightmost derivations

• k = length of prediction

• Enhanced grammar

• G = (N, Σ,P,S)

• G’ =(N ∪ {S’},Σ,P ∪ {Sʹ → S},Sʹ), S’∉ N

S.Motogna - FL&CD

S’ does NOT appear in any rhp

Page 4: Curs 8 - cs.ubbcluj.romotogna/LFTC/Course8.pdf1. S0)§ dr ÆAw )dr ÆØw; 2. S0)§ dr ∞Bx )dr ÆØy; 3. FIRSTk (w)=FIRSTk (y) rezult˘a c˘a Æ = Ø,A= B,x = y. Pornind de la defini¸tia

S.Motogna - FL&CD

• Definition 1: If in a gfg G = (N, Σ, P, S) we have S =>r αAw ⇒r αβw, where α ∈ (N ∪Σ)∗,A ∈ N,w ∈ Σ∗, thenany prefix of sequence αβ is called live prefix in G.

• Definition 2: LR(k) item is defined as [A → α.β,u], where A → αβ is a production, u ∈ Σk and describe the moment in which, considering the production A → αβ, α was detected(α is in head of stack) and it is expected to detect β.

• Definition 3: LR(k) item is valid for the live prefix γα if:S⇒r γAw ⇒r γαβwu = FIRSTk(w)

*

*

Page 5: Curs 8 - cs.ubbcluj.romotogna/LFTC/Course8.pdf1. S0)§ dr ÆAw )dr ÆØw; 2. S0)§ dr ∞Bx )dr ÆØy; 3. FIRSTk (w)=FIRSTk (y) rezult˘a c˘a Æ = Ø,A= B,x = y. Pornind de la defini¸tia

Definition 4: A cfg G = (N, Σ, P, S) is LR(k), for k>=0, if

1. S’⇒r αAw ⇒r αβw

2. S’⇒r γBx ⇒r αβy => α = γ AND A =B AND x=y

3. FIRSTk(w) = FIRSTk(y)

S.Motogna - FL&CD

γ

*

*

Page 6: Curs 8 - cs.ubbcluj.romotogna/LFTC/Course8.pdf1. S0)§ dr ÆAw )dr ÆØw; 2. S0)§ dr ∞Bx )dr ÆØy; 3. FIRSTk (w)=FIRSTk (y) rezult˘a c˘a Æ = Ø,A= B,x = y. Pornind de la defini¸tia

• [A → αβ.,u] – rhp detected - apply reduce

• [A → α.β,u] - shift

ÞWorking stack:$sinitX1s1 . . . Xmsm

where: $ - mark empty stackXi ∈N∪∑si - states

S.Motogna - FL&CD

state

action

decide

Page 7: Curs 8 - cs.ubbcluj.romotogna/LFTC/Course8.pdf1. S0)§ dr ÆAw )dr ÆØw; 2. S0)§ dr ∞Bx )dr ÆØy; 3. FIRSTk (w)=FIRSTk (y) rezult˘a c˘a Æ = Ø,A= B,x = y. Pornind de la defini¸tia

LR(k) principle

• Current state• Current symbol• prediction

uniquely determines:• Action to be applied•Move to a new state

=> LR(k) table – 2 parts: action part + goto part

S.Motogna - FL&CD

Page 8: Curs 8 - cs.ubbcluj.romotogna/LFTC/Course8.pdf1. S0)§ dr ÆAw )dr ÆØw; 2. S0)§ dr ∞Bx )dr ÆØy; 3. FIRSTk (w)=FIRSTk (y) rezult˘a c˘a Æ = Ø,A= B,x = y. Pornind de la defini¸tia

States What a state contains?

• LR items• closure

How many states? How to go from one state to another state?• goto

S.Motogna - FL&CD

Page 9: Curs 8 - cs.ubbcluj.romotogna/LFTC/Course8.pdf1. S0)§ dr ÆAw )dr ÆØw; 2. S0)§ dr ∞Bx )dr ÆØy; 3. FIRSTk (w)=FIRSTk (y) rezult˘a c˘a Æ = Ø,A= B,x = y. Pornind de la defini¸tia

• [A → α.Bβ,u] valid for live prefix γα =>

• B → δ ∈P =>

=> [B → .δ,u] valid for live prefix γα

S.Motogna - FL&CD

Analizoarele sintactice LR(k) lucreaza cu gramatica ımbogatita: G =(N [ {S0}, ß, P [ {S0 ! S}, S0), S0 /2 N , pentru a evita ca simbolul destart S sa apara ın partea dreapta a unei productii. Se observa ca celedoua gramatici sunt echivalente (genereaza acelasi limbaj) si ın aceastanoua gramatica S0 nu apare ın partea dreapta a nici unei productii.

Definitia 3.4. [Ser87] O gramatica G = (N, ß, P, S) este de tip LR(k)pentru k ∏ 0 daca din:

1. S0 §)dr ÆAw )dr ÆØw;

2. S0 §)dr ∞Bx)dr ÆØy;

3. FIRSTk(w) = FIRSTk(y)

rezulta ca Æ = Ø, A = B, x = y.

Pornind de la definitia elementului de analiza sa consideram un cazparticular [A ! ÆØ., u]. Semnificatia acestui element de analiza este caÆØ a fost deja detectat ın arbore si astfel s-ar putea reduce subarborelecu frontul ÆØ la neterminalul A. Astfel, am determinat cand (ın ce starea automatului push-down) se poate face reducerea si care este mansa.Deci, cand se ajunge ıntr-o stare ce contine element de analiza de forma[A ! ÆØ., u] actiunea va fi de reducere, altfel va fi deplasare. Deoarecestarea automatului decide actiunea, ea va fi de asemenea depusa ın stivade lucru. Configuratia stivei va fi:

$sinitX1

s1

. . . Xmsm

unde Xm este simbolul din varful stivei, iar sm este starea curenta a au-tomatului. Cele doua elemente ımpreuna cu predictia de lungime k vordetermina comportamentul analizorului, caracterizat prin:

• actiunea care se va efectua si

• tranzitia ın alta stare.

De aceea tabelele de analiza LR(k) au doua componente: de actiune side deplasare, numita ”goto“.

Care sunt si cum se determina aceste stari? Pentru a raspunde saconsideram elementul de analiza [A ! Æ.BØ, u] care, conform definitiei,implica:

S§)dr ∞Aw )dr ∞ÆBØw si

u = FIRSTk(w) valabil pentru prefixul viabil ∞Æ.Daca ın gramatica exista o productie B ! ± atunci elementul de analiza

[B ! .±, u] are, de asemenea, u valid pentru prefixul viabil ∞Æ:

67

Analizoarele sintactice LR(k) lucreaza cu gramatica ımbogatita: G =(N [ {S0}, ß, P [ {S0 ! S}, S0), S0 /2 N , pentru a evita ca simbolul destart S sa apara ın partea dreapta a unei productii. Se observa ca celedoua gramatici sunt echivalente (genereaza acelasi limbaj) si ın aceastanoua gramatica S0 nu apare ın partea dreapta a nici unei productii.

Definitia 3.4. [Ser87] O gramatica G = (N, ß, P, S) este de tip LR(k)pentru k ∏ 0 daca din:

1. S0 §)dr ÆAw )dr ÆØw;

2. S0 §)dr ∞Bx)dr ÆØy;

3. FIRSTk(w) = FIRSTk(y)

rezulta ca Æ = Ø, A = B, x = y.

Pornind de la definitia elementului de analiza sa consideram un cazparticular [A ! ÆØ., u]. Semnificatia acestui element de analiza este caÆØ a fost deja detectat ın arbore si astfel s-ar putea reduce subarborelecu frontul ÆØ la neterminalul A. Astfel, am determinat cand (ın ce starea automatului push-down) se poate face reducerea si care este mansa.Deci, cand se ajunge ıntr-o stare ce contine element de analiza de forma[A ! ÆØ., u] actiunea va fi de reducere, altfel va fi deplasare. Deoarecestarea automatului decide actiunea, ea va fi de asemenea depusa ın stivade lucru. Configuratia stivei va fi:

$sinitX1

s1

. . . Xmsm

unde Xm este simbolul din varful stivei, iar sm este starea curenta a au-tomatului. Cele doua elemente ımpreuna cu predictia de lungime k vordetermina comportamentul analizorului, caracterizat prin:

• actiunea care se va efectua si

• tranzitia ın alta stare.

De aceea tabelele de analiza LR(k) au doua componente: de actiune side deplasare, numita ”goto“.

Care sunt si cum se determina aceste stari? Pentru a raspunde saconsideram elementul de analiza [A ! Æ.BØ, u] care, conform definitiei,implica:

S§)dr ∞Aw )dr ∞ÆBØw si

u = FIRSTk(w) valabil pentru prefixul viabil ∞Æ.Daca ın gramatica exista o productie B ! ± atunci elementul de analiza

[B ! .±, u] are, de asemenea, u valid pentru prefixul viabil ∞Æ:

67

S§) ∞Aw )dr ∞ÆBØw )dr ∞ƱØw.

Aceasta observatie sugereaza faptul ca elementele de analiza cores-punzatoare unui acelasi prefix viabil ar trebui grupate ıntr-o multime si caaceasta multime caracterizeaza un pas al analizei, deci va forma o stare.Multimea care va contine toate elementele de analiza corespunzatoare unuiprefix viabil va forma o stare a automatului.

Starea initiala este cea care corespunde prefixului vid (ınca nu s-a cititnimic de pe banda de intrare), deci va contine elementul de analiza [S0 !.S, u]. Starea finala, corespunzatoare acceptarii unei secvente, va trebuisa contina elementul de analiza [S0 ! S., $] pentru a impune o actiune dereducere la S0, iar predictia este vida, adica la radacina arborelui, ceea cecorespunde construirii complete a arborelui de analiza sintactica.

Toate analizoarele sintactice LR au aceste caracteristici generale. Mo-dul de calcul al starilor automatului si constructia tabelului de analiza vorfi particularizate pentru diferite tipuri de analizoare LR, si anume: LR(0),SLR, LR(1), LALR.

Prezentarea fiecarui analizor va urma pasii:

• prezentarea elementului de analiza;

• construirea multimii de stari;

• construirea tabelului de analiza;

• functionarea analizorului.

3.3.2. Analizor sintactic LR(0)

Elementul de analiza LR(0) are forma [A ! Æ.Ø] (predictia fiindvida, ea dispare din elementul de analiza), ceea ce ınseamna ca vom aplicaprincipiile analizei sintactice LR de determinare a mansei, deplasare sireducere, dar fara sa tinem seama de predictie (de ce simbol urmeaza pebanda de intrare).

Constructia multimii de stari: Dupa cum am mai precizat o stareeste o multime care contine toate elementele de analiza corespunzatoareunui prefix viabil. Vom nota cu E

0

multimea elementelor de analiza LR(0).Vom folosi o functie de ınchidere pentru a construi aceasta multime[ASU86]:

closure : P(E0

)! P(E0

)care are urmatoarele doua proprietati importante, pe baza carora vomelabora algoritmul 3.8:

1. I 2 P(E0

), I µ closure(I)

68

Page 10: Curs 8 - cs.ubbcluj.romotogna/LFTC/Course8.pdf1. S0)§ dr ÆAw )dr ÆØw; 2. S0)§ dr ∞Bx )dr ÆØy; 3. FIRSTk (w)=FIRSTk (y) rezult˘a c˘a Æ = Ø,A= B,x = y. Pornind de la defini¸tia

LR(k) parsing:LR(0), SLR, LR(1), LALR

• Define item• Construct set of states• Construct table

• Parse sequence based on moves between configurations

S.Motogna - FL&CD

Executed 1 time

Page 11: Curs 8 - cs.ubbcluj.romotogna/LFTC/Course8.pdf1. S0)§ dr ÆAw )dr ÆØw; 2. S0)§ dr ∞Bx )dr ÆØy; 3. FIRSTk (w)=FIRSTk (y) rezult˘a c˘a Æ = Ø,A= B,x = y. Pornind de la defini¸tia

LR(0) Parser

• Prediction of length 0 (ignored)

1. LR(0) item: [A → α.β]

S.Motogna - FL&CD

Page 12: Curs 8 - cs.ubbcluj.romotogna/LFTC/Course8.pdf1. S0)§ dr ÆAw )dr ÆØw; 2. S0)§ dr ∞Bx )dr ÆØy; 3. FIRSTk (w)=FIRSTk (y) rezult˘a c˘a Æ = Ø,A= B,x = y. Pornind de la defini¸tia

2. Construct set of states

• What a state contains – Algorithm closure_LR0• How to move from a state to another – Function goto_LR0• Construct set of states – Algorithm ColCan_LR0

S.Motogna - FL&CD

Canonical collection

Page 13: Curs 8 - cs.ubbcluj.romotogna/LFTC/Course8.pdf1. S0)§ dr ÆAw )dr ÆØw; 2. S0)§ dr ∞Bx )dr ÆØy; 3. FIRSTk (w)=FIRSTk (y) rezult˘a c˘a Æ = Ø,A= B,x = y. Pornind de la defini¸tia

Algorithm Closure

2. closure(I) = I [ {[B ! .±]|[A ! Æ.BØ] 2 I}, conform observatieidin paragraful anterior.

Algoritmul 3.8 ClosureLR0INPUT: I-element de analiza; G’- gramatica ımbogatitaOUTPUT: C = closure(I);C := {I};repeat

for 8[A ! Æ.BØ] 2 C dofor 8B ! ∞ 2 P do

if [B ! .∞] /2 C thenC = C [ [B ! .∞]

end ifend for

end foruntil C nu se mai modifica

Pentru a determina starile si cum se deplaseaza automatul dintr-o stareın alta stare, ceea ce ınseamna ca se proceseaza un simbol, vom folosi ofunctie notata goto [ASU86]:

goto : P(E0

)£ (N [ ß) ! P(E0

)Daca [A ! Æ.XØ] este un element din starea si, procesarea lui X

ınseamna ca X a fost detectat si partea neprelucrata va contine doar Ø,deci vom ajunge ıntr-o noua stare care va contine elementul [A ! ÆX.Ø]:

goto(s,X) = closure({[A ! ÆX.Ø]|[A ! Æ.XØ] 2 s}).

Starile automatului formeaza o colectie canonica:

• colectie - pentru ca trebuie sa calculam toate posibilitatile de ”de-plasare“ ale automatului: pentru toate starile s si pentru toate sim-bolurile gramaticii X 2 N [ ß vom calcula goto(s,X);

• canonica - pentru ca daca printr-un calcul goto(s,X) obtinem o stareq deja existenta, ea nu va mai fi adaugata la colectie, ın schimbtrebuie memorata aceasta tranzitie a automatului: goto(s,X) = q.

Colectia de stari se construieste dupa algoritmul 3.9.

Exemplul 3.4. Fie G = ({S0, S, A}, {a, b, c}, P, S0), unde productiile sunt:S0 ! SS ! aAA ! bA

69

S.Motogna - FL&CD

Page 14: Curs 8 - cs.ubbcluj.romotogna/LFTC/Course8.pdf1. S0)§ dr ÆAw )dr ÆØw; 2. S0)§ dr ∞Bx )dr ÆØy; 3. FIRSTk (w)=FIRSTk (y) rezult˘a c˘a Æ = Ø,A= B,x = y. Pornind de la defini¸tia

Function goto

goto : P(ℰ0) × (N ∪ Σ) → P(ℰ0)where ℰ0 = set of LR(0) items

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

S.Motogna - FL&CD

Page 15: Curs 8 - cs.ubbcluj.romotogna/LFTC/Course8.pdf1. S0)§ dr ÆAw )dr ÆØw; 2. S0)§ dr ∞Bx )dr ÆØy; 3. FIRSTk (w)=FIRSTk (y) rezult˘a c˘a Æ = Ø,A= B,x = y. Pornind de la defini¸tia

Algorithm ColCan_LR(0)Algoritmul 3.9 Col stariLR0

INPUT: G’- gramatica ımbogatitaOUTPUT: C - colectia canonica de stariC := ;;s0

:= closure({[S0 ! .S]})C := C [ {s

0

};repeat

for 8s 2 C dofor 8X 2 N [ ß do

if goto(s,X) 6= ; and goto(s, X) /2 C thenC = C [ goto(s,X)

end ifend for

end foruntil C nu se mai modifica

A ! cs0

= closure({[S0 ! .S]}) = {[S0 ! .S], [S ! .aA]} { initializarea}Pentru a construi goto(s,X) va trebui sa cautam ın starea s un element

de analiza care sa contina ’.‘ ınaintea lui X. Construim elementul deanaliza cu ’.‘ dupa X si aplicam algoritmul de ınchidere:

s1

= goto(s0

, S) = closure({S0 ! S.}) = {[S0 ! S.]}s2

= goto(s0

, a) = closure({[S ! a.A]} = {[S ! a.A],[A ! .bA], [A ! .c]}goto(s

0

, b) = ;goto(s

0

, c) = ;goto(s

1

,X) = ;,8X 2 N [ ßs3

= goto(s2

, A) = closure({[S ! aA.]}) = {[S ! aA.]}s4

= goto(s2

, b) = closure({[A ! b.A]}) = {[A ! b.A],[A ! .bA], [A ! .c]}

s5

= goto(s2

, c) = closure({[A ! c.]}) = {[A ! c.]}s6

= goto(s4

, a) = closure({[A ! bA.]}) = {[A ! bA.]}goto(s

4

, b) = s4

goto(s4

, c) = s5

70

S.Motogna - FL&CD

Page 16: Curs 8 - cs.ubbcluj.romotogna/LFTC/Course8.pdf1. S0)§ dr ÆAw )dr ÆØw; 2. S0)§ dr ∞Bx )dr ÆØy; 3. FIRSTk (w)=FIRSTk (y) rezult˘a c˘a Æ = Ø,A= B,x = y. Pornind de la defini¸tia

3. Construct LR(0) table

• one line for each state

• 2 parts:• Action: one column (for a state, action is unique because prediction is

ignored)• Goto: one column for each symbol X ∈ N ∪ Σ

S.Motogna - FL&CD

Page 17: Curs 8 - cs.ubbcluj.romotogna/LFTC/Course8.pdf1. S0)§ dr ÆAw )dr ÆØw; 2. S0)§ dr ∞Bx )dr ÆØy; 3. FIRSTk (w)=FIRSTk (y) rezult˘a c˘a Æ = Ø,A= B,x = y. Pornind de la defini¸tia

Rules LR(0) table

1. if [A → α.β] ∈ si then action(si)=shift2. if [A → β.] ∈ si and A ≠ Sʹ then action(si)=reduce l, where l =

number of production A → β3. if [Sʹ → S.] ∈ si then action(si)=acc4. if goto(si, X) = sj then goto(si, X) = sj

5. otherwise = error

S.Motogna - FL&CD

Page 18: Curs 8 - cs.ubbcluj.romotogna/LFTC/Course8.pdf1. S0)§ dr ÆAw )dr ÆØw; 2. S0)§ dr ∞Bx )dr ÆØy; 3. FIRSTk (w)=FIRSTk (y) rezult˘a c˘a Æ = Ø,A= B,x = y. Pornind de la defini¸tia

Remarks

1) Initial state of parser = statea containing [Sʹ → .S] 2) No shift from accept state:

if s is accept state then goto(s, X) = ∅, ∀X ∈ N ∪ Σ. 3) If in state s action is reduce then goto(s, X) = ∅, ∀X ∈ N ∪ Σ. 4) Argument G’: Let G = ({S},{a,b,c},{S → aSbS,S → c},S)

states [S → aSbS.] and [S → c.] – accept / reduce ?

S.Motogna - FL&CD

Page 19: Curs 8 - cs.ubbcluj.romotogna/LFTC/Course8.pdf1. S0)§ dr ÆAw )dr ÆØw; 2. S0)§ dr ∞Bx )dr ÆØy; 3. FIRSTk (w)=FIRSTk (y) rezult˘a c˘a Æ = Ø,A= B,x = y. Pornind de la defini¸tia

Remarks (cont)

5) A grammar is NOT LR(0) if the LR(0) table contains conflicts: • shift – reduce conflict: a state contains items of the form [A → α.β]

and [B → γ.], yielding to 2 distinct actions for that state

• reduce – reduce conflict: when a state contains items of the form [A → αβ.] and [B → γ.], in which the action is reduce, but with distinct productions

S.Motogna - FL&CD

Page 20: Curs 8 - cs.ubbcluj.romotogna/LFTC/Course8.pdf1. S0)§ dr ÆAw )dr ÆØw; 2. S0)§ dr ∞Bx )dr ÆØy; 3. FIRSTk (w)=FIRSTk (y) rezult˘a c˘a Æ = Ø,A= B,x = y. Pornind de la defini¸tia

4. Define configurations and moves

• INPUT: • Grammar G’ = (NU{S’}, !, P U {S’->S},S’)• LR(0) table• Input sequence w =a1…an

• OUTPUT:if (w ∈L(G)) then string of productions

else error & location of error

S.Motogna - FL&CD

Page 21: Curs 8 - cs.ubbcluj.romotogna/LFTC/Course8.pdf1. S0)§ dr ÆAw )dr ÆØw; 2. S0)§ dr ∞Bx )dr ÆØy; 3. FIRSTk (w)=FIRSTk (y) rezult˘a c˘a Æ = Ø,A= B,x = y. Pornind de la defini¸tia

LR(0) configurations

(!, ", #)

where:• ! = working stack• " = input stack• # = output (result)

S.Motogna - FL&CD

Final configuration:($sacc, $, #)

Initial configuration:($s0,w$,$)

Page 22: Curs 8 - cs.ubbcluj.romotogna/LFTC/Course8.pdf1. S0)§ dr ÆAw )dr ÆØw; 2. S0)§ dr ∞Bx )dr ÆØy; 3. FIRSTk (w)=FIRSTk (y) rezult˘a c˘a Æ = Ø,A= B,x = y. Pornind de la defini¸tia

Moves1. Shift

if action(sm)= shift AND head(!)=ai AND goto(sm,ai)=Sj then($s0x1 ...xmsm,ai ...an$, ") ⊢ ($s0x1 ...xmsmaisj,ai+1 ...an$, ")

2. Reduceif action(sm) = reduce l AND (t) A → xm−p+1 ...xm AND goto(sm−p,A) = sj then

($s0 ...xmsm,ai ...an$, ") ⊢ ($s0 ...xm−psm−pAsj,ai ...an$,l ") 3. Accept

if action(sm) = accept then ($si,$, ")=acc4. Error - otherwise

S.Motogna - FL&CD

Page 23: Curs 8 - cs.ubbcluj.romotogna/LFTC/Course8.pdf1. S0)§ dr ÆAw )dr ÆØw; 2. S0)§ dr ∞Bx )dr ÆØy; 3. FIRSTk (w)=FIRSTk (y) rezult˘a c˘a Æ = Ø,A= B,x = y. Pornind de la defini¸tia

LR(0) Parsing Algorithm

INPUT:- LR(0) table – conflict free- grammar G’: production numbered

• - sequence = Input sequence w =a1…an

• OUTPUT:if (w ∈L(G)) then string of productions

else error & location of error

S.Motogna - FL&CD

Page 24: Curs 8 - cs.ubbcluj.romotogna/LFTC/Course8.pdf1. S0)§ dr ÆAw )dr ÆØw; 2. S0)§ dr ∞Bx )dr ÆØy; 3. FIRSTk (w)=FIRSTk (y) rezult˘a c˘a Æ = Ø,A= B,x = y. Pornind de la defini¸tia

LR(0) Parsing Algorithm

state :=0; alpha := ‘$s0’; beta :=‘w$’; phi := ‘’; end:= falseRepeat

if action(state)=‘shift’ thent = pop(beta);state = goto(state,t)push(t, state, alpha);

else

if action(state) =’reduce l” thensearch_prod(t,rhp,lhp);pop(rhp,alpha);state = goto(head(alpha.state),lhp);push(lhp,state,alpha);push(t,out);

elseif action(state)=‘accept’ then

write(” success”, out);end := true;

if action(state) = ‘error’ thenwrite(” error”)

end := trueUntil end

S.Motogna - FL&CD