How the CYK algorithm parses a Chomsky grammar

Short answer: The CYK algorithm decides whether a string belongs to a context-free grammar in Chomsky normal form, and it builds a parse table while doing so. Cell (length, start, variable) is true when that variable derives the substring. A production A → B C splits the substring into a prefix derived from B and a suffix derived from C. A production A → a fills the length-1 cells. The table is O(n³ · |G|) time for a string of length n.


What the problem actually asks

A general context-free grammar can be parsed by Earley’s algorithm or by converting to Chomsky normal form and running CYK. Chomsky normal form means every production is a terminal or a pair of variables. That restriction makes every derivation of a string longer than one character a binary split, so the substring DP has a single split index.

The grammar conversion introduces new variables and can change the shape of the parse tree. CYK on the converted grammar accepts the same strings. A parser that must return a tree in the original grammar has to translate the CYK backpointers back through the conversion, or use a different parser.


What the algorithm guarantees

The string is in the language exactly when the start symbol is true in the cell that covers the whole string. Time is O(n³ · |G|) for the usual representation that tries every production at every substring and every split. Space is O(n² · |variables|) for the boolean table, plus backpointers if one parse tree is required. Ambiguous grammars can set a cell true by several splits. The algorithm can keep all of them or one.

CYK does not handle ε-productions or unit productions unless they were eliminated in the conversion. Running the split loop on a grammar that still has A → B, a unit production, misses derivations or requires a closure the algorithm as stated does not do. Earley’s algorithm accepts a broader grammar directly.


The working steps

  1. Convert the grammar to Chomsky normal form, or start from a grammar already in that form. Record the conversion if the original tree shape matters.
  2. For each position i, set the length-1 cell to the variables that have a production of the terminal at i.
  3. For length from 2 to n, for every start position, for every split, and for every production A → B C, set A true on the substring when B is true on the left piece and C is true on the right piece.
  4. Accept if the start symbol is true on the full string.
  5. If a tree is required, store with each true cell the production and the split that justified it, and walk those pointers from the start symbol.

The cubic bound is for Chomsky normal form. It is not a license to run the same triple loop on an arbitrary grammar and expect the same correctness. Earley’s algorithm parses general context-free grammars in O(n³) as well, and in less time on some unambiguous grammars. CYK is the substring DP that the normal form makes valid.


Approaches that look simpler and fail

Approach Why it fails when a unit production is left in the grammar
Run the binary split on a grammar that still contains A → B The unit production derives no extra terminal and has no split index. The cell for A never becomes true through that production.
Fill long substrings before short ones A split reads smaller lengths. The length loop has to increase.
Accept when any variable derives the string Membership is about the start symbol. Another variable deriving the text is irrelevant.
Quote O(n³) and skip the grammar factor Each substring and split may try every production. The grammar size belongs in the bound.

A general context-free parser that does not require a prior conversion is Earley. A regular grammar does not need CYK. A finite automaton or a regular-expression matcher is enough. CYK is the Chomsky-normal-form DP.


A working example

This Python fragment parses a grammar already in Chomsky normal form. A substring is a nonterminal when some binary split produces the two children of a rule. The string aa is in the language of S → AB, A → a, B → a? With S → AA and A → a, aa is accepted. It prints yes.

# rules: binary (A, B, C) means A -> B C, terminal (A, ch)
binary = [("S", "A", "A")]
terminal = [("A", "a")]
text = "aa"
n = len(text)
nts = sorted({a for a, _, _ in binary} | {a for a, _ in terminal})
dp = [[set() for _ in range(n)] for _ in range(n)]
for i, ch in enumerate(text):
    for A, t in terminal:
        if t == ch:
            dp[i][i].add(A)
for length in range(2, n + 1):
    for i in range(n - length + 1):
        j = i + length - 1
        for k in range(i, j):
            for A, B, C in binary:
                if B in dp[i][k] and C in dp[k + 1][j]:
                    dp[i][j].add(A)
assert "S" in dp[0][n - 1]
print("yes")

Where it is used

CYK parses a grammar already in Chomsky normal form, which is what some natural-language toolkits do after the conversion. The table is cubic in the sentence length. Earley’s algorithm accepts a general grammar and skips that conversion.


When you can drop this algorithm

Drop CYK when the grammar is regular or the language is a simple pattern. Use an automaton. Drop it when you cannot convert to Chomsky normal form and still recover the tree you need. Use Earley or a parser generator. Keep CYK for a grammar already in Chomsky normal form.


Why this stays published

We publish this so a search for the CYK algorithm returns the binary split over substrings and the Chomsky normal form it requires. CYK recognizes a Chomsky-normal-form grammar in O(n³ · |G|) time by dynamic programming over substrings and binary production splits.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *