The Cocke–Younger–Kasami algorithm #
Membership in the language of a grammar in Chomsky normal form, decided by dynamic programming
over subwords Kasami Younger Hopcroft, Motwani and Ullman,
Section 7.4.4. The table holds, for each subword w[i, i + l) of the input, the
list of nonterminals deriving it. Cells of length one are read off the rules A → a. A cell of
length l ≥ 2 is filled by trying every split point k and every rule A → B C with B in the
cell of the first k + 1 letters and C in the cell of the remaining l - k - 1. The input is
accepted when the start symbol is in the cell of the whole word, or, for the empty word, when the
grammar has the rule S → ε.
This is the algorithm in the proof of Sipser, Theorem 7.16 that every context-free
language is in P, for a grammar that is already in Chomsky normal form. Conversion of an arbitrary
context-free grammar to Chomsky normal form is not formalized here; a Word-RAM implementation and
O((|R| + 1)(|w| + 1)³) running-time bound are proved in CslibFrontier.Algorithms.WordRAM.CYK.
Main definitions #
Cslib.Algorithms.Lean.CYK.table: the table, filled level by level (by subword length).Cslib.Algorithms.Lean.CYK.cyk: the membership test.
Main results #
Cslib.Algorithms.Lean.CYK.mem_table_iff: a nonterminal is in a table cell iff it derives the subword of the cell, in the sense of Mathlib'sContextFreeGrammar.DerivesforChomskyNormalFormGrammar.toCFG.Cslib.Algorithms.Lean.CYK.cyk_eq_true_iff: CYK accepts exactly the words of the language.Cslib.Algorithms.Lean.CYK.decidableMemLanguage: membership in the language of a grammar in Chomsky normal form is decidable.Cslib.Algorithms.Lean.CYK.nodup_table_and_length_le: every cell is a list without repetitions of at most|R|nonterminals.
References #
- T. Kasami, An efficient recognition and syntax-analysis algorithm for context-free languages
- D. H. Younger, Recognition and parsing of context-free languages in time n³
- J. E. Hopcroft, R. Motwani and J. D. Ullman, Introduction to Automata Theory, Languages, and Computation
- M. Sipser, Introduction to the Theory of Computation
The left-hand side A of a rule A → a with x = some a.
The left-hand side A of a rule A → B C with B ∈ L and C ∈ R.
Scan the rules once, adding the nonterminal of every match to acc unless it is already there
(List.insert).
A CYK table: tbl i l lists nonterminals for the subword of length l starting at i.
The cell of the letter at position i: the nonterminals A with a rule A → w[i].
The cell of the subword of length l ≥ 2 at i, from the cells of shorter subwords: the
nonterminals A with a rule A → B C, B in the cell of the first k + 1 letters and C in
the cell of the remaining l - k - 1, for some split k < l - 1.
The cell of the subword of length l at i.
Overwrite the cell (i, l) of a table.
Fill the cells of length l at every start position i with i + l ≤ |w|.
The table with the cells of lengths 1, …, L filled, in order of length.
The CYK membership test: accept the empty word when the grammar has the rule S → ε, and a
nonempty word when the start symbol is in the cell of the whole word.
Correctness #
A scan adds no repetitions.
Filling level l sets the cells of length l that fit in the word, and nothing else.
Once level l has been filled (l ≤ L), cells of length l are unchanged by later levels.
Correctness of the CYK table: after filling the levels 1, …, L, the cell of a subword of
length l ≤ L lists exactly the nonterminals deriving it.
Correctness of CYK Hopcroft, Motwani and Ullman, Section 7.4.4: the test accepts exactly the words of the language of the grammar.
Membership in the language of a grammar in Chomsky normal form is decidable, by CYK.
Size of the cells #
Every nonterminal in a cell is the left-hand side of a rule.
A cell has at most one entry per rule.
Every cell of the table is a list without repetitions of at most |R| nonterminals, for a
grammar with |R| rules.