A grammar is context-free when each production has one nonterminal on its left.
A context-free grammar consists of terminal and nonterminal alphabets, a start symbol, and productions whose left side is one nonterminal. A derivation records the order of substitutions, while a parse tree records their hierarchical structure, so several derivations can represent one parse tree.
A right-linear grammar has productions of the form or , where are nonterminals and is a terminal word, possibly empty. Each right-hand side has at most one nonterminal, at its right end. Such grammars generate exactly regular languages: regard nonterminals as states and expand each terminal word into a finite path to another state or an accepting endpoint. Conversely the transition graph of a deterministic finite automaton gives these productions directly.
A nonterminal symbol is a grammar symbol that may be replaced by a production and need not occur in a completed word.
A terminal symbol belongs to the alphabet of completed words and is not replaced by a grammar production.
A production rule specifies a permitted replacement during a grammar derivation. In a context-free grammar it has the form , with one nonterminal on the left.
A sentence of a grammar is a terminal word derivable from its start symbol.
A grammar derivation is a finite sequence of words in which each step replaces one occurrence of a production's left side by its right side. A leftmost derivation always replaces the leftmost available nonterminal.
A parse tree places the start symbol at its root, the symbols on the right side of a production at the children of its left-side nonterminal, and terminal symbols at its leaves. Reading the leaves from left to right gives the tree's yield.
Two grammars are equivalent when they generate the same formal language.
Two grammars over a common terminal alphabet are isomorphic when a bijection between their nonterminal alphabets maps one start symbol to the other and maps their productions bijectively after being extended to fix every terminal symbol. Isomorphic grammars are equivalent.
A context-free grammar is in Chomsky normal form when every production has the form or , with nonterminals and a terminal. If the empty word belongs to the language, one commonly also permits the exceptional rule under the usual restriction on the start symbol.
An epsilon production is a grammar production whose right-hand side is the empty word.
A unit production replaces one nonterminal by one nonterminal and emits no terminal symbols.
To convert a context-free grammar to Chomsky normal form, first add a fresh start symbol. Remove epsilon productions, retaining the exceptional start production only when the empty word is to remain in the language; then remove unit productions and symbols that cannot participate in a terminal derivation. Replace each terminal occurring in a right-hand side of length at least two by a new nonterminal that produces that terminal. Finally split every right-hand side of length greater than two into binary productions using fresh nonterminals.
A Chomsky-normal-form parse tree for a word of length has terminal productions and binary productions. Every derivation represented by the tree therefore has exactly steps.
The CYK algorithm decides membership in a context-free language given by a grammar in Chomsky normal form. It records, for every substring, the nonterminals that derive that substring and combines adjacent substrings using binary productions. For a word of length and a fixed grammar, it runs in time.
Weak Chomsky normal form additionally permits ternary productions . Compressing a two-step pattern , into preserves its generated yields whenever the intermediate variable has no other role, and saves one derivation step each time the pattern is used.
The productions generate exactly the nonempty binary palindromes of even length.
The intersection of a context-free language with a regular language is context-free, because a pushdown automaton and a finite automaton can be run in product.
If context-free grammars with disjoint nonterminal alphabets have start symbols , adjoining a new start symbol and the production gives a context-free grammar for the concatenation of their languages.
If a context-free grammar for has start symbol , adjoining a new start symbol and productions gives a context-free grammar for the Kleene star .
Every context-free language over a one-symbol alphabet is a regular language. Equivalently, the set of word lengths of a unary context-free language is ultimately periodic. This follows, for example, from the one-dimensional case of Parikh's theorem.
A context-free grammar can enforce sums such as by nesting one matched terminal pair for each independent count.
The language
is context-free. The grammar
nests the dependency around the dependency.

Ancestors (5)

  1. Formal language theory
  2. Foundations of mathematics
  3. Area of mathematics
  4. Mathematics
  5. Home

Discussion (0)

New discussion

There are no discussions about this article yet.

Articles by others on the same topic (2)

Context-free language by Ciro Santilli 40 Created 2025-02-13 Updated 2025-07-16
A **context-free language (CFL)** is a type of formal language that can be generated by a context-free grammar (CFG). In formal language theory, context-free languages are significant because they can describe a wide range of syntactic structures used in programming languages and natural languages. ### Key Concepts: 1. **Context-Free Grammar (CFG)**: - A CFG consists of a set of production rules that define how symbols in the language can be replaced or transformed.