The CYK Parsing Algorithm Lecture 19 Section 6.3 Robb T. Koether Hampden-Sydney College Fri, Oct 7, 2016 Robb T. Koether (Hampden-Sydney College) The CYK Parsing Algorithm Fri, Oct 7, 2016 1 / 21
1 The Membership Problem for CFGs 2 The CYK Parsing Algorithm 3 Assignment Robb T. Koether (Hampden-Sydney College) The CYK Parsing Algorithm Fri, Oct 7, 2016 2 / 21
Outline 1 The Membership Problem for CFGs 2 The CYK Parsing Algorithm 3 Assignment Robb T. Koether (Hampden-Sydney College) The CYK Parsing Algorithm Fri, Oct 7, 2016 3 / 21
The Membership Problem for CFGs Definition (The Membership Problem for CFGs) Given a context-free language L and a word w, is w in L(G)? We may assume that G is in Chomsky Normal Form. Let n be the length of the word w. Robb T. Koether (Hampden-Sydney College) The CYK Parsing Algorithm Fri, Oct 7, 2016 4 / 21
The Membership Problem for CFGs An unsophisticated, brute-force algorithm will examine every possible derivation of length 2n 1 (actually n 1). If none of these produces w, then w / L(G). There problem is, the number of such derivations may grow exponentially in n. (Why?) We need a better algorithm. Robb T. Koether (Hampden-Sydney College) The CYK Parsing Algorithm Fri, Oct 7, 2016 5 / 21
Parsing Definition (To parse) Given a grammar G and a string W, to parse w is to find a derivation of w in G. There are many different parsing algorithms for context-free grammars. As usual, the more powerful parsing algorithms are more complex. We will examine a relatively simple algorithm developed by Cocke, Younger, and Kasami, known as the CYK algorithm, which runs in polynomial time. Robb T. Koether (Hampden-Sydney College) The CYK Parsing Algorithm Fri, Oct 7, 2016 6 / 21
Outline 1 The Membership Problem for CFGs 2 The CYK Parsing Algorithm 3 Assignment Robb T. Koether (Hampden-Sydney College) The CYK Parsing Algorithm Fri, Oct 7, 2016 7 / 21
The CYK Parsing Algorithm The strategy of the CYK algorithm is to begin with all one-symbol substrings of w that are derivable from G. These come directly from the rules of the form A a. Then proceed inductively. Robb T. Koether (Hampden-Sydney College) The CYK Parsing Algorithm Fri, Oct 7, 2016 8 / 21
The CYK Parsing Algorithm Let w = a 1 a 2 a 3 a n. Let w ij = a i a j, for all 1 i j n. Define sets For example, V 11 = {A V A a 1 }. V 35 = {A V A a 3 a 4 a 5 }. V 1n = {A V A w}. V ij = {A V A w ij }. Robb T. Koether (Hampden-Sydney College) The CYK Parsing Algorithm Fri, Oct 7, 2016 9 / 21
The CYK Parsing Algorithm The asic Step Initialize each V ii to the set of all variables A for which there is a rule A a i. Robb T. Koether (Hampden-Sydney College) The CYK Parsing Algorithm Fri, Oct 7, 2016 10 / 21
The CYK Parsing Algorithm The Inductive Step For i < j, compute V ij = {A V A C and V ik and C V k+1,j }. i k<j That is, a i a k and C a k+1 a j, so A C a i a j. Robb T. Koether (Hampden-Sydney College) The CYK Parsing Algorithm Fri, Oct 7, 2016 11 / 21
Example (The CYK Parsing Algorithm) Let the grammer G be S SAS baa A as Sb ab λ Let w = ababaa. Is w L(G)? Robb T. Koether (Hampden-Sydney College) The CYK Parsing Algorithm Fri, Oct 7, 2016 12 / 21
Example (The CYK Parsing Algorithm) The grammar converted to CNF is S SD CE SS C A S SC C a C b D AS E A Robb T. Koether (Hampden-Sydney College) The CYK Parsing Algorithm Fri, Oct 7, 2016 13 / 21
The CYK Parsing Algorithm Example (The CYK Parsing Algorithm) The CYK algorithm is much easier to perform if we use an n n table. The entry in row i, column j is the set V ij. We begin by initializing the diagonal elements (i, i) and then proceed inductively to the right. Robb T. Koether (Hampden-Sydney College) The CYK Parsing Algorithm Fri, Oct 7, 2016 14 / 21
Example (The CYK Parsing Algorithm) C C Robb T. Koether (Hampden-Sydney College) The CYK Parsing Algorithm Fri, Oct 7, 2016 15 / 21
Example (The CYK Parsing Algorithm) A C S A C S Robb T. Koether (Hampden-Sydney College) The CYK Parsing Algorithm Fri, Oct 7, 2016 15 / 21
Example (The CYK Parsing Algorithm) A A, E C S A A A, E C S Robb T. Koether (Hampden-Sydney College) The CYK Parsing Algorithm Fri, Oct 7, 2016 15 / 21
Example (The CYK Parsing Algorithm) A A, E C S A S, E A A, E E C S Robb T. Koether (Hampden-Sydney College) The CYK Parsing Algorithm Fri, Oct 7, 2016 15 / 21
Example (The CYK Parsing Algorithm) A A, E A, D C S A S, E S A A, E E C S Robb T. Koether (Hampden-Sydney College) The CYK Parsing Algorithm Fri, Oct 7, 2016 15 / 21
Example (The CYK Parsing Algorithm) A A, E A, D A, E C S A S, E S A A, E E C S Robb T. Koether (Hampden-Sydney College) The CYK Parsing Algorithm Fri, Oct 7, 2016 15 / 21
Example (The CYK Parsing Algorithm) ecause S / V 1,6, ababaa / L(G) Robb T. Koether (Hampden-Sydney College) The CYK Parsing Algorithm Fri, Oct 7, 2016 16 / 21
Example (The CYK Parsing Algorithm) ecause S / V 1,6, ababaa / L(G) Now perform the algorithm for w = babaaba Robb T. Koether (Hampden-Sydney College) The CYK Parsing Algorithm Fri, Oct 7, 2016 16 / 21
Example (The CYK Algorithm) C C C Robb T. Koether (Hampden-Sydney College) The CYK Parsing Algorithm Fri, Oct 7, 2016 17 / 21
Example (The CYK Algorithm) C S A C S A C S Robb T. Koether (Hampden-Sydney College) The CYK Parsing Algorithm Fri, Oct 7, 2016 17 / 21
Example (The CYK Algorithm) C S A A A, E C S A A, E C S Robb T. Koether (Hampden-Sydney College) The CYK Parsing Algorithm Fri, Oct 7, 2016 17 / 21
Example (The CYK Algorithm) C S A S, E A A, E E C S A A, E C S Robb T. Koether (Hampden-Sydney College) The CYK Parsing Algorithm Fri, Oct 7, 2016 17 / 21
Example (The CYK Algorithm) C S A S, E S A A, E E C S A A, E C S Robb T. Koether (Hampden-Sydney College) The CYK Parsing Algorithm Fri, Oct 7, 2016 17 / 21
Example (The CYK Algorithm) C S A S, E S A A A, E E C S A A, E C S Robb T. Koether (Hampden-Sydney College) The CYK Parsing Algorithm Fri, Oct 7, 2016 17 / 21
Example (The CYK Algorithm) C S A S, E S A S, E A A, E E C S A A, E C S Robb T. Koether (Hampden-Sydney College) The CYK Parsing Algorithm Fri, Oct 7, 2016 17 / 21
Example (The CYK Parsing Algorithm) ecause S V 1,7, babaaba L(G) Robb T. Koether (Hampden-Sydney College) The CYK Parsing Algorithm Fri, Oct 7, 2016 18 / 21
Example (The CYK Parsing Algorithm) Let the grammer G be E E + E E E (E) a b Use the CYK algorithm to show that a(+b c) / L(G). Robb T. Koether (Hampden-Sydney College) The CYK Parsing Algorithm Fri, Oct 7, 2016 19 / 21
Outline 1 The Membership Problem for CFGs 2 The CYK Parsing Algorithm 3 Assignment Robb T. Koether (Hampden-Sydney College) The CYK Parsing Algorithm Fri, Oct 7, 2016 20 / 21
Collected To be collected on Wed, Oct 12: Section 4.2 Exercises 14, 18. Section 4.3 Exercise 2, 5c. Section 5.1 Exercise 12b, 23. Section 6.1 Exercise 10. Robb T. Koether (Hampden-Sydney College) The CYK Parsing Algorithm Fri, Oct 7, 2016 21 / 21
Outline 1 The Membership Problem for CFGs 2 The CYK Parsing Algorithm 3 Assignment Robb T. Koether (Hampden-Sydney College) The CYK Parsing Algorithm Fri, Oct 7, 2016 22 / 21
Assignment Assignment Section 6.3 Exercises 1, 4. Robb T. Koether (Hampden-Sydney College) The CYK Parsing Algorithm Fri, Oct 7, 2016 23 / 21