<?xml version="1.0" encoding="UTF-8"?>

<record version="30" id="2546">
 <title>context-free language</title>
 <name>ContextFreeLanguage</name>
 <created>2002-02-23 21:50:07</created>
 <modified>2009-08-19 16:00:16</modified>
 <type>Definition</type>
<parent id="8609">formal grammar</parent>
 <creator id="3771" name="CWoo"/>
 <author id="3771" name="CWoo"/>
 <author id="6075" name="rspuzio"/>
 <author id="6" name="Logan"/>
 <classification>
	<category scheme="msc" code="68Q45"/>
	<category scheme="msc" code="68Q42"/>
 </classification>
 <defines>
	<concept>context-free</concept>
	<concept>context-free grammar</concept>
 </defines>
 <synonyms>
	<synonym concept="context-free language" alias="type-2 language"/>
	<synonym concept="context-free language" alias="type-2 grammar"/>
 </synonyms>
 <related>
	<object name="Language"/>
	<object name="RegularLanguage"/>
	<object name="DeterministicFiniteAutomaton"/>
	<object name="NonDeterministicFiniteAutomaton"/>
	<object name="PushdownAutomaton"/>
	<object name="ContextSensitiveLanguage"/>
	<object name="DeterministicPushdownAutomaton"/>
 </related>
 <keywords>
	<term>syntax</term>
	<term>grammar</term>
	<term>formal language</term>
	<term>Chomsky</term>
 </keywords>
 <preamble>\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsfonts}</preamble>
 <content>A \emph{context-free language} is a language over some alphabet that can be generated from a particular kind of a \PMlinkescapetext{grammar} known as a \emph{context-free grammar}.

Formally, a \emph{context-free grammar} is a formal grammar $G=(\Sigma, N, P, \sigma)$ whose productions in $P$ have the form $$X\to w,$$ where $X\in N$ and $w\in \Sigma^*$.  A \emph{context-free language} is the formal language $L(G)$ generated by a context-free grammar $G$.  In other words,
$$L(G):=\lbrace v\in T^*\mid \sigma\stackrel{*}{\to} v\rbrace,$$
where $T:=\Sigma-N$ is the set of terminals, and $\sigma\stackrel{*}{\to}v$ simply means that the productions in $P$ are applied a finite number of times to the starting symbol $\sigma$ so that $v$ is reached in the end.

This formal definition is not initially very enlightening.  As an example, consider typical infix arithmetic notation.  An expression typically consists of one or more terms, joined together by operators with varying precedence.
Parentheses can be used to override operator precedence.  This syntax is a context-free language.  Let us describe such expressions with the operators \verb.+. and \verb.-. with lowest precedence, and \verb.*. and \verb./. with highest precedence, where all operators are left-associative and the only other allowed symbols are integer literals and parentheses.

\begin{eqnarray}
T &amp; := &amp; \left\{\verb.0., \verb.1., \verb.2., \verb.3., \verb.4.,
\verb.5., \verb.6., \verb.7., \verb.8., \verb.9., \verb.(., \verb.).,
\verb.+., \verb.-., \verb.*., \verb./. \right\} \nonumber \\
N &amp; := &amp; \left\{ S, A, B, C, D \right\} \nonumber \\
P &amp; := &amp; \{ (S, A), (S, S\verb.+.A), (S, S\verb.-.A), \nonumber \\
&amp;&amp; (A, B), (A, A\verb.*.B), (A, A\verb./.B), \nonumber \\
&amp;&amp; (B, C), (B, \verb.(.S\verb.).), \nonumber
(C, D), (C, CD), \nonumber \\
  &amp;    &amp;
(D, \verb.0.),
(D, \verb.1.),
(D, \verb.2.),
(D, \verb.3.),
(D, \verb.4.),
(D, \verb.5.),
(D, \verb.6.),
(D, \verb.7.),
(D, \verb.8.),
(D, \verb.9.)
\} \nonumber \\
\sigma &amp; := &amp; S \nonumber
\end{eqnarray}

It can be shown that the languages of the classical propositional logic and predicate logic are both context-free.

Context-free grammars are also known as Type-2 grammars in the Chomsky hierarchy.  The Chomsky hierarchy specifies Type-2 grammars as consisting only
of production rules of the form $A \to \gamma$, where $A$ is a non-terminal
and $\gamma$ is a string of terminals and non-terminals.

\textbf{Remarks}.
\begin{enumerate}
\item
A context-free grammar can be represented by a pushdown automaton.  The automaton serves both as an \emph{acceptor} for the language (that is, it can decide whether or not any arbitrary sentence is in the language) and as a \emph{generator} for the language (that is, it can generate any finite sentence in the language in finite time).
\item
The syntaxes of most programming languages are context-free grammars (or very close to it).  A very useful subset of context-free languages are regular languages.
\item
A context-free grammar $G$ is context-sensitive if none of its productions has the form $X\to \lambda$, where $\lambda$ is the empty word.
\item
The family of context-free languages has the following closure properties: union, concatenation, Kleene star, homomorphism, inverse homomorphism, reversal, and intersection with a regular language.  However, it is not closed under intersection, set difference, and complementation.
\end{enumerate}

\begin{thebibliography}{9}
\bibitem{sg} S. Ginsburg {\em The Mathematical Theory of Context-Free Languages}. McGraw-Hill, New York (1966).
\end{thebibliography}</content>
</record>
