4.6.9 · D4Theory of Computation

Exercises — Chomsky Normal Form (CNF) — conversion

4,104 words19 min readBack to topic

Throughout, the vocabulary is the one built in the parent note. A quick refresher of the two symbols you must never confuse:


Level 1 — Recognition

Exercise 1.1

For each rule, say CNF-legal or not, and if not, name the exact violation: (a) (b) (c) (d) (e) (f) (where is not the start symbol) (g) (where is the start symbol).

Recall Solution 1.1

Recall the two allowed shapes: box→two boxes, or box→one tile (plus start→ε).

  • (a) Legal is exactly two variables. ✓
  • (b) Legal — one terminal. ✓
  • (c) Illegalmixed terminal and variable. A single rule may not contain both a tile and a box.
  • (d) Illegalunit rule (a rename); it produces neither a tile nor a branch into two boxes.
  • (e) Illegaltoo long (three variables). CNF caps the right side at two.
  • (f) Illegal — an -rule on a non-start variable is forbidden.
  • (g) Legal — only the start symbol is allowed to vanish, and only this way. ✓

Exercise 1.2

The grammar — is it already in CNF? Justify rule-by-rule.

Recall Solution 1.2
  • : two variables → shape . ✓
  • : one terminal → shape . ✓
  • : one terminal. ✓

Every rule matches a permitted shape, so yes, is already in CNF. No conversion needed.


Level 2 — Application (single steps)

Exercise 2.1 (DEL — nullable set)

Grammar: . Compute the nullable set by fixed-point iteration.

Recall Solution 2.1

A variable is nullable if it can derive the empty string. Build from scratch:

  • Round 1 (direct -rules): so ; so . Now .
  • Round 2 (all-nullable right sides): — both , so . Now .
  • Round 3: nothing new appears → fixed point reached.


Exercise 2.2 (DEL — patching a rule)

Given (so nullable, not), list the rules DEL produces from .

Recall Solution 2.2

The nullable positions are . Add a version for every subset of those positions dropped, but never delete everything down to empty:

  • drop nothing:
  • drop :
  • drop :
  • drop and :

With two nullable positions there are subsets, and none collapses to empty (because stays), so all four survive.


Exercise 2.3 (UNIT — unit pairs)

Grammar: . List all unit pairs with by unit rules only, then give the grammar after UNIT.

Recall Solution 2.3

A unit pair means reaches using only rename rules (reflexive: every variable reaches itself). The unit rules are .

  • Reflexive: .
  • Direct: .
  • Transitive: gives .

Full set: .

Now for each variable, copy the non-unit bodies of everything it reaches. The only non-unit rule is .

  • reaches → gets .
  • reaches → gets .
  • stays.

After UNIT (rename rules dropped):


Exercise 2.4 (TERM)

Apply TERM to .

Recall Solution 2.4

TERM isolates terminals that sit inside a rule alongside variables. Here and are mixed with . Introduce a fresh box per distinct terminal: then replace the terminals in the body: Now the long rule contains only variables (length still 3 — BIN will handle that next).


Exercise 2.5 (BIN)

Apply BIN to (four variables). How many helper variables appear?

Recall Solution 2.5

BIN chops a long right side into a chain of length-2 rules, each holding "the rest": A right side of length needs helpers. Here , so helpers ().


Level 3 — Analysis (multi-step reasoning)

Exercise 3.1

Grammar: . Convert fully to CNF (Sipser order START → DEL → UNIT → TERM → BIN). Note is in the language.

Recall Solution 3.1

START. Add . Rules: ; .

DEL. Nullable: gives . Is nullable? with nullable → yes, . So . Patch rules over nullable positions:

  • : nullable position is the middle . Drop it → add . Body .
  • : similarly → . Body .
  • : delete (S is not the start).
  • : nullable, but dropping it would give keep that one, because is in the language and is the start. So keep and .

After DEL: ; .

UNIT. Unit pairs: . The only rename is . Copy 's non-unit bodies into :

TERM. Terminals appear mixed. Add :

BIN. The length-3 rules and need one helper each: Final CNF:

The language is even-length binary palindromes (including ), unchanged. ✓


Exercise 3.2

For the final grammar of Exercise 3.1, draw the CNF parse tree of the string and count its nodes. Verify the count matches the CNF prediction .

Recall Solution 3.2

has length . The outer letters are , so start with the -rule; the inner block comes from the -rule: Reading the figure. The diagram draws this exact tree, top to bottom. The blue nodes (, , ) are the branching nodes — trace each one and you will see it splits into exactly two children (drawn as two blue edges), which is the visual signature of CNF: a binary tree. The orange nodes () are terminal-helper boxes, each sending a single green edge down to a finished tile ( or ) at the bottom row. Follow the bottom row left-to-right and you read off the string .

Now count using the tree's structure. In a binary parse tree for a length- string: Here : branching nodes (the three blue nodes ), leaf tiles (the four bottom green tiles), total . The figure's node count matches. That fixed, predictable size is exactly why CYK runs in .


Exercise 3.3

Convert to CNF. Watch the ordering carefully.

Recall Solution 3.3

START. . Rules: ; ; ; .

DEL. Nullable: ; . contains terminal (not nullable) so . , not nullable → . So . Patch (nullable positions and ; the tile always stays):

  • keep both:
  • drop :
  • drop :
  • drop both:

. Delete and ; patches to . After DEL: ; ; .

PRUNE (remove non-generating symbols). A variable is generating if it can eventually derive a string of terminals only. After deleting , the variable has no productions at all, so it can generate nothing — it is non-generating. Any rule body that contains (here and ) can therefore never finish deriving into terminals, so those bodies are dead and are removed. Why this preserves the language: a dead body contributes zero terminal strings, so deleting it removes no derivable string — the language is unchanged. Remove and its dead bodies:

UNIT. Pairs: . Copy 's non-unit bodies to : . Drop .

TERM. . Bodies , :

BIN. All right sides now have length ≤ 2 — nothing to chop.

Final CNF: Language . ✓


Level 4 — Synthesis (build to spec)

Exercise 4.1

Design a CNF grammar for directly (not by converting), then confirm it derives .

Recall Solution 4.1

We want each matched by a later . Structure: an outer wrapping a smaller copy. In CNF we need boxes only: Reading it: places one (via ) then = (inner matched string)(one ). Base case = single . Derive : Every rule is or valid CNF, and . ✓


Exercise 4.2

Build a CNF grammar whose language is (all length-2 strings over ). How many right sides does need?

Recall Solution 4.2

Length-2 strings: — four of them. In CNF the top rule must produce two boxes, each a single letter: needs right sides (one per ordered pair of letters). Each expands to a length-2 tile pair — exactly the strings of length 2. ✓ (This is finite, so and no start- rule is needed.)


Exercise 4.3

Convert (balanced parentheses, treating ( and ) as terminals, including the empty string) to CNF.

Recall Solution 4.3

Write terminals as = ( and = ). START. ; . DEL. Nullable: ; . . Patch:

  • : both positions nullable. Subsets: keep both ; drop one (either) → ; drop both → (deleted). So add (a unit! handled next) and keep .
  • : middle nullable → drop it: . Body .
  • Delete ; keep (since ).

After DEL: ; . UNIT. Unit rules: , and (trivial self-loop, remove). Pairs: . Non-unit bodies of : . Copy to : TERM. ((), ()). Rewrite and : BIN. Length-3 rule needs one helper: , . Final CNF:

( ( and ).) Balanced parentheses, including , unchanged. ✓


Level 5 — Mastery (subtle & complete)

Exercise 5.1

Ordering counterexample. Take . Show that doing BIN/TERM first, then DEL can leave a stray rule that violates CNF, whereas Sipser's order does not. Explain precisely.

Recall Solution 5.1

Sipser order (correct). After START: ; ; . DEL: nullable, . → drop : ; keep . But now has no rules (dead) → prune . Left ; . UNIT: . Result ; . TERM/BIN: nothing needed (single terminal is legal). Clean CNF. ✓

Wrong order (TERM/BIN before DEL). TERM on : add , get . BIN: length 2, fine. Now DEL: nullable, patch → drop : . That is a unit rule ! It was created after the UNIT step would have run, so if you already ran UNIT, nothing removes it — CNF violated. You'd have to re-run UNIT.

Precise reason: TERM turns the harmless terminal into a variable ; then DEL's dropping of the nullable leaves the single variable , i.e. a rename. Doing DEL first means is still a terminal when it's dropped-to, giving legal , not a unit. Sipser's DEL-before-TERM avoids the cleanup loop.


Exercise 5.2

Full pipeline, everything at once. Convert (This is the parent note's Example 1 — reproduce the final CNF and independently verify contains the string .)

Recall Solution 5.2

Following the parent note's full run (START, DEL with , UNIT with pairs , TERM on , BIN on ) the final grammar is:

Check ? Trace : is there a start derivation of the single tile ? 's bodies are — none is a single directly. ? That gives , not . Try → gives . There is no rule producing exactly , and every branching body forces at least two symbols or an . So . Sanity against the original grammar: can derive ? , can be (nullable) giving ; you always keep at least the productive core. The shortest strings are and ; alone is not derivable from . Both grammars agree: . ✓ (This is a negative verification — a good habit.)


Exercise 5.3

Node-count mastery. In any CNF grammar, a derivation of a string of length uses exactly how many rule applications of type , and how many of type ? Prove the totals.

Recall Solution 5.3

Let be the string length.

  • Type (terminal rules): each produces exactly one final tile, and every tile is produced by exactly one such rule. So there are exactly of them.
  • Type (branching rules): think of the binary parse tree. It has leaves (the tiles). A full binary tree with leaves has exactly internal branching nodes. Each branching node corres