1. Home
  2. Theory of Computation
  3. CYK Algorithm (Parsing Context-Free Grammars)

CYK Algorithm (Parsing Context-Free Grammars)

Decide if a grammar can generate a string by filling a triangular table with dynamic programming, one substring length at a time.

Interactive 3DAdvanced14 min readTOCUpdated

Drag to rotate · Right-drag to pan · Click, then scroll to zoom · Space play · Left arrowRight arrow step

What's happening

The CYK algorithm decides whether a string belongs to a context-free language by filling a triangle of cells with dynamic programming. Enter a string of a’s and b’s and press Run CYK.

Pseudocode

  1. for i ← 1 to n: // substrings of length 1
  2. T[1][i] ← { X : X → w[i] }
  3. for len ← 2 to n:
  4. for i ← 1 to n − len + 1:
  5. for k ← 1 to len − 1: // split point
  6. for each rule X → Y Z:
  7. if Y ∈ T[k][i] and Z ∈ T[len−k][i+k]: add X to T[len][i]
  8. accept if S ∈ T[n][1]
Read this model as text · 18 steps

One run of the model above, written out step by step. Useful with a screen reader, on a slow connection, or for quick revision. The values may differ from the run on your screen.

  1. The CYK algorithm decides whether a string belongs to a context-free language by filling a triangle of cells with dynamic programming. Enter a string of a’s and b’s and press Run CYK.
  2. Grammar (Chomsky normal form): S→AB|BC, A→BA|a, B→CC|b, C→AB|a. Cell (len, i) will hold every variable that can produce the substring of length len starting at position i.
  3. Length 1, position 1: the letter 'b' can be produced by B (rules X → b).
  4. Length 1, position 2: the letter 'a' can be produced by A,C (rules X → a).
  5. Length 1, position 3: the letter 'a' can be produced by A,C (rules X → a).
  6. Length 1, position 4: the letter 'b' can be produced by B (rules X → b).
  7. Length 1, position 5: the letter 'a' can be produced by A,C (rules X → a).
  8. Substring ba. split 1|1: B × A,C → S→BC, A→BA. So the cell holds A,S.
  9. Substring aa. split 1|1: A,C × A,C → B→CC. So the cell holds B.
  10. Substring ab. split 1|1: A,C × B → S→AB, C→AB. So the cell holds C,S.
  11. Substring ba. split 1|1: B × A,C → S→BC, A→BA. So the cell holds A,S.
  12. Substring baa. split 1|2: B × B → no rule; split 2|1: A,S × A,C → no rule. So the cell holds ∅.
  13. Substring aab. split 1|2: A,C × C,S → B→CC; split 2|1: B × B → no rule. So the cell holds B.
  14. Substring aba. split 1|2: A,C × A,S → no rule; split 2|1: C,S × A,C → B→CC. So the cell holds B.
  15. Substring baab. split 1|3: B × B → no rule; split 2|2: A,S × C,S → no rule; split 3|1: ∅ × B → no rule. So the cell holds ∅.
  16. Substring aaba. split 1|3: A,C × B → S→AB, C→AB; split 2|2: B × A,S → A→BA; split 3|1: B × A,C → S→BC, A→BA. So the cell holds A,C,S.
  17. Substring baaba. split 1|4: B × A,C,S → S→BC, A→BA; split 2|3: A,S × B → S→AB, C→AB; split 3|2: ∅ × A,S → no rule; split 4|1: ∅ × A,C → no rule. So the cell holds A,C,S.
  18. The top cell contains S, so the grammar can generate "baaba": accepted. CYK runs in O(n³ · |G|).

Try this in the 3D model

  • Run the default string baaba. Which variables are in the top cell? Is S among them?
  • Run aabb. Which row first has an empty set (∅), and what does that tell you?
  • Pick a length-2 cell and list its split. How many splits does a length-4 cell have?
  • Try a string of 6 letters and count the cells. How does the work grow with the length?

The membership question

Given a context-free grammar G and a string w, is w in the language of G? Compilers must answer this for every program. The CYK algorithm (Cocke–Younger–Kasami) answers it for any grammar in Chomsky normal form (CNF), using the same table-filling idea as matrix chain multiplication and LCS.

Chomsky normal form

Every rule must look like X → YZ (two variables) or X → a (one terminal). Any grammar can be converted to this form.

The grammar used in the visualization:

S → AB | BC
A → BA | a
B → CC | b
C → AB | a

The idea

Let T[len][i] be the set of variables that can produce the substring of length len that starts at position i.

  • Length 1: X is in the cell if there is a rule X → (that letter).
  • Longer substrings: cut the substring into a left part and a right part in every possible way. If Y can produce the left part, Z the right part, and X → YZ is a rule, then X can produce the whole substring.

The string is accepted when S is in the top cell (the whole string).

Worked example: baaba

Length Cells (left to right)
5 A, C, S
4 none, A, C, S
3 none, B, B
2 A, S; B; C, S; A, S
1 B; A, C; A, C; B; A, C

The top cell contains S, so baaba is generated by the grammar. For aabb the top cell is empty, so it is rejected.

Code

def cyk(w, binary, terminal):
    n = len(w)
    T = [[set() for _ in range(n)] for _ in range(n + 1)]
    for i, ch in enumerate(w):
        T[1][i] = {x for x, t in terminal if t == ch}
    for length in range(2, n + 1):
        for i in range(n - length + 1):
            for k in range(1, length):
                for x, y, z in binary:
                    if y in T[k][i] and z in T[length - k][i + k]:
                        T[length][i].add(x)
    return 'S' in T[n][0]

Where is it used?

CYK is the textbook parser for exams, and the idea survives in practical parsers for natural language. Programming languages use faster algorithms such as LL and LR parsers, which work on restricted grammars in O(n) time.

Common mistakes

  • Using a grammar that is not in CNF: rules like A → aB or S → ε break the table logic.
  • Trying only one split point instead of all of them.
  • Forgetting that a cell can hold several variables.
  • Checking for S in the bottom row instead of the top cell.

Complexity at a glance

Case / operationTimeWhy
CYK parsingO(n³ · |G|)n² cells, up to n splits each, and |G| rules checked per split.
Table sizeO(n²)One set of variables per substring.
Extra spaceO(n²) table cells

Quick check

Test yourself — pick an answer to see if you got it.

1. What form must the grammar be in to use CYK?

2. What does cell (len, i) contain?

3. How is a cell of length 4 computed?

4. What is the running time of CYK for a string of length n?

Saved in this browser.
Spotted a mistake or a bug in the 3D model?

Report a mistake

in CYK Algorithm (Parsing Context-Free Grammars). Thank you — every report makes the lesson better for the next reader.

We'll also include a link to the step of the 3D model you're on and your browser type, so we can reproduce it.