In formal language theory, a context-free grammar (CFG) is a formal grammar whose production rules are of the form
A
→
α
{\displaystyle A\ \to \ \alpha }
with
A
{\displaystyle A}
a single nonterminal symbol, and
α
{\displaystyle \alpha }
a string of terminals and/or nonterminals (
α
{\displaystyle \alpha }
can be empty). A formal grammar is "context free" if its production rules can be applied regardless of the context of a nonterminal. No matter which symbols surround it, the single nonterminal on the left hand side can always be replaced by the right hand side. This is what distinguishes it from a context-sensitive grammar.
A formal grammar is essentially a set of production rules that describe all possible strings in a given formal language. Production rules are simple replacements. For example, the first rule in the picture,
⟨
Stmt
⟩
→
⟨
Id
⟩
=
⟨
Expr
⟩
;
{\displaystyle \langle {\text{Stmt}}\rangle \to \langle {\text{Id}}\rangle =\langle {\text{Expr}}\rangle ;}
replaces
⟨
Stmt
⟩
{\displaystyle \langle {\text{Stmt}}\rangle }
with
⟨
Id
⟩
=
⟨
Expr
⟩
;
{\displaystyle \langle {\text{Id}}\rangle =\langle {\text{Expr}}\rangle ;}
. There can be multiple replacement rules for a given nonterminal symbol. The language generated by a grammar is the set of all strings of terminal symbols that can be derived, by repeated rule applications, from some particular nonterminal symbol ("start symbol").
Nonterminal symbols are used during the derivation process, but do not appear in its final result string.
Languages generated by context-free grammars are known as context-free languages (CFL). Different context-free grammars can generate the same context-free language. It is important to distinguish the properties of the language (intrinsic properties) from the properties of a particular grammar (extrinsic properties). The language equality question (do two given context-free grammars generate the same language?) is undecidable.
Context-free grammars arise in linguistics where they are used to describe the structure of sentences and words in a natural language, and they were invented by the linguist Noam Chomsky for this purpose. By contrast, in computer science, as the use of recursively-defined concepts increased, they were used more and more. In an early application, grammars are used to describe the structure of programming languages. In a newer application, they are used in an essential part of the Extensible Markup Language (XML) called the Document Type Definition.In linguistics, some authors use the term phrase structure grammar to refer to context-free grammars, whereby phrase-structure grammars are distinct from dependency grammars. In computer science, a popular notation for context-free grammars is Backus–Naur form, or BNF.
You do not have permission to view the full content of this post.
Log in or register now.