Q4Theory of Computation
Question
Simplify the given CFG by removing null and unit productions.
Answer
Simplifying a CFG removes null productions by adding omitted-symbol variants, then removes unit productions by transitive substitution, illustrated on a worked example grammar.
A production is called null/nullable. First identify all nullable non-terminals: is nullable if directly, or if where every is nullable. Then, for every production where contains one or more nullable non-terminals, add new productions with every combination of nullable symbols omitted (excluding the case where everything is omitted, unless is otherwise needed to preserve the language, in which case it is retained only for the start symbol). Finally, delete all productions of the exact form (except possibly for the start symbol, if ).
A production of the form , where both and are non-terminals, is a unit production. These are eliminated by computing, for each non-terminal , the set of non-terminals reachable via a chain of unit productions, then replacing each unit production with copies of all of 's non-unit productions directly under : i.e., if is a non-unit production, add . This is repeated (or computed via transitive closure) until no unit productions remain.
Let : , , . Step 1: is nullable (direct). Is nullable? and is nullable, so is also nullable. Is nullable? , both nullable, so is nullable too, but since is the start symbol we may retain only if required; here we proceed assuming need not be specially preserved unless asked. Rewrite productions accounting for nullable symbols: from , add (omit B) and (omit A); from , since is nullable, add but this is itself a null production so it is simply noted and then dropped after use. Remove all epsilon rules: and any newly introduced ones are deleted. Resulting grammar: , , .
Step 2: eliminate unit productions , , and . For : 's only non-unit production is , so add . For : 's non-unit production is (and is itself a unit production, so trace further: , giving transitively), so add and (already present). For : add . After removing the unit productions themselves, the final simplified grammar is: , , .
Removing null and unit productions (along with useless symbols — non-generating non-terminals that derive no terminal string, and unreachable symbols not derivable from the start symbol) is a mandatory preprocessing step before converting a CFG to Chomsky Normal Form, since CNF explicitly disallows both epsilon rules (except possibly at the start symbol) and single-non-terminal right-hand sides. It also produces a cleaner, more compact grammar, useful for both parser generation and for the formal proofs that rely on CNF-style structural bounds, such as the CFL pumping lemma and CYK parsing.