Concatenation grammar Created 2026-09-24 Updated 2026-11-05
For context-free grammars and with disjoint nonterminal symbol sets, introduce a fresh nonterminal symbol and put
Then , the concatenation of the two formal languages. After the first production, every production rule acts within one of the two component parse trees. Their terminal words form one word from followed by one from . Conversely, any pair of component derivations can be performed after .
The construction does not work for arbitrary variable-based grammars. Let have production rules and , and let have the single production rule . Here is empty and , but the combined formal grammar derives . The production rule consumes a terminal symbol supplied by the other component, crossing the boundary that context-free grammar rules preserve.
Store the start nonterminal symbol on a stack. An epsilon transition nondeterministically replaces its top nonterminal symbol by a production rule right-hand side. A top terminal symbol can be removed only while consuming the same input symbol. With a protected stack bottom marker, enter a final control state when that marker is exposed. Requiring all input to have been read implements acceptance by final state. The stack records the unprocessed suffix of a leftmost derivation, proving equality of the recognized and generated formal languages.
Formal grammar Created 2026-09-24 Updated 2026-11-05
A formal grammar specifies how to generate a formal language by repeated applications of production rules. Write , where is a finite set of nonterminal symbols, is a disjoint finite alphabet of terminal symbols, is the start symbol, and is a finite set of production rules . In an unrestricted formal grammar, the word must contain at least one nonterminal symbol; may be any word over .
A grammar derivation replaces an occurrence of inside a word by . The generated formal language is
Intermediate words may still contain nonterminal symbols; these are sentential forms. For example, the production rules and give and generate exactly . Restricting each left side to one nonterminal symbol gives a context-free grammar.[ref]
Nonproductive nonterminal Created 2026-09-24 Updated 2026-11-05
A nonterminal symbol of a formal grammar is productive if for some word consisting only of terminal symbols; otherwise it is nonproductive. In a context-free grammar, a sentential form containing a nonproductive nonterminal symbol cannot derive a terminal word: the descendants of that occurrence would have to provide a terminal derivation from the nonterminal symbol. Consequently productions involving nonproductive nonterminal symbols can be discarded when simplifying a context-free grammar.
The context-free hypothesis matters. A general variable-based grammar can have a production rule while neither nor alone derives a terminal word. A terminal derivation from a larger context therefore need not demonstrate productivity of each individual nonterminal symbol.
Use the standard conversion of a context-free grammar to a pushdown automaton, implementing a leftmost derivation on a stack. Take control states , input alphabet , stack alphabet , initial control state , initial stack symbol , and sole final control state . The symbol is a fresh stack bottom marker. As in part (a), the top of the stack is written on the left.
Initialize with
In control state , replace a top nonterminal symbol by one of its production rule right-hand sides:
The replacements are written in the order in which they are to be processed. For example, replacing by leaves on top; an implementation using individual push operations pushes , then , then .
Match and pop top terminal symbols by
Finally include
All unlisted transitions have no choices; has no outgoing transitions. This is acceptance by final state, leaving the bottom marker on the stack. Taking the last transition too early cannot cause false acceptance, because an unread input suffix remains and cannot consume it.
For correctness, after initialization the stack contains the unprocessed part of a sentential form. Expanding a top nonterminal symbol simulates one step of a leftmost derivation, and popping a matching terminal symbol transfers it to the already-read input prefix. Thus if the input prefix consumed so far is and the stack above is , then . Acceptance gives for the entire input .
Conversely, any leftmost derivation of specifies choices of these transitions: expand the next nonterminal symbol, and read each resulting leading terminal symbol when it reaches the top. After all symbols of are consumed, only remains, enabling the move to . The nondeterministic pushdown automaton therefore recognizes exactly . The empty word is accepted by choosing after initialization.
Every generated word satisfies
where count occurrences of the indicated terminal symbols. To see this, follow the unique chain of occurrences in a parse tree. Each use of introduces one from , at least one from its rightmost , and possibly one further from . Each use of adds one and no . The terminating contributes neither. Hence no generated word has more symbols than symbols.
Also, for every , the word is generated: apply exactly times, choose each time, then use and all remaining .
Suppose were a regular language, and let be a pumping length from the pumping lemma for regular languages. Apply it to . Every allowed decomposition with and has for some . Pumping twice gives
which violates the proved counting inequality and hence is not generated. This contradicts the pumping lemma for regular languages. Therefore is not a regular language. Ambiguity was not used: the obstruction is the unbounded constraint on symbol counts.
In strict Chomsky normal form, every production of a context-free grammar is or , with nonterminal symbols and a terminal symbol. Such a derivation cannot produce the empty word: it starts with one nonterminal symbol, and every step either increases the symbol count or replaces one symbol by one symbol. The context-free language , generated by , is therefore not generated by any strict CNF grammar.
The convention matters: extended Chomsky normal form permits with excluded from right-hand sides. Under that convention every context-free language has such a grammar, so the requested example would not exist.
A context-free grammar is in Chomsky normal form when every production rule has one of the forms
where are nonterminal symbols and is a terminal symbol. If the language contains the empty word, one may additionally permit , with the start symbol absent from all right-hand sides.
A context-free grammar is in Chomsky normal form when every production is of one of the forms
where are nonterminal symbols and is a terminal symbol. Under this strict convention there is no epsilon production, so such a grammar cannot generate the empty word. Conversion of an arbitrary grammar therefore gives
If , the two languages are equal. An alternative extended convention permits the exceptional start production ; under that convention conversion preserves the whole language.
A context-free grammar is a quadruple consisting of a finite set of nonterminal symbols, a disjoint finite alphabet of terminal symbols, a start symbol , and a finite set of productions with and . A sentence is a terminal word over an alphabet with , and the generated formal language is .
The first language is context-free. For example, the productions
generate exactly : each recursive production adds two s to the left and two s to the right.
A context-free grammar is in Chomsky normal form when every production has the form or , where are nonterminals and is a terminal symbol; one may additionally allow the new start production when the language contains the empty string, provided never appears on a right-hand side.
The standard conversion proceeds as follows.
  • Introduce a fresh start symbol and . This changes only when the old start symbol occurs on a right-hand side or when a protected start symbol is needed to preserve .
  • Find all nullable nonterminals, add productions obtained by omitting nullable occurrences, and remove the original -productions except the permitted . This leaves unchanged.
  • Eliminate each unit production by adding to the non-unit productions reachable through its transitive closure. This leaves unchanged.
  • Delete non-generating symbols and then unreachable symbols. This is the only simplification stage that can shrink .
  • In every right-hand side of length at least two, replace a terminal by a fresh nonterminal with . New nonterminals are needed exactly for terminals that occur in such mixed or long productions.
  • Break every right-hand side of length at least three into binary productions, introducing fresh nonterminals for successive suffixes. New nonterminals are needed exactly when such long productions remain.
The terminal alphabet may be kept unchanged throughout, although unused terminals may be discarded by convention. Every stage preserves the generated language, with the explicit protection of at the start-symbol stage. For example,
is already in Chomsky normal form and generates the infinite language , so its conversion may be the grammar itself.
Sentential form 2026-11-05
A sentential form of a formal grammar is any word derivable from the start symbol: . It may contain both terminal symbols and nonterminal symbols. A terminal-only sentential form is a word of the generated formal language.
For productions and , the derivation contains four sentential forms. The intermediate is not a sentence of the terminal language because remains. An arbitrary word over need not be a sentential form: it must actually be reachable by a grammar derivation.
Tracking sentential forms makes it possible to discuss intermediate parsing states and distinguish leftmost derivations from other orders of rewriting. The word need not be a complete output or a prefix of one in an arbitrary grammar.
Stack alphabet 2026-10-31
The finite alphabet of symbols permitted on the stack of a pushdown automaton. It may contain nonterminal symbols, terminal symbols, and a fresh stack bottom marker, independently of the input alphabet.
In a formal grammar, terminal symbols are symbols of completed words; a nonterminal symbol is an auxiliary symbol that may be replaced by a production. The two alphabets are disjoint.