S untaksanal u us...Shift-reduce parsimine Shift-reduce parsimineon uldine meetod alt- ules s...
Transcript of S untaksanal u us...Shift-reduce parsimine Shift-reduce parsimineon uldine meetod alt- ules s...
S�untaksanal�u�us
Shift-reduce parsimineLR(0) parsimineSLR(1) parsimine
Shift-reduce parsimine
Shift-reduce parsimine on �uldine meetod alt-�uless�untaksanal�u�usiks:
puu konstrueerimine toimub lehtedest juure suunaseesm�argiga "taandada" sisendstring algs�umboliks;
parsimise ajal on korraga terve mets puid, mis vastavaderinavatele, juba �aratuntud, alamstringidele;
kaks baasaktsiooni:
{ shift loeb uue sisends�umboli;{ reduce taandab mingi produktsioonireegli parempoolelevastava (juba loetud/taandatud sisends�umbolite jamitteterminalide) jada reegli vasakpoolel olevaksmitteterminaliks;
konstruktsioon vastab paremderivatsioonile.
2 / 19
Shift-reduce parsimine
N�aide:
S ! aAB e
A ! b cA j c
B ! d
shiftshiftshiftshiftreduce A! c
reduce A! b cA
shiftreduce B ! d
shiftreduce S ! aAB e
Input String: � a b c c d e
Stack:
a b c c
A
A
d
B
e
S
S =)rm aAB e =)rm aAd e
=)rm a b cAd e =)rm a b c c d e
3 / 19
Shift-reduce parsimine
N�aide:
S ! aAB e
A ! b cA j c
B ! d
shiftshiftshiftshiftreduce A! c
reduce A! b cA
shiftreduce B ! d
shiftreduce S ! aAB e
Input String: a � b c c d e
Stack: a
a b c c
A
A
d
B
e
S
S =)rm aAB e =)rm aAd e
=)rm a b cAd e =)rm a b c c d e
3 / 19
Shift-reduce parsimine
N�aide:
S ! aAB e
A ! b cA j c
B ! d
shiftshiftshiftshiftreduce A! c
reduce A! b cA
shiftreduce B ! d
shiftreduce S ! aAB e
Input String: a b � c c d e
Stack: a b
a b c c
A
A
d
B
e
S
S =)rm aAB e =)rm aAd e
=)rm a b cAd e =)rm a b c c d e
3 / 19
Shift-reduce parsimine
N�aide:
S ! aAB e
A ! b cA j c
B ! d
shiftshiftshiftshiftreduce A! c
reduce A! b cA
shiftreduce B ! d
shiftreduce S ! aAB e
Input String: a b c � c d e
Stack: a b c
a b c c
A
A
d
B
e
S
S =)rm aAB e =)rm aAd e
=)rm a b cAd e =)rm a b c c d e
3 / 19
Shift-reduce parsimine
N�aide:
S ! aAB e
A ! b cA j c
B ! d
shiftshiftshiftshiftreduce A! c
reduce A! b cA
shiftreduce B ! d
shiftreduce S ! aAB e
Input String: a b c c � d e
Stack: a b c c
a b c c
A
A
d
B
e
S
S =)rm aAB e =)rm aAd e
=)rm a b cAd e =)rm a b c c d e
3 / 19
Shift-reduce parsimine
N�aide:
S ! aAB e
A ! b cA j c
B ! d
shiftshiftshiftshiftreduce A! c
reduce A! b cA
shiftreduce B ! d
shiftreduce S ! aAB e
Input String: a b c c � d e
Stack: a b cA
a b c c
A
A
d
B
e
S
S =)rm aAB e =)rm aAd e
=)rm a b cAd e =)rm a b c c d e
3 / 19
Shift-reduce parsimine
N�aide:
S ! aAB e
A ! b cA j c
B ! d
shiftshiftshiftshiftreduce A! c
reduce A! b cA
shiftreduce B ! d
shiftreduce S ! aAB e
Input String: a b c c � d e
Stack: aA
a b c c
A
A
d
B
e
S
S =)rm aAB e =)rm aAd e
=)rm a b cAd e =)rm a b c c d e
3 / 19
Shift-reduce parsimine
N�aide:
S ! aAB e
A ! b cA j c
B ! d
shiftshiftshiftshiftreduce A! c
reduce A! b cA
shiftreduce B ! d
shiftreduce S ! aAB e
Input String: a b c c d � e
Stack: aAd
a b c c
A
A
d
B
e
S
S =)rm aAB e =)rm aAd e
=)rm a b cAd e =)rm a b c c d e
3 / 19
Shift-reduce parsimine
N�aide:
S ! aAB e
A ! b cA j c
B ! d
shiftshiftshiftshiftreduce A! c
reduce A! b cA
shiftreduce B ! d
shiftreduce S ! aAB e
Input String: a b c c d � e
Stack: aAB
a b c c
A
A
d
B
e
S
S =)rm aAB e =)rm aAd e
=)rm a b cAd e =)rm a b c c d e
3 / 19
Shift-reduce parsimine
N�aide:
S ! aAB e
A ! b cA j c
B ! d
shiftshiftshiftshiftreduce A! c
reduce A! b cA
shiftreduce B ! d
shiftreduce S ! aAB e
Input String: a b c c d e �
Stack: aAB e
a b c c
A
A
d
B
e
S
S =)rm aAB e =)rm aAd e
=)rm a b cAd e =)rm a b c c d e
3 / 19
Shift-reduce parsimine
N�aide:
S ! aAB e
A ! b cA j c
B ! d
shiftshiftshiftshiftreduce A! c
reduce A! b cA
shiftreduce B ! d
shiftreduce S ! aAB e
Input String: a b c c d e �
Stack: S
a b c c
A
A
d
B
e
S
S =)rm aAB e =)rm aAd e
=)rm a b cAd e =)rm a b c c d e
3 / 19
Shift-reduce parsimine
N�aide:
S ! aAB e
A ! b cA j c
B ! d
shiftshiftshiftshiftreduce A! c
reduce A! b cA
shiftreduce B ! d
shiftreduce S ! aAB e
Input String:
Stack:
a b c c
A
A
d
B
e
S
S =)rm aAB e =)rm aAd e
=)rm a b cAd e =)rm a b c c d e
3 / 19
Shift-reduce parsimine
Lausevormi nimetatakse paremlausevormiks (right-sententialform), kui ta on tuletatav paremderivatsiooni abil.
Paremlausevormi pide (handle) on produktsiooniree-gel A ! � ja s~onas alamss~ona � esinemise positsioon,mis paremderivatsiooni eelmises lausevormis on asendatudmitteterminaliga A.
Ekvivalentselt, paremlausevormi pide on alamstring �,selline et S =)?
rm�Aw =)rm ��w = , kus �; ; � 2 V ? ja
w 2 T ?.
Pidemete leidmise ja nende taandamise protsessi nimeta-takse "pidemete k�arpimiseks" ("handle pruning").
NB! �Uhese grammatika korral on paremderivatsioon, jaseega ka pidemed, unikaalsed.
4 / 19
Shift-reduce parsimine
N�aide: olgu antud grammatika
S ! aAB e
A ! b cA j c
B ! d
ja paremderivatsioon
S =)rm aAB e =)rm aAd e =)rm a b cAd e
Paremlausevormi abcAde pide on bcA.
5 / 19
Shift-reduce parsimine
Shift-reduce parseris tekib pide enne taandamist magasinitippu.
Paremlausevormi pre�kseid, mis ei l~ope kaugemal paremalkui selles lausevormis olev pide, nimetatakse eluj~oulisteks(viable).
Eluj~oulised pre�ksid on shift-reduce parseri v~oimalikeksmagasinideks!
NB! "Eluj~ouliste pre�ksite keel" on regulaarne!
J�arelikult leidub l~oplik automaat, mis tunneb �ara eluj~oulisipre�kseid.
See automaat on k~oigi LR-parserite p~ohikomponendiks.
6 / 19
Shift-reduce parsimine
LR(0)-element (v~oi lihtsalt element; ingl. item) on produkt-sioonireegel, mille parempoolde on kuhugi lisatud punkt.
N�aide: olgu antud grammatika
S ! aA c
A ! Ab j "
Tema LR(0)-elemendid on:
[S ! � aA c][S ! a � Ac][S ! aA � c][S ! aA c �]
[A! � Ab][A! A � b][A! Ab �][A! �]
7 / 19
Shift-reduce parsimine
Element [A ! � � �] on eluj~oulise pre�ksi ' suhtes kehtiv(valid), kui leidub paremderivatsioon
S =)?rm
�Aw =)rm ���w
ja �� = '.
Kehtiv element [A! � � �] t�ahendab, et seni n�ahtud sisendon koosk~olas reegli A ! �� rakendamisega ja parser onjuba �ara tundnud �.
Kehtiv element [A ! ���] t�ahendab, et parser on �aratundnud �� ja on koosk~olas selle redutseerimisega mitteter-minaliks A.
8 / 19
Shift-reduce parsimine
Mittedetermineeritud LR(0)-automaat on NFA, kus oleku-teks on elemendid ja kus on kaht liiki �uleminekuid:
{ elementide [A ! � � X�] ja [A ! �X � �] vahel on(terminal- v~oi mitteterminal-) s�umboliga X m�argendatud
�uleminek
[A ! � �X�] [A ! �X � �]X
{ elementide [A! ��X�] ja [X ! � ] vahel on t�uhis�umboliga" m�argendatud �uleminek
[A ! � �X�] [X ! � ]"
Grammatika on laiendatud uue algs�umboliga S0 ja �uhe uuereegliga S0 ! S.
Elementi [S0 ! �S] sisaldav olek on automaadi algolekuks.
9 / 19
Shift-reduce parsimineS ! aAB e
A ! b cA j c
B ! d
10 / 19
Shift-reduce parsimineS ! aAB e
A ! b cA j c
B ! d
[S0! �S]
[S ! � aABe]
[A! � bcA]
[A! � c]
[B ! � d]
[S0! S �]
[S ! a � ABe]
[A! b � cA]
[A! c �]
[B ! d �]
[S ! aA � Be]
[A! bc � A]
[S ! aAB � e]
[A! bcA �]
[S ! aABe �]
S
a A B e
b c A
c
d
10 / 19
Shift-reduce parsimineS ! aAB e
A ! b cA j c
B ! d
[S0! �S]
[S ! � aABe]
[A! � bcA]
[A! � c]
[B ! � d]
[S0! S �]
[S ! a � ABe]
[A! b � cA]
[A! c �]
[B ! d �]
[S ! aA � Be]
[A! bc � A]
[S ! aAB � e]
[A! bcA �]
[S ! aABe �]
S
a A B e
b c A
c
d
"
"
"
"
"
"
10 / 19
Shift-reduce parsimine
NFA-st saame konstrueerida DFA kasutades osahulkademoodustamise konstruktsiooni.
DFA olekuteks on elementide hulgad.
Elemente, mis on mitte-"-�uleminekute sihiks, nimetataksetuumelementideks (kernel items).
Elemente, mis on "-�uleminekute sihiks, nimetatakse sulund-elementideks (closure items).
Tuumelemendid identi�tseerivad �uheselt oleku.
11 / 19
Shift-reduce parsimine
[S0! �S]
[S ! � aABe][S0
! S �]
[S ! a � ABe][A! � bcA][A! � c]
[S ! aA � Be][B ! � d]
[S ! aAB � e]
[S ! aABe �]
[A! b � cA][A! bc � A][A! � bcA][A! � c]
[A! bcA �][A! c �]
[B ! d �]
S
a
A
b
c
B
d
e
c
Ac
b
12 / 19
Shift-reduce parsimine
LR(0) parsimisalgoritm:
push([S0 ! �S]);while true do fstate := top();if 9[A! � �X�] 2 state ^X 2 T thenY := getToken();if 9[A! � � Y �] 2 state then /* shift */push([A! �Y � �]);
else error;else if 9[A! �] 2 state then
if A = S0 ^ = S then accept;else pop(j j); state := top(); /* reduce */
if 9[B ! � � A�] 2 state thenpush([B ! �A � �]);
else error;else error;
g
13 / 19
Shift-reduce parsimine
Grammatika on LR(0), kui iga t�aiselement (so. elementkujul [A! ��] 2 state) on teda sisaldava oleku state
ainsaks elemendiks.
{ Shift-reduce kon ikt: kui 9[B ! � � �] 2 state ;{ Reduce-reduce kon ikt: kui 9[B ! ��] 2 state .
Reduce olekud: t�aiselemente sisaldavad olekud.
Shift olekud: k~oik �ulej�a�anud olekud.
14 / 19
Shift-reduce parsimineS0 ! S
S ! ( S ) S j "
[S0! �S]
[S ! � (S)S]
[S ! (�S)S]
[S0! S �]
[S ! �]
[S ! (S�)S]
[S ! (S)S �]
[S ! (S) � S]
"
( "
S
"
"
S )
S"
"
15 / 19
Shift-reduce parsimine
[S0! �S]
[S ! � (S)S][S ! �]
[S ! ( � S)S][S ! � (S)S][S ! �]
[S ! (S) � S][S ! � (S)S][S ! �]
[S ! (S)S �]
[S ! (S � )S]
[S0! S �]
S
(S
)
( S
(
16 / 19
Shift-reduce parsimine
SLR(1) = Simple LR(1) parsimine.
LR(0) parsimise efektiivne laiendus, mis on piisavalt v~oimasmitmete praktiliste keelte k�asitlemiseks.
Kasutab LR(0) elementidest moodustatud DFA-d.
Sarnane LR(0) parsimisele, kuid aktsiooni valiku otsustuson l�ukatud v~oimalikult hiliseks.
{ Kontrollib sisends�umbolit enne shift'i, et kindlustadavastava DFA �ulemineku olemasolu.
{ Kasutab mitteterminali Follow hulka, et otsustadamilline reduktsioon teha.
17 / 19
Shift-reduce parsimine
SLR(1) parsimisalgoritm:
push([S0 ! �S]);while true do fstate := top(); X := getToken();if 9[A! � �X�] 2 state then /* shift */push([A! �X � �]);
else if 9[A! �] 2 state ^X 2 Follow(A) thenif A = S0 ^ = S ^X = $ then accept;else pop(j j); state := top(); /* reduce */
if 9[B ! � � A�] 2 state thenpush([B ! �A � �]);
else error;else error;
g
18 / 19
Shift-reduce parsimine
Grammatika on SLR(1) kui tema DFA-s ei ole j�argnevaidkon ikte:
Shift-reduce kon ikt:
8[A! � �X�] 2 state ^X 2 T
) :(9[B ! �] 2 state ^X 2 Follow(B))
Reduce-reduce kon ikt:
8[A! ��] 2 state ^ [B ! ��] 2 state
) Follow(A) \ Follow(B) = ;
19 / 19