CYK Algorithm
Context: FIT2014_MOC Β· the decision procedure for CFL membership β a bottom-up parser that runs in polynomial time on any CFG, ambiguous or not Parent Framework: Chomsky Normal Form
Quick Revision
- π― Objective: given a CFG and a string , decide whether the grammar generates β build up, by substring length, the set of nonterminals deriving each substring; Accept iff derives the whole string.
- π¦ Core Components: CNF grammar β every split is binary | length-ordered table β answers for length reuse all shorter answers.
- β‘ Key Constraint: the grammar must be in Chomsky Normal Form first, and must be sent to the nullability algorithm instead β CNF cannot generate .
π How It Works
1. Setup
- Input β with each a letter, .
- Empty string β if , run nullability (see there) and stop.
- Otherwise β convert to Chomsky Normal Form for the non-empty words, then fill the table.
2. Base row β single letters
- For each , collect every nonterminal with a dead production .
3. Inductive rows β every longer substring
- Pairs β for each deriving and deriving , add every with a rule .
- Triples β consider both binary splits, and : for each left part and right part, add every with .
- Length β there are split points; take the union over all of them.
- Rules That Always Hold: β CNF guarantees exactly two children, so only binary splits ever need checking; a substring of length only ever consults substrings of length .
4. Decide
- Accept iff the start symbol appears in the cell for the whole string ; otherwise Reject.
π Exam Execution Trace & Applied Exercises
Manual Execution Trace
Grammar , in CNF: , , Β· Input ().
Cell = nonterminals deriving the substring of length starting at position .
| 1 | : | : | : | : | : |
| 2 | : | : | : | : | |
| 3 | : | : | : | ||
| 4 | : | : | |||
| 5 | : |
Justifications for the non-empty cells:
| Cell | Split used | Pair | Rule fired |
|---|---|---|---|
Final Extracted Output: βΉ the grammar generates β ACCEPT. (Sanity check: and , exactly applied twice.)
Applied Exercise
Problem: the lecture sets three exercises β write the algorithm formally, prove it correct by induction, and give its complexity. The complexity argument:
Final Extracted Output: in the string length for a fixed grammar β polynomial, which is the whole point of the algorithm. (Correctness is by induction on substring length: the base row is exact by the dead productions, and the inductive step is exact because CNF forces a binary root split, so every derivation of a length- substring is caught by one of the splits.)
β οΈ Common Mistakes
- π‘ Forgetting a split point β length needs all splits, not just . A missed split turns an Accept into a wrong Reject.
- π‘ Running CYK on a non-CNF grammar β a rule like or breaks the βone binary splitβ invariant the whole table rests on.
- π‘ Order matters in β must derive the left part and the right. never fires on an pair.
- π‘ Handing to CYK β CNF has no -rule, so the table is meaningless; use nullability.
- π‘ An empty cell is not an error β most cells are empty; only the top cellβs contents decide the answer.
π§ Active Recall
Why does CYK work on ambiguous grammars when a shift-reduce parser chokes on them?
Answer
- Short answer: CYK stores a set of nonterminals per substring and unions over all splits β it never has to choose. A shift-reduce parser is a deterministic PDA that must commit to one move, so ambiguity surfaces as a shift-reduce or reduce-reduce conflict.
- Why: Exhaustive vs deterministic β CYK explores every parse simultaneously in , paying polynomial time for completeness; the LR parser pays only linear time but is confined to .
Why is the table filled in order of increasing substring length rather than left-to-right?
Answer
- Short answer: a cell of length is computed from two strictly shorter substrings, so all shorter lengths must already be complete. Length is the induction variable; position is not.
- Why: Bottom-up dynamic programming β CNF forces the parse treeβs root to split its yield into two non-empty halves. Ordering by length guarantees both halves are resolved before the parent is asked, which is exactly the induction that proves the algorithm correct.