Context-Free Languages: Simplification & Normal Forms

grammar simplification

Before you renovate a house you clear out the junk: broken furniture nobody can reach, rooms with no door, light switches wired to nothing. Grammar simplification is the same housekeeping for a context-free grammar. It removes rules and symbols that add clutter without changing what the grammar produces, leaving a leaner grammar that generates exactly the same language.

Simplification is a small pipeline of cleanup steps, each preserving the language. First you remove epsilon-productions, the rules of the form A -> epsilon (the empty string) that let a variable vanish; then unit productions, the rules A -> B that just rename one variable as another; and you remove useless symbols, which split into non-generating symbols (variables that can never derive a string of terminals, so they are dead ends) and unreachable symbols (variables the start symbol can never reach, so they never get used). After the dust settles you have a grammar with no empty rules, no rename-only rules, and no dead weight, while every string of the original language is still derivable.

Simplification matters because it is the front half of converting a grammar to a normal form. Chomsky normal form, for instance, has no epsilon-rules and no unit-rules by definition, so you must clean those out first. It also makes a grammar smaller and faster to parse and easier to reason about. The order of steps is not free: removing epsilon-productions can create new unit productions, and removing symbols can create new uselessness, so the steps must be sequenced carefully (see the order of simplification steps).

Given S -> AB | a, A -> a, B -> B, C -> aC, with C never appearing on a reachable right-hand side and B only rewriting to itself: B is non-generating (it can only become B forever) and C is unreachable from S. Cleanup deletes B, C, and the rule S -> AB, leaving S -> a, A -> a, which generates the same single string a.

Pruning dead variables and the rules that mention them, without changing the language generated.

Simplification preserves the language but may change parse trees. If semantic actions are attached to the deleted rules, they must be moved elsewhere or the meaning is lost.

Also called
grammar cleanupgrammar normalization preprocessing文法清理文法淨化