02 parsing mit backtracking - ling.uni-potsdam.de parsing mit backtracking.pdfparsing • gegeben...
TRANSCRIPT
![Page 1: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/1.jpg)
Parsing mitBacktracking
Vorlesung “Computerlinguistische Techniken” Alexander Koller
20. Oktober 2015
![Page 2: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/2.jpg)
Kontextfreie GrammatikenT = {Hans, isst, Käsebrot, ein} N = {S, NP, VP, V, N, Det}; Startsymbol: S
Produktionsregeln:S → NP VPNP → Det NVP → V NP
V → isstNP → Hans
Det → einN → Käsebrot
Ableitung Parsebaum
S ⇒ NP VP ⇒ Hans VP⇒ Hans V NP ⇒ Hans isst NP⇒ Hans isst Det N⇒ Hans isst ein N⇒ Hans isst ein Käsebrot
S
VP
NP
N
Kasebrot
Det
ein
V
isst
NP
Hans
S
VP
PP
NP
N
pyjamas
PRP$
my
P
in
VP
NP
N
elephant
Det
an
IV
shot
NP
I
S
VP
NP
N
PP
NP
N
pyjamas
PRP$
my
P
in
N
elephant
Det
an
IV
shot
NP
I
1
![Page 3: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/3.jpg)
Parsing
• Gegeben seien eine kfG G und ein Wort w.
• Das Wortproblem ist die Frage, ob w ∈ L(G) ist. Wortproblem wird von Erkenner gelöst.
• Das Parsingproblem ist das Problem, alle Parsebäume von w bzgl. G zu bestimmen.Parsingproblem wird von Parser gelöst.
![Page 4: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/4.jpg)
Parsing: Beispiele
S → a S b S → a b
Ist aabb ∈ L(G)?Wenn ja, was sind die Parsebäume?
G
![Page 5: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/5.jpg)
Parsing: Beispiele
S → a S b S → a b
Ist abab ∈ L(G)?Wenn ja, was sind die Parsebäume?
G
![Page 6: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/6.jpg)
Parsing: Beispiele
S → S S S → a
Ist aaa ∈ L(G)? Wenn ja, was sind die Parsebäume?
G
![Page 7: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/7.jpg)
Parsing: Beispiele
Ist aaaa ∈ L(G)?Wenn ja, was sind die Parsebäume?
S → S S S → a
Ist aaa ∈ L(G)? Wenn ja, was sind die Parsebäume?
G
![Page 8: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/8.jpg)
Recursive-Descent-Parsing
• Wie löst man das Wort- und das Parsingproblem so systematisch, dass man es implementieren kann?
• Ein erster Ansatz für Parsing: Recursive Descent. (Wir lösen hier nur das Wortproblem.)
• Wir nehmen an, dass kfGs in Chomsky-Normalform (CNF) sind: Jede Regel ist von der Form A → B C oder A → a.
![Page 9: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/9.jpg)
Recursive-Descent-Parsing
• Rekursiver Algorithmus, der für A und w die Frage “A ⇒* w?” entscheidet.
w
![Page 10: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/10.jpg)
Recursive-Descent-Parsing
• Rekursiver Algorithmus, der für A und w die Frage “A ⇒* w?” entscheidet.
A?
w
![Page 11: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/11.jpg)
Recursive-Descent-Parsing
• Rekursiver Algorithmus, der für A und w die Frage “A ⇒* w?” entscheidet.
A?
A → B C
w
![Page 12: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/12.jpg)
Recursive-Descent-Parsing
• Rekursiver Algorithmus, der für A und w die Frage “A ⇒* w?” entscheidet.
A?
A → B C
w
![Page 13: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/13.jpg)
Recursive-Descent-Parsing
• Rekursiver Algorithmus, der für A und w die Frage “A ⇒* w?” entscheidet.
A?
A → B CB?
w
w1
![Page 14: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/14.jpg)
Recursive-Descent-Parsing
• Rekursiver Algorithmus, der für A und w die Frage “A ⇒* w?” entscheidet.
A?
A → B CB? C?
w
w1 w2
![Page 15: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/15.jpg)
Recursive-Descent-Parsing
• Der Algorithmus sieht so aus: ‣ Wir wollen wissen, ob A ⇒* w ist.
‣ Wenn w ∈ T und es eine Regel A → w gibt, gib “ja” zurück, sonst “nein”.
‣ Rate eine Regel A → B C, die wir anwenden wollen.
‣ Rate eine Zerlegung w = w1 w2.
‣ Wenn sowohl B ⇒* w1 als auch C ⇒* w2 “ja” gesagt haben, gib “ja” zurück, sonst “nein”.
![Page 16: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/16.jpg)
RD-Parsing: Ein BeispielS → A B S → A C
A → a C → B C
B → bC → c
![Page 17: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/17.jpg)
RD-Parsing: Ein Beispiel
S ⇒* a b c ?
S → A B S → A C
A → a C → B C
B → bC → c
![Page 18: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/18.jpg)
RD-Parsing: Ein Beispiel
S ⇒* a b c ?(versuche abc = a· bc, S → A B)
S → A B S → A C
A → a C → B C
B → bC → c
![Page 19: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/19.jpg)
RD-Parsing: Ein Beispiel
S ⇒* a b c ?(versuche abc = a· bc, S → A B)
A ⇒* a ?
S → A B S → A C
A → a C → B C
B → bC → c
![Page 20: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/20.jpg)
RD-Parsing: Ein Beispiel
S ⇒* a b c ?(versuche abc = a· bc, S → A B)
A ⇒* a ? B ⇒* b c ?
S → A B S → A C
A → a C → B C
B → bC → c
![Page 21: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/21.jpg)
RD-Parsing: Ein Beispiel
S ⇒* a b c ?(versuche abc = a· bc, S → A B)
A ⇒* a ? B ⇒* b c ?(ja, da A → a)
S → A B S → A C
A → a C → B C
B → bC → c
![Page 22: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/22.jpg)
RD-Parsing: Ein Beispiel
S ⇒* a b c ?(versuche abc = a· bc, S → A B)
A ⇒* a ? B ⇒* b c ?(ja, da A → a)
✓
S → A B S → A C
A → a C → B C
B → bC → c
![Page 23: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/23.jpg)
RD-Parsing: Ein Beispiel
S ⇒* a b c ?(versuche abc = a· bc, S → A B)
A ⇒* a ? B ⇒* b c ?(ja, da A → a)
✓(nein: es gibt keine
binären Regeln für B)
S → A B S → A C
A → a C → B C
B → bC → c
![Page 24: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/24.jpg)
RD-Parsing: Ein Beispiel
S ⇒* a b c ?(versuche abc = a· bc, S → A B)
A ⇒* a ? B ⇒* b c ?(ja, da A → a)
✓(nein: es gibt keine
binären Regeln für B)
✗
S → A B S → A C
A → a C → B C
B → bC → c
![Page 25: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/25.jpg)
RD-Parsing: Ein Beispiel
S ⇒* a b c ?(versuche abc = a· bc, S → A B)
A ⇒* a ? B ⇒* b c ?(ja, da A → a)
✓(nein: es gibt keine
binären Regeln für B)
✗
✗
S → A B S → A C
A → a C → B C
B → bC → c
![Page 26: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/26.jpg)
RD-Parsing: Ein BeispielS → A B S → A C
A → a C → B C
B → bC → c
![Page 27: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/27.jpg)
RD-Parsing: Ein Beispiel
S ⇒* a b c ?
S → A B S → A C
A → a C → B C
B → bC → c
![Page 28: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/28.jpg)
RD-Parsing: Ein Beispiel
S ⇒* a b c ?
S → A B S → A C
A → a C → B C
B → bC → c
(versuche abc = a· bc, S → A C)
![Page 29: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/29.jpg)
RD-Parsing: Ein Beispiel
S ⇒* a b c ?
A ⇒* a ?(ja, da A → a)
✓
S → A B S → A C
A → a C → B C
B → bC → c
(versuche abc = a· bc, S → A C)
C ⇒* b c ?
![Page 30: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/30.jpg)
RD-Parsing: Ein Beispiel
S ⇒* a b c ?
A ⇒* a ?(ja, da A → a)
✓
S → A B S → A C
A → a C → B C
B → bC → c
(versuche abc = a· bc, S → A C)
C ⇒* b c ?(versuche bc = b· c, C → B C)
![Page 31: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/31.jpg)
RD-Parsing: Ein Beispiel
S ⇒* a b c ?
A ⇒* a ?(ja, da A → a)
✓
S → A B S → A C
A → a C → B C
B → bC → c
(versuche abc = a· bc, S → A C)
C ⇒* b c ?(versuche bc = b· c, C → B C)
B ⇒* b ? C ⇒* c ?
![Page 32: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/32.jpg)
RD-Parsing: Ein Beispiel
S ⇒* a b c ?
A ⇒* a ?(ja, da A → a)
✓
S → A B S → A C
A → a C → B C
B → bC → c
(versuche abc = a· bc, S → A C)
C ⇒* b c ?
(ja, da B → b)
(versuche bc = b· c, C → B C)
B ⇒* b ? C ⇒* c ?
![Page 33: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/33.jpg)
RD-Parsing: Ein Beispiel
S ⇒* a b c ?
A ⇒* a ?(ja, da A → a)
✓
S → A B S → A C
A → a C → B C
B → bC → c
(versuche abc = a· bc, S → A C)
C ⇒* b c ?
(ja, da B → b)✓
(versuche bc = b· c, C → B C)
B ⇒* b ? C ⇒* c ?
![Page 34: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/34.jpg)
RD-Parsing: Ein Beispiel
S ⇒* a b c ?
A ⇒* a ?(ja, da A → a)
✓
S → A B S → A C
A → a C → B C
B → bC → c
(versuche abc = a· bc, S → A C)
C ⇒* b c ?
(ja, da B → b)✓
(versuche bc = b· c, C → B C)
B ⇒* b ?(ja, da C → c)C ⇒* c ?
![Page 35: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/35.jpg)
RD-Parsing: Ein Beispiel
S ⇒* a b c ?
A ⇒* a ?(ja, da A → a)
✓
S → A B S → A C
A → a C → B C
B → bC → c
(versuche abc = a· bc, S → A C)
C ⇒* b c ?
(ja, da B → b)✓
(versuche bc = b· c, C → B C)
B ⇒* b ?(ja, da C → c)
✓C ⇒* c ?
![Page 36: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/36.jpg)
RD-Parsing: Ein Beispiel
S ⇒* a b c ?
A ⇒* a ?(ja, da A → a)
✓
S → A B S → A C
A → a C → B C
B → bC → c
(versuche abc = a· bc, S → A C)
C ⇒* b c ?
(ja, da B → b)✓
(versuche bc = b· c, C → B C)
B ⇒* b ?(ja, da C → c)
✓C ⇒* c ?
✓
![Page 37: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/37.jpg)
RD-Parsing: Ein Beispiel
S ⇒* a b c ?
A ⇒* a ?(ja, da A → a)
✓
S → A B S → A C
A → a C → B C
B → bC → c
(versuche abc = a· bc, S → A C)
C ⇒* b c ?
(ja, da B → b)✓
(versuche bc = b· c, C → B C)
B ⇒* b ?(ja, da C → c)
✓C ⇒* c ?
✓
✓
![Page 38: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/38.jpg)
Zwei Beobachtungen
• Der RD-Algorithmus ist rekursiv:Um A ⇒* w zu testen, überprüfen wir B ⇒* w1 und C ⇒* w2.
• Der RD-Algorithmus ist nichtdeterministisch: Er “rät” Regeln und Stringzerlegungen. ‣ Implementierung auf echtem Computer muss deshalb alle
Möglichkeiten der Reihe nach durchprobieren.
‣ Revidiert dabei Entscheidungen aus früheren rekursiven Aufrufen -> Backtracking.
![Page 39: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/39.jpg)
Pseudocode
function S(w, i, k): for j = i+1, ..., k-1 do if NP(w, i, j) and VP(w, j, k) then return true else return false end if end for
function NP(w, i, k): if k = i + 1 and w(i) = “Hans” then return true else return false end if
![Page 40: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/40.jpg)
Pseudocode
function S(w, i, k): for j = i+1, ..., k-1 do if NP(w, i, j) and VP(w, j, k) then return true else return false end if end for
function NP(w, i, k): if k = i + 1 and w(i) = “Hans” then return true else return false end if
S ⇒* wi … wk-1 ?
![Page 41: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/41.jpg)
Pseudocode
function S(w, i, k): for j = i+1, ..., k-1 do if NP(w, i, j) and VP(w, j, k) then return true else return false end if end for
function NP(w, i, k): if k = i + 1 and w(i) = “Hans” then return true else return false end if
S ⇒* wi … wk-1 ?
NP ⇒* wi … wj-1 ?
![Page 42: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/42.jpg)
Pseudocode
function S(w, i, k): for j = i+1, ..., k-1 do if NP(w, i, j) and VP(w, j, k) then return true else return false end if end for
function NP(w, i, k): if k = i + 1 and w(i) = “Hans” then return true else return false end if
S ⇒* wi … wk-1 ?
NP ⇒* wi … wj-1 ?
VP ⇒* wj … wk-1 ?
![Page 43: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/43.jpg)
Pseudocode
function S(w, i, k): for j = i+1, ..., k-1 do if NP(w, i, j) and VP(w, j, k) then return true else return false end if end for
function NP(w, i, k): if k = i + 1 and w(i) = “Hans” then return true else return false end if
S ⇒* wi … wk-1 ?
NP ⇒* wi … wj-1 ?
VP ⇒* wj … wk-1 ?
NP ⇒* wi … wk-1 ?
![Page 44: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/44.jpg)
Pseudocode
function S(w, i, k): for j = i+1, ..., k-1 do if NP(w, i, j) and VP(w, j, k) then return true else return false end if end for
function NP(w, i, k): if k = i + 1 and w(i) = “Hans” then return true else return false end if
S ⇒* wi … wk-1 ?
NP ⇒* wi … wj-1 ?
VP ⇒* wj … wk-1 ?
NP ⇒* wi … wk-1 ?
ja falls wi … wk-1 = Hans
![Page 45: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/45.jpg)
Korrektheit und Vollständigkeit
• Warum sollen wir glauben, dass RD-Erkenner für alle Eingaben das richtige Ergebnis liefert?
• Wir müssen beweisen: ‣ Korrektheit: RD-Erkenner behauptet w ∈ L(G) nur dann,
wenn es auch wahr ist.
‣ Vollständigkeit: Wenn w ∈ L(G) ist, dann behauptet das der RD-Erkenner auch.
![Page 46: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/46.jpg)
Probleme und Algorithmen
• Problem: Funktion von irgendwelchen Eingaben zu {0,1}. ‣ Wortproblem von kfGs: gegeben Eingabe (G,w),
gib 1 aus gdw w in L(G), sonst 0.
• Algorithmus: abstrakte Berechnungsvorschrift, die das Problem löst. ‣ RD-Erkenner ist Algorithmus für das Wortproblem.
• Algorithmen kann man in Programmiersprachen implementieren.
![Page 47: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/47.jpg)
Probleme und Algorithmen
Wortproblem(G,w) = 1gdw w ∈ L(G)
Problem
Algorithmus
Programm
function S(w, i, k): for j = i+1, ..., k-1 do if NP(w, i, j) and VP(w, j, k) then return true else return false end if end for
![Page 48: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/48.jpg)
Beweistechnik
• Korrektheit und Vollständigkeit von Parsingalgo. beweist man typischerweise mit vollständiger Induktion: ‣ Korrektheit: … über Länge der Berechnung des Algorithmus
‣ Vollständigkeit: … über Länge der Ableitung.
• Vollständige Induktion: ‣ Zeige, dass Aussage für n = 0 (oder n = 1) wahr ist.
‣ Zeige: Wenn Aussage für beliebiges n wahr ist, dann ist sie auch für n+1 wahr.(alternativ: sei n beliebig, und nimm an, dass Aussage für alle k ≤ n wahr ist),
‣ Daraus folgt dann: Aussage für alle n ≥ 0 (bzw. n ≥ 1) wahr.
![Page 49: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/49.jpg)
Korrektheit des RD-Erkenners
• Korrektheit: Wenn RD-Erkenner w ∈ L(G) behauptet, dann ist es auch wahr.
• Zeige stärkere Aussage: Wenn RD-Erkenner A ⇒* w behauptet, ist das wahr. (Daraus folgt Korrektheit.)
• Induktion über die Länge n der Berechnung (= Anzahl der rekursiven Aufrufe): ‣ Fall n = 1: Algo. hat ohne rekursiven Aufruf direkt “ja” gesagt.
Dann war das, weil es Regel A → w gibt, also ist A ⇒* w.
![Page 50: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/50.jpg)
Korrektheit des RD-Erkenners
• Zeige stärkere Aussage: Wenn RD-Erkenner A ⇒* w behauptet, ist das wahr. (Daraus folgt Korrektheit.) ‣ Fall n → n+1: Algo. hat “ja” gesagt und dabei im ersten Schritt
w = w1 w2 zerlegt und Regel A → B C angewendet, und rekursive Aufrufe B ⇒* w1 und C ⇒* w2 waren beide “ja”.
‣ Rekursive Berechnungen waren beide kürzer: kB, kC ≤ n. Wegen Induktionsannahme gilt deshalb: B ⇒* w1 und C ⇒* w2 sind beide wahr.
‣ Deshalb ist auch A ⇒* w wahr, nämlich wie folgt: A ⇒ B C ⇒* w1 C ⇒* w1 w2 = w.
![Page 51: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/51.jpg)
Vollständigkeit des RD-Erkenners
• Vollständigkeit: Wenn w ∈ L(G) wahr ist, dann behauptet es der RD-Erkenner auch.
• Zeige stärkere Aussage: Wenn A ⇒* w wahr ist, dann sagt RD-Erkenner “ja”. (Daraus folgt Vollständigkeit.)
• Induktion über die Länge n der Ableitung A ⇒* w:
‣ Fall n = 1: A ⇒ w, es gibt also Regel A → w. RD-Erkenner sagt ohne rekursiven Aufruf “ja”.
![Page 52: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/52.jpg)
Vollständigkeit des RD-Erkenners
• Vollständigkeit: Wenn w ∈ L(G) wahr ist, dann behauptet es der RD-Erkenner auch. ‣ Fall n → n + 1: Es gilt A ⇒* w. Diese Ableitung beginnt mit
einem ersten Schritt, A ⇒ B C ⇒* w1 C ⇒* w1 w2 = w.
‣ RD-Erkenner überprüft alle Zerlegungen von w, also insbesondere auch A → B C und w = w1 w2.
‣ Die Ableitungen B ⇒* w1 und C ⇒* w2 haben beide weniger als n Schritte. Nach Induktionsannahme sagt RD-Erkenner für beide rekursive Aufrufe deshalb “ja”.
‣ Daher sagt RD-Erkenner auch für A ⇒*w “ja”.
![Page 53: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/53.jpg)
Top-Down vs. Bottom-Up
• Parser kann den Parsebaum top-down oder bottom-up zu berechnen versuchen.
top-down(z.B. Recursive Descent)
bottom-up
S
VP
NP
N
Kasebrot
Det
ein
V
isst
NP
Hans
S
VP
PP
NP
N
pyjamas
PRP$
my
P
in
VP
NP
N
elephant
Det
an
IV
shot
NP
I
S
VP
NP
N
PP
NP
N
pyjamas
PRP$
my
P
in
N
elephant
Det
an
IV
shot
NP
I
1
S
VP
NP
N
Kasebrot
Det
ein
V
isst
NP
Hans
S
VP
PP
NP
N
pyjamas
PRP$
my
P
in
VP
NP
N
elephant
Det
an
IV
shot
NP
I
S
VP
NP
N
PP
NP
N
pyjamas
PRP$
my
P
in
N
elephant
Det
an
IV
shot
NP
I
1
![Page 54: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/54.jpg)
Bottom-Up-ParsingS → NP VPNP → Det NVP → V NP
V → isstNP → Hans
Det → einN → Käsebrot
Hans isst ein Käsebrot.
![Page 55: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/55.jpg)
Bottom-Up-ParsingS → NP VPNP → Det NVP → V NP
V → isstNP → Hans
Det → einN → Käsebrot
Hans isst ein Käsebrot.
NP
![Page 56: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/56.jpg)
Bottom-Up-ParsingS → NP VPNP → Det NVP → V NP
V → isstNP → Hans
Det → einN → Käsebrot
Hans isst ein Käsebrot.
NP V
![Page 57: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/57.jpg)
Bottom-Up-ParsingS → NP VPNP → Det NVP → V NP
V → isstNP → Hans
Det → einN → Käsebrot
Hans isst ein Käsebrot.
NP DetV
![Page 58: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/58.jpg)
Bottom-Up-ParsingS → NP VPNP → Det NVP → V NP
V → isstNP → Hans
Det → einN → Käsebrot
Hans isst ein Käsebrot.
NP DetV N
![Page 59: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/59.jpg)
Bottom-Up-ParsingS → NP VPNP → Det NVP → V NP
V → isstNP → Hans
Det → einN → Käsebrot
Hans isst ein Käsebrot.
NP DetV N
NP
![Page 60: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/60.jpg)
Bottom-Up-ParsingS → NP VPNP → Det NVP → V NP
V → isstNP → Hans
Det → einN → Käsebrot
Hans isst ein Käsebrot.
NP DetV N
NP
VP
![Page 61: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/61.jpg)
Bottom-Up-ParsingS → NP VPNP → Det NVP → V NP
V → isstNP → Hans
Det → einN → Käsebrot
Hans isst ein Käsebrot.
NP DetV N
NP
VP
S
![Page 62: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/62.jpg)
Bottom-up-ParsingS → S S S → a
aa a
![Page 63: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/63.jpg)
Bottom-up-ParsingS → S S S → a
aa a
S
![Page 64: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/64.jpg)
Bottom-up-ParsingS → S S S → a
a
S
a a
S
![Page 65: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/65.jpg)
Bottom-up-ParsingS → S S S → a
a
S S
a a
S
![Page 66: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/66.jpg)
Bottom-up-ParsingS → S S S → a
a
S S
S
a a
S
![Page 67: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/67.jpg)
Bottom-up-ParsingS → S S S → a
a
S S
S
a a
S
S
![Page 68: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/68.jpg)
Bottom-up-ParsingS → S S S → a
a
S S
S
a a
S
S
aa a
![Page 69: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/69.jpg)
Bottom-up-ParsingS → S S S → a
a
S S
S
a a
S
S
a
S
a a
![Page 70: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/70.jpg)
Bottom-up-ParsingS → S S S → a
a
S S
S
a a
S
S
a
S S
a a
![Page 71: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/71.jpg)
Bottom-up-ParsingS → S S S → a
a
S S
S
a a
S
S
a
S S
S
a a
![Page 72: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/72.jpg)
Bottom-up-ParsingS → S S S → a
a
S S
S
a a
S
S
a
S S
S
a a
S
![Page 73: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/73.jpg)
Bottom-up-ParsingS → S S S → a
a
S S
S
a a
S
S
a
S S
S
a a
S
S
![Page 74: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/74.jpg)
Shift-Reduce-Parsing
Hans isst ein
NP V
Hans isst ein
NP V Det
Hans isst ein Käsebrot.
NP DetV N
Hans isst ein Käsebrot.
NP DetV N
NP
Shift: nächstes Terminalsymbol lesen
Reduce: Regel auf letzte offene Symbole anwenden
![Page 75: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/75.jpg)
Shift-Reduce-Parsing
• Wir verwenden als Datenstruktur einen Stack von Terminal- und Nichtterminalsymbolen.
• Ein Stack ist eine Liste, in der ich nur an einem Ende (“oben”) lesen und schreiben kann.
• Unser Stack enthält die unverarbeiteten Terminal- und Nichtterminalsymbole. Schreibweise: in Stack s1 s2 s3 ist s3 “oben”.
NP NPVP
NP Spush pop pop push
![Page 76: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/76.jpg)
Shift-Reduce-Parsing
• Shift-Regel:(s, a⋅w) → (s⋅a, w)
• Reduce-Regel:(s⋅w’, w) → (s⋅A, w) falls A → w’ in P
• Start: (ε, w)
• Wende Regeln nichtdeterministisch an. Algorithmus sagt “ja”, wenn er Konfiguration (S, ε) erreicht (d.h. erreichen kann).
![Page 77: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/77.jpg)
Shift-Reduce: Beispiel
Hans isst ein Käsebrot.
Shift: (s, a⋅w) → (s⋅a, w)Reduce: (s⋅w’, w) → (s⋅A, w)
![Page 78: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/78.jpg)
Shift-Reduce: Beispiel
(ε, Hans isst ein K.)
Hans isst ein Käsebrot.
Shift: (s, a⋅w) → (s⋅a, w)Reduce: (s⋅w’, w) → (s⋅A, w)
![Page 79: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/79.jpg)
Shift-Reduce: Beispiel
(ε, Hans isst ein K.) → (Hans, isst ein K.)
Hans isst ein Käsebrot.
NP
Shift: (s, a⋅w) → (s⋅a, w)Reduce: (s⋅w’, w) → (s⋅A, w)
![Page 80: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/80.jpg)
Shift-Reduce: Beispiel
(ε, Hans isst ein K.) → (Hans, isst ein K.) → (NP, isst ein K.)
Hans isst ein Käsebrot.
NP
Shift: (s, a⋅w) → (s⋅a, w)Reduce: (s⋅w’, w) → (s⋅A, w)
![Page 81: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/81.jpg)
Shift-Reduce: Beispiel
(ε, Hans isst ein K.) → (Hans, isst ein K.) → (NP, isst ein K.)→ (NP isst, ein K.)
Hans isst ein Käsebrot.
NP V
Shift: (s, a⋅w) → (s⋅a, w)Reduce: (s⋅w’, w) → (s⋅A, w)
![Page 82: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/82.jpg)
Shift-Reduce: Beispiel
(ε, Hans isst ein K.) → (Hans, isst ein K.) → (NP, isst ein K.)→ (NP isst, ein K.) → (NP V, ein K.)
Hans isst ein Käsebrot.
NP V
Shift: (s, a⋅w) → (s⋅a, w)Reduce: (s⋅w’, w) → (s⋅A, w)
![Page 83: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/83.jpg)
Shift-Reduce: Beispiel
(ε, Hans isst ein K.) → (Hans, isst ein K.) → (NP, isst ein K.)→ (NP isst, ein K.) → (NP V, ein K.) → (NP V ein, K.)
Hans isst ein Käsebrot.
NP DetV
Shift: (s, a⋅w) → (s⋅a, w)Reduce: (s⋅w’, w) → (s⋅A, w)
![Page 84: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/84.jpg)
Shift-Reduce: Beispiel
(ε, Hans isst ein K.) → (Hans, isst ein K.) → (NP, isst ein K.)→ (NP isst, ein K.) → (NP V, ein K.) → (NP V ein, K.)→ (NP V Det, K.)
Hans isst ein Käsebrot.
NP DetV
Shift: (s, a⋅w) → (s⋅a, w)Reduce: (s⋅w’, w) → (s⋅A, w)
![Page 85: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/85.jpg)
Shift-Reduce: Beispiel
(ε, Hans isst ein K.) → (Hans, isst ein K.) → (NP, isst ein K.)→ (NP isst, ein K.) → (NP V, ein K.) → (NP V ein, K.)→ (NP V Det, K.) → (NP V Det K., ε)
Hans isst ein Käsebrot.
NP DetV N
Shift: (s, a⋅w) → (s⋅a, w)Reduce: (s⋅w’, w) → (s⋅A, w)
![Page 86: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/86.jpg)
Shift-Reduce: Beispiel
(ε, Hans isst ein K.) → (Hans, isst ein K.) → (NP, isst ein K.)→ (NP isst, ein K.) → (NP V, ein K.) → (NP V ein, K.)→ (NP V Det, K.) → (NP V Det K., ε) → (NP V Det N, ε)
Hans isst ein Käsebrot.
NP DetV N
Shift: (s, a⋅w) → (s⋅a, w)Reduce: (s⋅w’, w) → (s⋅A, w)
![Page 87: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/87.jpg)
Shift-Reduce: Beispiel
(ε, Hans isst ein K.) → (Hans, isst ein K.) → (NP, isst ein K.)→ (NP isst, ein K.) → (NP V, ein K.) → (NP V ein, K.)→ (NP V Det, K.) → (NP V Det K., ε) → (NP V Det N, ε)→ (NP V NP, ε)
Hans isst ein Käsebrot.
NP DetV N
NP
Shift: (s, a⋅w) → (s⋅a, w)Reduce: (s⋅w’, w) → (s⋅A, w)
![Page 88: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/88.jpg)
Shift-Reduce: Beispiel
(ε, Hans isst ein K.) → (Hans, isst ein K.) → (NP, isst ein K.)→ (NP isst, ein K.) → (NP V, ein K.) → (NP V ein, K.)→ (NP V Det, K.) → (NP V Det K., ε) → (NP V Det N, ε)→ (NP V NP, ε) → (NP VP, ε)
Hans isst ein Käsebrot.
NP DetV N
NP
VP
Shift: (s, a⋅w) → (s⋅a, w)Reduce: (s⋅w’, w) → (s⋅A, w)
![Page 89: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/89.jpg)
Shift-Reduce: Beispiel
(ε, Hans isst ein K.) → (Hans, isst ein K.) → (NP, isst ein K.)→ (NP isst, ein K.) → (NP V, ein K.) → (NP V ein, K.)→ (NP V Det, K.) → (NP V Det K., ε) → (NP V Det N, ε)→ (NP V NP, ε) → (NP VP, ε) → (S, ε)
Hans isst ein Käsebrot.
NP DetV N
NP
VP
SShift: (s, a⋅w) → (s⋅a, w)Reduce: (s⋅w’, w) → (s⋅A, w)
![Page 90: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/90.jpg)
Shift-Reduce: Beispiel
a
S S
S
a a
S
S
(ε, aaa)→ (a, aa) → (S, aa)→ (Sa, a) → (SSa, ε)→ (SSS, ε) → (SS, ε) → (S, ε)
→ (SS, a)
a
S S
S
a a
S
S
(ε, aaa)→ (a, aa) → (S, aa)→ (Sa, a) → (SS, a) → (S, a)→ (Sa, ε) → (SS, ε) → (S, ε)
S → S S S → a
![Page 91: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/91.jpg)
Shift-Reduce: Korrektheit
• Zeige: ‣ wenn Shift-Reduce-Berechnung (s,w) →* (s’,w’) existiert,
‣ dann gibt es kfG-Ableitung s’w’ ⇒* sw.
• Daraus folgt dann Korrektheit: ‣ SR-Erkenner behauptet w ∈ L(G)
‣ gdw (ε, w) →* (S, ε)
‣ daher S ⇒* w (s.o.)
‣ d.h. w ∈ L(G) ist wahr.
![Page 92: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/92.jpg)
(s,w) →* (s’,w’) ⇒ s’w’ ⇒* sw
• n = 0: sw ⇒* sw trivial
• n → n+1: Betrachte ersten Schritt von (s,w) →* (s’,w’) ‣ erster Schritt shift: (s, aw) → (sa, w) →* (s’,w’)
es gilt: s’w’ ⇒* sa⋅w ⇒* s⋅aw
‣ erster Schritt reduce: (sw’, w) → (sA, w) →* (s’,w’)es gilt: s’w’ ⇒* sAw ⇒ sw’w
Induktion über Länge n der Berechnung (d.h. Anzahl der Shift- und Reduce-Schritte)
IA
IA
![Page 93: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/93.jpg)
Shift-Reduce: Vollständigkeit
• Zeige: ‣ wenn kfG-Ableitung A ⇒* w existiert mit w ∈ T*,
‣ dann gibt es SR-Berechnung (ε, w) →* (A, ε)
• Daraus folgt dann Vollständigkeit: ‣ w ∈ L(G) gilt
‣ gdw kfG-Ableitung S ⇒* w existiert mit w ∈ T*
‣ dann gibt es SR-Berechnung (ε, w) →* (S, ε) (s.o.)
‣ also behauptet SR-Erkenner, dass w ∈ L(G) gilt.
![Page 94: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/94.jpg)
A ⇒* w ⇒ (ε, w) →* (A, ε)
• n = 1: d.h. A → w ∈ P, also (ε, w) → (w, ε) → (A, ε)
• n → n+1: d.h. A ⇒ B C ⇒* B w2 ⇒* w1 w2 = w
‣ IA1: (ε, w1) →* (B, ε); daher auch (ε, w1 v) →* (B, v)
‣ IA2: (ε, w2) →* (C, ε); daher auch (u, w2) →* (u C, ε)
‣ also: (ε, w1 w2) →* (B, w2) →* (B C, ε) → (A, ε)
Induktion über Länge n der kfG-Ableitung(für CNF-Grammatiken; allgemeiner Fall nur mehr Schreibarbeit)
s
rIV1
r
IV2
![Page 95: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/95.jpg)
Probleme: Übersicht
• Parsertypen haben komplementäre Probleme:
• Allgemein nicht zu vermeiden (Ambiguität).
top-down bottom-up
Regeln raten A → w1 vs. A → w2
A → w vs. B → w
Zerlegung raten String aufteilen Shift vs. Reduce entscheiden
![Page 96: 02 Parsing mit Backtracking - ling.uni-potsdam.de Parsing mit Backtracking.pdfParsing • Gegeben seien eine kfG G und ein Wort w. • Das Wortproblem ist die Frage, ob w ∈ L(G)](https://reader030.vdocument.in/reader030/viewer/2022040705/5e03ecddee00c323e54d023b/html5/thumbnails/96.jpg)
Zusammenfassung
• Bottom-up vs. Top-down: ‣ top-down: Recursive Descent
‣ bottom-up: Shift-Reduce
• Korrektheit und Vollständigkeit
• Beide Parsertypen haben komplementäre Probleme beim Parsing verschiedener Grammatiken.