How statement and proof provenance work
The first chip identifies the source of the statement or construction; the second identifies the source of its local proof or verification.
- Literature-sourced: the exact statement appears in a cited source; only wording and notation differ.
- AI-adapted: a semantically identical restatement of literature-sourced material, modulo indexing, notation, and boundary cases adopted by the library.
- AI-generated: a genuinely novel statement formulated by AI, with no source for the claim itself.
These labels describe origin, not correctness: citations and verification chips remain separate evidence.
Context-Free Grammars and Normal Forms
1 · Prerequisites
- Construction of the Natural Numbers
- Countability and Uncountability
- Finite Counting, Factorials and Binomial Coefficients
- Formal Languages, Encodings, and Decision Problems
- Linear Recurrences and Rational Generating Functions
- Ramsey Theory
- Relations, Functions, and Quotients
- The ZFC Axioms and the Basic Set Constructions
2 · Summary
This page develops context-free grammars from derivations and parse trees through ambiguity, grammar simplification, Chomsky and Greibach normal forms, and the core closure constructions. The local route keeps the normal-form arguments constructive: useless, epsilon, and unit elimination are proved first, and the GNF conversion is justified by explicit substitution and left-recursion rewrite lemmas.
3 · Logical flowchart
4 · Definitions, theorems and proofs
Context-free grammars
Definition
A context-free grammar is a quadruple such that:
- is a finite set of variables.
- is a finite alphabet of terminals, disjoint from .
- is the start variable.
- is a finite set of productions of the form , where and is a finite word over the alphabet in the sense of Computation alphabets, words, the empty word, and .
The word may be empty, in which case the production is written .
Remarks
-
"Context-free" means that the left-hand side of every production is a single variable, independent of the surrounding sentential context.
-
The grammar is finite because both the variable set and the production set are finite.
One-step derivation and finite derivation in a context-free grammar
Definition
Let be a context-free grammar. A word over is a sentential form.
For sentential forms , write when there exist words over , a variable , and a production in such that This is the one-step derivation relation of , a relation in the sense of Relation, , , , and the specialisations "relation from to " and "relation on " on the set of sentential forms.
Write when is obtained from by finitely many one-step derivations, possibly zero. This is the reflexive transitive closure of , and a chain is a finite derivation from to .
The language generated by a CFG
Definition
Let be a context-free grammar. The language generated by is
A language over is context-free when for some context-free grammar over .
Leftmost and rightmost derivations
Definition
Let be a context-free grammar, and suppose is one derivation step.
This step is leftmost when contains no variables, so the rewritten occurrence of is the leftmost variable in the sentential form. It is rightmost when contains no variables, so the rewritten occurrence of is the rightmost variable.
A finite derivation is a leftmost derivation when each of its steps is leftmost, and a rightmost derivation when each of its steps is rightmost.
Parse trees and their yields
Definition
Let be a context-free grammar. A parse tree for is a finite, finitely branching rooted tree of finite sequences in the sense of Rooted trees of finite sequences, levels, branches, and finite branching, with ordered finite successor sets, together with a label on each node, satisfying the following conditions.
- The root is labelled by the start variable .
- Every internal node is labelled by a variable .
- If an internal node labelled has ordered children labelled , then is a production of .
- A leaf is either labelled by a terminal in , or is a variable leaf corresponding to a production and contributes no terminal symbol to the yield.
Reading the terminal leaves from left to right gives a word in , called the yield of the parse tree.
Remarks
-
The finite-sequence address set is what makes "left to right" precise: among siblings, the natural-number labels of successor nodes provide the order.
-
The tree itself is finite. This rules out infinite variable chains that do not correspond to a finite derivation.
-
An -production contributes no terminal letter to the yield.
Derivations and parse trees correspond
Statement
Let be a context-free grammar and let .
- The word is derived from in if and only if there exists a parse tree for with yield .
- Every parse tree for determines a unique leftmost derivation of its yield and a unique rightmost derivation of its yield.
Facts & Assumptions
Given: A context-free grammar and a word .
Leftmost and rightmost derivations are the derivations that rewrite the leftmost or rightmost remaining variable at each step, by Leftmost and rightmost derivations.
A parse tree is finite; each internal node labelled records exactly one production whose ordered children carry the labels ; variable leaves may represent -productions; and the yield is the left-to-right list of terminal leaves, by Parse trees and their yields.
Proof
Suppose first that . Start with one root labelled and process the derivation step by step. When a step rewrites a current leaf labelled by a nonempty right-hand side , make that leaf an internal node with ordered children labelled . When a step uses , leave that leaf in place as the variable leaf corresponding to the -production. Because the derivation is finite, this constructs a finite parse tree whose nodes record exactly the productions used in the derivation.
Conversely, suppose a parse tree for has yield . Start from the sentential form and repeatedly rewrite the leftmost variable leaf represented in the fixed tree: if that leaf is an internal node of the tree with ordered children , apply the production ; if it is a variable leaf corresponding to , apply that -production. By [L2], every step is licensed by , and by construction every step is leftmost in the sense of [L1]. Because the tree is finite, this process terminates after one step for each variable-labelled node, and the final terminal frontier is exactly the yield .
At the end of the construction in step 1.1, the leaves read left to right are exactly the symbols of the terminal word , because each derivation step replaces one leaf occurrence of by the same ordered right-hand side that the tree stores. So a derivation of yields a parse tree whose yield is .
The choice in step 1.2 is forced: at each stage there is a unique leftmost variable leaf of the fixed parse tree, so the leftmost derivation read from the tree is unique. The same argument, reading rightmost variable leaves instead, gives a unique rightmost derivation.
Steps 1.1 and 2.1 prove the forward implication of clause 1, and step 1.2 proves the reverse implication. Step 2.2 proves clause 2.
Ambiguous grammars, unambiguous grammars, and inherently ambiguous languages
Definition
Let be a context-free grammar.
The grammar is ambiguous when there exists a word having two distinct leftmost derivations in . By Derivations and parse trees correspond, this is equivalent to requiring two distinct finite parse trees for with yield .
The grammar is unambiguous when it is not ambiguous.
A context-free language is inherently ambiguous when every context-free grammar with is ambiguous.
Remarks
-
Ambiguity is a property of a grammar, while inherent ambiguity is a property of a language.
-
A language may admit both ambiguous and unambiguous grammars; in that case the ambiguous grammar is not evidence that the language is inherently ambiguous.
Nullable, generating, and reachable variables
Definition
Let be a context-free grammar.
- A variable is nullable when .
- A variable is generating when for some terminal word .
- A variable is reachable when there exist sentential forms over with
A variable is useful when it is both generating and reachable.
Eliminating useless symbols preserves the generated language
Statement
For every context-free grammar there exists a context-free grammar such that and either:
- and is the evident empty-language grammar, or
- every variable of is both generating and reachable.
Facts & Assumptions
Given: A context-free grammar .
Variables may be nullable, generating, reachable, or useful exactly as in Nullable, generating, and reachable variables.
The generated language is , by The language generated by a CFG.
Proof
Let be the generating variables of , and let consist of the productions whose left-hand side lies in and whose right-hand side contains no variable outside . If , then no terminal word is derivable from , so [L2] gives and we may take to be any fixed grammar generating the empty language. If , set .
In the case , let be the variables reachable from in , let consist of the productions in whose left-hand side lies in and whose right-hand side contains no variable outside , and set . Every derivation beginning at stays inside by definition of reachability.
Assume now that . Any derivation of a terminal word can use only generating variables, because every variable appearing in that derivation must eventually derive a terminal subword. Conversely, every rule kept in was already a rule of . Hence .
Therefore deleting the unreachable variables changes no derivation from to a terminal word, while every variable remaining in is both generating and reachable. So .
Taking in the case and the empty-language grammar in the case proves the theorem.
Epsilon-productions can be eliminated except possibly at the start symbol
Statement
For every context-free grammar there exists a context-free grammar such that:
- has no -productions and
- If , then there exists a context-free grammar with whose only -production is a start-symbol rule , where does not occur on any right-hand side.
Facts & Assumptions
Given: A context-free grammar .
A variable is nullable exactly when it derives , by Nullable, generating, and reachable variables.
The generated language is defined by derivability from the start symbol, by The language generated by a CFG.
Proof
Let be the nullable variables of . Form a grammar by deleting every production and, for each remaining production , adding every nonempty right-hand side obtained by deleting any chosen subset of nullable occurrences among the .
If , clause 1 already proves the theorem. Assume instead that . Introduce a fresh start variable and add the two productions and , leaving all productions of unchanged.
Any derivation of a nonempty terminal word in can be shortened to one in by collapsing each maximal family of nullable subderivations into a single use of one of the added productions. Conversely, every added production in can be expanded back in by re-inserting derivations of the deleted nullable variables to . Therefore [L2] gives .
The rule restores the missing empty word, while step 2.1 already preserved every nonempty terminal word. Because is fresh, it appears on no right-hand side, so this is the only -production of the new grammar . Hence .
Unit productions can be eliminated
Statement
A unit production is a production whose left- and right-hand sides are both single variables.
For every context-free grammar there exists a context-free grammar such that and has no unit productions.
Facts & Assumptions
Given: A context-free grammar .
By Epsilon-productions can be eliminated except possibly at the start symbol, we may first replace by an equivalent grammar whose only possible -production is a special start-symbol rule.
The generated language is defined by derivability from the start symbol, by The language generated by a CFG.
Proof
Replace by an equivalent grammar as in [L1]. For variables , write when using only unit productions. Now form by deleting all unit productions and, whenever and is a nonunit production of , adding the copied production .
Any derivation of a terminal word in can be compressed to a derivation in by replacing each maximal block of unit productions followed by one nonunit production with the copied rule placed into . Therefore every word of lies in .
Conversely, every copied production of came from some unit chain in and a nonunit production of . Expanding one use of back into that unit chain followed by turns any derivation in into a derivation in . So every word of lies in .
Steps 2.1 and 2.2 give , and has no unit productions by construction.
Chomsky normal form
Definition
A context-free grammar is in Chomsky normal form when every production of is of one of the following types:
- , where ;
- , where and ;
- optionally, , provided that does not occur on any right-hand side.
Remarks
-
The start-symbol -rule is an exception, not part of the generic binary-or-terminal pattern.
-
In CNF, every nonempty derivation step either expands one variable into two variables or replaces one variable by one terminal.
Every context-free language has an equivalent grammar in Chomsky normal form
Statement
Every context-free grammar has an equivalent context-free grammar in Chomsky normal form.
Facts & Assumptions
Given: A context-free grammar .
Useless symbols can be eliminated without changing the language, by Eliminating useless symbols preserves the generated language.
Epsilon-productions can be eliminated except for the special start-symbol exception, by Epsilon-productions can be eliminated except possibly at the start symbol.
Unit productions can be eliminated without changing the language, by Unit productions can be eliminated.
Chomsky normal form allows only productions of the shapes listed in Chomsky normal form.
Proof
Apply [L1], [L2], and [L3] in order to obtain an equivalent grammar in which every remaining production is nonunit and every -production is removed except possibly a start-symbol rule.
For each terminal that appears in a right-hand side of length at least , introduce a fresh variable with production , and replace that occurrence of by . This changes no generated word, because each use of can immediately be expanded to the terminal . After this step, every right-hand side is either a single terminal or a word of variables only.
For each production with , introduce fresh variables and replace the production by The middle family is empty when , so that case is the two-rule chain and . One use of the original long rule is equivalent to this forced chain of binary rules, and conversely the binary chain produces exactly the same string of variables.
After steps 2.1 and 3.1, every production is either , , or the exceptional start-symbol rule . Therefore [L4] says the resulting grammar is in Chomsky normal form, and the preceding steps preserved the language.
Greibach normal form
Definition
A context-free grammar is in Greibach normal form when every production is of the form where , , and is a possibly empty word of variables, together with the optional exception when does not occur on any right-hand side.
Remarks
-
In GNF, every nonempty derivation step emits one terminal immediately.
-
The optional start-symbol -rule plays the same role here as in Chomsky normal form.
Substituting a variable's productions into a leading variable occurrence preserves the language
Statement
Let be a context-free grammar containing the production Suppose the productions of in are exactly where are words over variables and terminals. Let be the grammar obtained by deleting the production and replacing it by the family while keeping every other production of .
Then
Facts & Assumptions
Given: Grammars and related by the displayed substitution step, with the displayed list equal to the full family of -productions in .
A finite derivation is a chain of one-step derivations, by One-step derivation and finite derivation in a context-free grammar.
The language consists of the terminal words derivable from the start symbol, by The language generated by a CFG.
Proof
Let be one use of the deleted production in a derivation of a terminal word, and follow the distinguished occurrence of created by this step. It is eventually rewritten by some , since the displayed rules are all the productions of . Every intervening step rewrites a different variable occurrence. Such a step commutes with the rewrite of the distinguished : applying the two context-free productions in either order gives the same sentential form. We may therefore move leftward past all intervening steps and replace the resulting adjacent fragment by the single -step . Restoring the commuted surrounding-context steps gives the same final terminal word.
Conversely, every use in of a substituted production can be expanded in to the two steps Replacing each substituted step in a derivation of a terminal word in by this two-step fragment yields a derivation in with the same yield. Hence [L2] gives .
If a terminal derivation in never uses the deleted production , then it is also a derivation in . Otherwise apply step 1.1 to one use of . This decreases by one the number of uses of the deleted production in the finite derivation without changing its terminal yield. Repeating finitely many times produces an -derivation of the same word, so [L2] gives .
The two inclusions from steps 2.1 and 1.2 prove .
Eliminating immediate left recursion preserves the language
Statement
Suppose the productions of a variable in a context-free grammar are exactly where none of the words begins with . Introduce a fresh variable and replace those productions by leaving every other production unchanged.
Then the resulting grammar generates the same language as the original grammar.
Facts & Assumptions
Given: The original grammar and the rewritten grammar related by the displayed immediate-left-recursion elimination step.
Leftmost derivations always rewrite the leftmost remaining variable, by Leftmost and rightmost derivations.
The language of a grammar is determined by the terminal words derivable from its start symbol, by The language generated by a CFG.
Proof
Consider a leftmost derivation segment in the original grammar beginning with . Because every left-recursive rule has the form , any maximal initial block of left-recursive steps must look like So the contribution of this segment is always one non-left-recursive word followed by a finite concatenation of the .
The new variable generates exactly the nonempty finite concatenations of the : use for a one-term concatenation and to prepend further 's. Therefore the segment in step 1.1 is reproduced in the rewritten grammar either by when or by followed by a derivation of the same tail from when .
Conversely, any derivation segment in the rewritten grammar beginning with either uses directly or uses followed by a derivation of a finite concatenation of the from . Reversing the regrouping in step 1.1 turns that segment back into a block of left-recursive uses of ending with in the original grammar.
Since all other productions are unchanged, steps 2.1 and 3.1 convert derivations of terminal words back and forth without changing the yield. Hence [L2] gives equality of the generated languages.
Every context-free language has an equivalent grammar in Greibach normal form
Statement
Every context-free grammar has an equivalent context-free grammar in Greibach normal form.
Facts & Assumptions
Given: A context-free grammar .
By Every context-free language has an equivalent grammar in Chomsky normal form, we may first replace by an equivalent grammar in Chomsky normal form.
If a production is replaced by the family formed from all productions of , then the generated language is preserved, by Substituting a variable's productions into a leading variable occurrence preserves the language.
Eliminating immediate left recursion preserves the generated language, by Eliminating immediate left recursion preserves the language.
Greibach normal form means that every nonexceptional production has the form with leading terminal , by Greibach normal form.
Proof
Replace by an equivalent CNF grammar as in [L1], and order its original variables as . In a forward pass through this list, fix and repeatedly use [L2] to remove every production of the form with . When that is done, use [L3] to eliminate the remaining immediate left-recursive productions , introducing an auxiliary variable if needed. After stage , every production of begins either with a terminal or with an original variable satisfying , and every new production of begins either with a terminal or with an original variable.
In particular, after the forward pass the productions of already begin with terminals, because there is no index . Now argue downward on . Assume the productions of already begin with terminals. Every production of begins either with a terminal or with some having by step 1.1, so repeated use of [L2] substitutes the full terminal-leading production family of each such into the leading occurrence. Thus every production of can also be made terminal-leading without changing the language.
The same downward substitutions clean every auxiliary variable introduced during the left-recursion eliminations. By step 1.1, each production of such a already begins either with a terminal or with an original variable . Once the productions of each are terminal-leading by step 2.1, repeated use of [L2] removes that leading variable from the productions of as well. Therefore, after finitely many substitutions, every production in the whole grammar begins with a terminal, except possibly the start-symbol rule .
Because the construction started from CNF, the symbols after the first terminal in any rewritten production are variables, and the optional start-symbol rule is preserved only in the exceptional case allowed by [L4]. Hence the final grammar is in Greibach normal form, and [L2] and [L3] preserved equivalence throughout.
Context-free languages are closed under union, concatenation, Kleene star, and homomorphism
Statement
If are context-free languages over an alphabet , and if is an alphabet map, then the following languages are context-free:
- ,
- ,
- ,
- .
Facts & Assumptions
Given: Context-free languages and , together with grammars and generating them, and an alphabet map .
A language is context-free exactly when it is generated by some context-free grammar, by The language generated by a CFG.
The induced word map is obtained by replacing each letter of a word by the word and concatenating the results, by Alphabet maps into and their induced maps on words.
Proof
Rename variables of and apart, and add a fresh start variable . The grammar with productions of and together with and generates exactly . Similarly, adding only the bridge rule yields a grammar for the concatenation .
For Kleene star, keep and add a fresh start variable with productions and . Repeating the second rule any finite number of times and then using generates exactly a concatenation of finitely many words from , so the new grammar generates .
For homomorphism, first rename the variables of the chosen grammar for so that they are disjoint from . Then replace every terminal letter occurring in a production by the word , keeping that renamed variable set unchanged. The result is still a context-free grammar over , and if a derivation in the source grammar yields , then the corresponding derivation in the rewritten grammar yields by [L2].
Conversely, any terminal derivation in the rewritten grammar follows the same variable-expansion pattern as a terminal derivation in the renamed source grammar, because the only change was to replace each source terminal occurrence by its prescribed image word . Therefore the terminal word produced at the end is exactly the image under of a word of , so the new grammar generates exactly .
Steps 1.1, 1.2, 1.3, and 2.1 construct grammars for all four languages, so [L1] shows that context-free languages are closed under union, concatenation, Kleene star, and homomorphism.
FALSE: an ambiguous CFG always generates an inherently ambiguous language
Statement
False claim: if a context-free grammar is ambiguous, then the language it generates is inherently ambiguous.
Facts & Assumptions
Given: The grammar
The statement refuted is: every ambiguous context-free grammar generates an inherently ambiguous language.
A context-free language is inherently ambiguous when every context-free grammar generating it is ambiguous, by Ambiguous grammars, unambiguous grammars, and inherently ambiguous languages.
Refutation
The displayed grammar is ambiguous: the word has one parse corresponding to and another corresponding to . So the grammar is an ambiguous CFG.
The standard precedence grammar generates the same terminal strings. One inclusion follows by simultaneous induction on its -, -, and -derivations, since every displayed production can be simulated by the first grammar. For the reverse inclusion, scan a generated word at parenthesis depth zero: if there is a +, split at the rightmost such +; otherwise, if there is a *, split at the rightmost such *; the remaining factor is either , , or a parenthesized expression. Induction on the word length gives a derivation in the precedence grammar. These rightmost top-level splits are forced by its left-recursive - and -rules, while the -case is forced by the outer symbols, so the resulting parse is unique. In particular, has only the parse . Thus the language has an unambiguous grammar.
By [L1], a language with an unambiguous grammar is not inherently ambiguous. So the ambiguous grammar from step 1.1 does not force its language to be inherently ambiguous, and [A1] is false.
FALSE: converting to Chomsky normal form preserves epsilon without a special start-symbol exception
Statement
False claim: converting a context-free grammar to Chomsky normal form preserves without any special exception.
Facts & Assumptions
Given: The grammar with one variable and one production,
The statement refuted is: CNF conversion preserves without a special start-symbol exception.
Chomsky normal form allows an -production only in the special case with absent from every right-hand side, by Chomsky normal form.
Every context-free grammar is equivalent to one in Chomsky normal form, by Every context-free language has an equivalent grammar in Chomsky normal form.
Refutation
The given grammar generates exactly the language . So any equivalent CNF grammar must still generate .
Without the exception described in [L1], every CNF production would have the form or . Such rules can produce only nonempty terminal words, because every terminal step contributes one symbol and no rule deletes all remaining symbols. Therefore a grammar with only those two rule types cannot generate .
Step 1.2 contradicts the requirement from step 1.1, so the special start-symbol exception is genuinely necessary. The claim [A1] is false.
5 · Examples, counterexamples and false statements
None yet.
Sources
- Jean Gallier and Jocelyn Quaintance, Introduction to the Theory of Computation: Some Notes for CIS511
- John Watrous, Introduction to the Theory of Computing, Lecture 7
- John Watrous, Introduction to the Theory of Computing, Lecture 8
- CSC444 CFLs and PDAs
- John Watrous, Introduction to the Theory of Computing, Lecture 9
- Alfred V. Aho, COMS W3261 CS Theory, Lecture 11: Properties of CFL's