In computer science, a **context-free grammar** (**CFG**) is a formal grammar in which every production rule is of the form

- V
`->`*w*

Context-free grammars are important because they are powerful enough to describe the syntax of programming languages; in fact, almost all programming languages are defined via context-free grammars. On the other hand, context-free grammars are simple enough to allow the construction of efficient parsing algorithms which for a given string determine whether and how it can be generated from the grammar. See LR parser and LL parser for examples.

BNF (Backus-Naur Form) is often used to express context-free grammars.

Table of contents |

2 Derivations and Syntax trees 3 Normal forms 4 Properties of context-free languages |

A simple context-free grammar is

where | is used to separate different options for the same non-terminal and ε stands for the empty string. This grammar generates the languageThe distinction between leftmost derivation and rightmost derivation is important because in most parsers the transformation of the input is defined by giving a piece of code for every grammar rule that is executed whenever the rule is applied. Therefore it is important to know whether the parser determines a leftmost or a rightmost derivation because this determines the order in which the pieces of code will be executed. See for an example LL parsers and LR parsers.

A derivation also imposes in some sense a hierarchical structure on the string that is derived. For example the structure of the string "1 + 1 + 1" would, according to the leftmost derivation, be:

- S->S+S (1)
- S->S+S+S (1)
- S->1+S+S (2)
- S->1+1+S (2)
- S->1+1+1 (2)
- { { { 1 }
_{S}+ { 1 }_{S}}_{S}+ { 1 }_{S}}_{S}

S /|\\ / | \\ / | \\ S '+' S /|\\ | / | \\ | S '+' S '1' | | '1' '1'This tree is called a

- S-> S + S (1)
- S-> 1 + S (2)
- S-> 1 + S + S (1)
- S-> 1 + 1 + S (2)
- S-> 1 + 1 + 1 (2)

S /|\\ / | \\ / | \\ S '+' S | /|\\ | / | \\ '1' S '+' S | | '1' '1'If for certain strings in the language of the grammar there are more than one parsing tree then the grammar is said to be an

Every context-free grammar which does not generate the empty string can be transformed into an equivalent one in Chomsky Normal Form or Greibach normal form. "Equivalent" here means that the two grammars generate the same language.

Because of the especially simple form of production rules in Chomsky Normal Form grammars, this normal form has both theoretical and practical implications. For instance, given a context-free grammar, one can use the Chomsky Normal Form to construct a polynomial-time algorithm which decides whether a given string is in the language represented by that grammar or not (the CYK algorithm).

- An alternative and equivalent definition of context-free languages employs non-deterministic push-down automata: a language is context-free if and only if it can be accepted by such an automaton.
- The union and concatenation of two context-free languages is context-free; the intersection need not be.
- The reverse of a context-free language is context-free, but the complement need not be.
- Every regular language is context-free because it can be described by a regular grammar.
- The intersection of a context-free language and a regular language is always context-free.
- There exist context-sensitive languages which are not context-free.
- To prove that a given language is not context-free, one employs the pumping lemma for context-free languages.