Context-free Grammars and Derivations

Learn about context-free grammar and look at some examples.

We'll cover the following

Let’s formally define context-free grammar (CFG).

Context-free grammar

A context-free grammar is a formal grammar consisting of the following:

  • A set of variables (i.e., a nonterminal alphabet), VV, one of which is a start variable.
  • An alphabet, Σ\Sigma, of terminal symbols.
  • A set of production rules of the form vsv\rightarrow s, where vVv\in V and s(VΣ)s\in (V\cup\Sigma)^*, a string of terminals and/or variables.

Let’s look at some illustrations of CFGs for some of the languages.

Examples of CFGs

The following CFG generates the language {anbn    n>0}\{a^nb^n\;|\;n>0\}:

Get hands-on with 1200+ tech skills courses.