Vorlesung
Grundlagen der Theoretischen Informatik / Einführung in die Theoretische Informatik I
Bernhard Beckert
Institut für Informatik
Sommersemester 2007
B. Beckert – Grundlagen d. Theoretischen Informatik: SS 2007 1 / 328
Diese Vorlesungsmaterialien basieren ganz wesentlich auf den Folien zu den Vorlesungen von
Katrin Erk (gehalten an der Universität Koblenz-Landau) Jürgen Dix (gehalten an der TU Clausthal)
Ihnen beiden gilt mein herzlicher Dank.
– Bernhard Beckert, April 2007
B. Beckert – Grundlagen d. Theoretischen Informatik: SS 2007 2 / 328
Chart-Parsing
Beispiel 7.1
Die Grammatik G = ({ S },{ a , b }, R , S ) mit
R = { S → aSa | bSb | aa | bb }
erzeugt die Sprache { vv R | v ∈ { a , b } + } Betrachten wir das Wort w = abbaabba.
Was sind mögliche letzte Schritte von Ableitungen, die zu w geführt haben können?
Wir merken uns alle möglichen einzelnen Ableitungsschritte in einer Chart, um Mehrfacharbeit zu vermeiden.
Wenn das Wort w in der Sprache L ( G ) ist,
enthält am Ende der Chart eine mit S markierte Kante, die vom ersten bis zum letzten Knoten reicht.
B. Beckert – Grundlagen d. Theoretischen Informatik: SS 2007 111 / 328
Chart-Parsing
Beispiel (Forts.)
a b b a
S S
a b b a
S
B. Beckert – Grundlagen d. Theoretischen Informatik: SS 2007 112 / 328
Chart-Parsing
Beispiel (Forts.)
a b b a
S S
a b b a
S S
B. Beckert – Grundlagen d. Theoretischen Informatik: SS 2007 113 / 328
Chart-Parsing
Beispiel (Forts.)
a b b a
S S
a b b a
S S
S
B. Beckert – Grundlagen d. Theoretischen Informatik: SS 2007 114 / 328
Chart-Parsing
Beispiel (Forts.)
a b b a
S S
a b b a
S S
S
S
B. Beckert – Grundlagen d. Theoretischen Informatik: SS 2007 115 / 328
Chart-Parsing
Beispiel (Forts.)
a b b a
S S
a b b a
S S
S
S
S
B. Beckert – Grundlagen d. Theoretischen Informatik: SS 2007 116 / 328
a b b a
S S
a b b a
S S
S
S
S S
B. Beckert – Grundlagen d. Theoretischen Informatik: SS 2007 117 / 328
Zur Vereinfachung Wir fordern:
Grammatik ist in Chomsky-Normalform.
Dann:
Immer nur zwei benachbarte Kanten betrachten, um herauszufinden, ob darüber eine neue Kante eingefügt werden kann.
B. Beckert – Grundlagen d. Theoretischen Informatik: SS 2007 118 / 328
Chart-Parsing
Beispiel (Forts.)
Grammatik in CNF, die dieselbe Sprache wie oben erzeugt:
G = ({ S , S a , S b , A , B },{ a , b }, R , S } mit
R = { S → AS a | BS b | AA | BB S a → SA
S b → SB A → a B → b }
B. Beckert – Grundlagen d. Theoretischen Informatik: SS 2007 119 / 328
Chart-Parsing
Darstellung als Array
Für eine Kante, die den i . bis j. Buchstaben überspannt und mit A markiert ist, steht im [ i , j ] -Element des Arrays die Eintragung A.
Definition 7.2 (M ∗ N)
Sei L = L ( G ) kontextfrei, und G = ( V , T , R , S ) in Chomsky-Normalform. Mit M , N ⊆ V sei
M ∗ N := { A ∈ V | ∃ B ∈ M ,∃ C ∈ N : A → BC ∈ R }
B. Beckert – Grundlagen d. Theoretischen Informatik: SS 2007 120 / 328
Chart-Parsing
Definition 7.3 (w i , j , V i , j ) Sei w = a 1 . . . a n mit a i ∈ Σ . Dann:
w i , j := a i . . . a j ist das Infix von w vom i-ten bis zum j-ten Buchstaben V i , j := { A ∈ V | A = ⇒ ∗
Gw i , j }
B. Beckert – Grundlagen d. Theoretischen Informatik: SS 2007 121 / 328
Chart-Parsing
Lemma 7.4
Sei w = a 1 . . . a n , a i ∈ Σ , d.h. | w | = n. Dann gilt:
1
V i , i = { A ∈ V | A → a i ∈ R }
2
V i , k =
k − 1
S
j = i
V i , j ∗ V j + 1 , k für 1 ≤ i < k ≤ n
Beachte:
Die Grammatik muss in Chomsky-Normalform sein!
B. Beckert – Grundlagen d. Theoretischen Informatik: SS 2007 122 / 328
Chart-Parsing
Beweis
1
V i , i = { A ∈ V | A = ⇒ ∗
Ga i } = { A ∈ V | A → a i ∈ R } , da G in CNF ist.
2
A ∈ V i , k mit 1 ≤ i < k ≤ n gdw A = ⇒ ∗
Ga i . . . a k
gdw ∃ j , i ≤ j < k : ∃ B , C ∈ V : A = ⇒ BC, und B = ⇒ ∗
Gw i , j 6= ε
und C = ⇒ ∗
Gw j + 1 , k 6= ε (da G in CNF ist) gdw ∃ j , i ≤ j < k : ∃ B , C ∈ V : A = ⇒ BC
und B ∈ V i , j und C ∈ V j + 1 , k gdw ∃ j , i ≤ j < k : A ∈ V i , j ∗ V j + 1 , k
B. Beckert – Grundlagen d. Theoretischen Informatik: SS 2007 123 / 328
Teil IV
1 Ableitungsbäume
2 Umformung von Grammatiken
3 Normalformen
4 Pumping-Lemma für kontextfreie Sprachen
5 Pushdown-Automaten (PDAs)
6 Abschlusseigenschaften
7 Wortprobleme
8 Der CYK-Algorithmus
B. Beckert – Grundlagen d. Theoretischen Informatik: Der CYK-Algorithmus SS 2007 124 / 328
Algorithmus
Input sei eine Grammatik G in CNF und ein Wort w = a 1 . . . a n ∈ Σ ∗ . (i) for i := 1 to n do / ∗ Regeln A → a eintragen ∗ /
V i , i := { A ∈ V | A → a i ∈ R } (ii) for h := 1 to n − 1 do
for i := 1 to n − h do V i , i + h =
i + h − 1
S
j = i
V i , j ∗ V j + 1 , i + h
(iii) if S ∈ V 1 , n then return Ausgabe w ∈ L ( G ) else return Ausgabe w 6∈ L ( G )
B. Beckert – Grundlagen d. Theoretischen Informatik: Der CYK-Algorithmus SS 2007 125 / 328
Eigenschaften
Für Wörter der Länge | w | = n entscheidet der CYK-Algorithmus in der Größenordnung von n 3 Schritten, ob w ∈ L ( G ) ist.
B. Beckert – Grundlagen d. Theoretischen Informatik: Der CYK-Algorithmus SS 2007 126 / 328
CYK-Algorithmus (Cocke-Younger-Kasami)
Beispiel 8.1 (CYK)
Eine Grammatik in CNF, die dieselbe Sprache wie oben erzeugt:
G = ({ S , S a , S b , A , B },{ a , b }, R , S }
R = { S → AS a | BS b | AA | BB S a → SA
S b → SB A → a B → b }
Die Sprache ist: L ( G ) = { vv R | v ∈ { a , b } + } Auführlich an der Tafel.
B. Beckert – Grundlagen d. Theoretischen Informatik: Der CYK-Algorithmus SS 2007 127 / 328