{"id":275,"date":"2026-11-13T12:36:39","date_gmt":"2026-11-13T04:36:39","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-the-cyk-algorithm-parses-a-chomsky-grammar\/"},"modified":"2026-11-13T21:54:13","modified_gmt":"2026-11-13T13:54:13","slug":"how-the-cyk-algorithm-parses-a-chomsky-grammar","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-the-cyk-algorithm-parses-a-chomsky-grammar\/","title":{"rendered":"How the CYK algorithm parses a Chomsky grammar"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> The <strong>CYK algorithm<\/strong> decides whether a string belongs to a context-free grammar in <strong>Chomsky normal form<\/strong>, and it builds a parse table while doing so. Cell (length, start, variable) is true when that variable derives the substring. A production A \u2192 B C splits the substring into a prefix derived from B and a suffix derived from C. A production A \u2192 a fills the length-1 cells. The table is <strong>O(n\u00b3 \u00b7 |G|)<\/strong> time for a string of length n.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">A general context-free grammar can be parsed by Earley&#8217;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.<\/p>\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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\u00b3 \u00b7 |G|) for the usual representation that tries every production at every substring and every split. Space is O(n\u00b2 \u00b7 |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.<\/p>\n\n\n<p class=\"wp-block-paragraph\">CYK does not handle \u03b5-productions or unit productions unless they were eliminated in the conversion. Running the split loop on a grammar that still has A \u2192 B, a unit production, misses derivations or requires a closure the algorithm as stated does not do. Earley&#8217;s algorithm accepts a broader grammar directly.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>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.<\/li>\n<li>For each position i, set the length-1 cell to the variables that have a production of the terminal at i.<\/li>\n<li>For length from 2 to n, for every start position, for every split, and for every production A \u2192 B C, set A true on the substring when B is true on the left piece and C is true on the right piece.<\/li>\n<li>Accept if the start symbol is true on the full string.<\/li>\n<li>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.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">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&#8217;s algorithm parses general context-free grammars in O(n\u00b3) as well, and in less time on some unambiguous grammars. CYK is the substring DP that the normal form makes valid.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when a unit production is left in the grammar<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Run the binary split on a grammar that still contains A \u2192 B<\/td>\n<td>The unit production derives no extra terminal and has no split index. The cell for A never becomes true through that production.<\/td>\n<\/tr>\n<tr>\n<td>Fill long substrings before short ones<\/td>\n<td>A split reads smaller lengths. The length loop has to increase.<\/td>\n<\/tr>\n<tr>\n<td>Accept when any variable derives the string<\/td>\n<td>Membership is about the start symbol. Another variable deriving the text is irrelevant.<\/td>\n<\/tr>\n<tr>\n<td>Quote O(n\u00b3) and skip the grammar factor<\/td>\n<td>Each substring and split may try every production. The grammar size belongs in the bound.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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 <code>aa<\/code> is in the language of S \u2192 AB, A \u2192 a, B \u2192 a? With S \u2192 AA and A \u2192 a, <code>aa<\/code> is accepted. It prints <code>yes<\/code>.<\/p>\n\n<pre><code class=\"language-python\"># rules: binary (A, B, C) means A -&gt; B C, terminal (A, ch)\nbinary = [(&quot;S&quot;, &quot;A&quot;, &quot;A&quot;)]\nterminal = [(&quot;A&quot;, &quot;a&quot;)]\ntext = &quot;aa&quot;\nn = len(text)\nnts = sorted({a for a, _, _ in binary} | {a for a, _ in terminal})\ndp = [[set() for _ in range(n)] for _ in range(n)]\nfor i, ch in enumerate(text):\n    for A, t in terminal:\n        if t == ch:\n            dp[i][i].add(A)\nfor length in range(2, n + 1):\n    for i in range(n - length + 1):\n        j = i + length - 1\n        for k in range(i, j):\n            for A, B, C in binary:\n                if B in dp[i][k] and C in dp[k + 1][j]:\n                    dp[i][j].add(A)\nassert &quot;S&quot; in dp[0][n - 1]\nprint(&quot;yes&quot;)\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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&#8217;s algorithm accepts a general grammar and skips that conversion.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for the CYK algorithm returns the binary split over substrings and the Chomsky normal form it requires. <strong>CYK recognizes a Chomsky-normal-form grammar in O(n\u00b3 \u00b7 |G|) time by dynamic programming over substrings and binary production splits.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 \u2192 B C splits the substring into&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,33],"tags":[],"class_list":["post-275","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-dynamic-programming"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/275","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=275"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/275\/revisions"}],"predecessor-version":[{"id":674,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/275\/revisions\/674"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=275"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=275"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}