Formal grammar

In computer science a formal grammar is an abstract structure that describes a formal language precisely, i.e., a set of rules that mathematically delineates a (usually infinite) set of finite-length strings over a (usually finite) alphabet. Formal grammars are so named by analogy to grammar in human languages.

Formal grammars fall into two main categories: generative and analytic.


 * A generative grammar, the most well-known kind, is a set of rules by which all possible strings in the language to be described can be generated by successively rewriting strings starting from a designated start symbol. A generative grammar in effect formalizes an algorithm that generates strings in the language.


 * An analytic grammar, in contrast, is a set of rules that assume an arbitrary string to be given as input, and which successively reduce or analyze that input string yielding a final boolean, "yes/no" result indicating whether or not the input string is a member of the language described by the grammar. An analytic grammar in effect formally describes a parser for a language.

In short, an analytic grammar describes how to read a language, whereas a generative grammar describes how to write it.

Generative grammars
A generative grammar consists of a set of rules for transforming strings. To generate a string in the language, one begins with a string consisting of only a single "start" symbol, and then successively applies the rules (any number of times, in any order) to rewrite this string. The language consists of all the strings that can be generated in this manner. Any particular sequence of legal choices taken during this rewriting process yields one particular string in the language, and if there are multiple different ways of generating a single string, then the grammar is said to be ambiguous.

For example, assume the alphabet consists of '$$a$$' and '$$b$$', the start symbol is '$$S$$' and we have the following rules:


 * 1. $$S \longrightarrow aSb$$
 * 2. $$S \longrightarrow ba$$

then we start with "$$S$$", and can choose a rule to apply to it. If we choose rule 1, we replace '$$S$$' with '$$aSb$$' and obtain "$$aSb$$". If we choose rule 1 again, we replace '$$S$$' with '$$aSb$$' and obtain "$$aaSbb$$". This process is repeated until we only have symbols from the alphabet (i.e., '$$a$$' and '$$b$$'). Finishing off our example, if we now choose rule 2, we replace '$$S$$' with '$$ba$$' and obtain "$$aababb$$", and are done. We can write this series of choices more briefly, using symbols: $$S \longrightarrow aSb \longrightarrow aaSbb \longrightarrow aababb$$. The language of the grammar is the set of all the strings that can be generated using this process: $$\left \{ba, abab, aababb, aaababbb, ...\right \}$$.

Formal definition
In the classic formalization of generative grammars first proposed by Noam Chomsky in the 1950s, a grammar G consists of the following components:
 * A finite set $$N$$ of nonterminal symbols.
 * A finite set $$\Sigma$$ of terminal symbols that is disjoint from $$N$$.
 * A finite set $$P$$ of production rules where a rule is of the form
 * string in $$(\Sigma \cup N)^{*} \longrightarrow$$ string in $$(\Sigma \cup N)^{*} $$


 * (where $${}^{*}$$ is the Kleene star and $$\cup$$ is union) with the restriction that the left-hand side of a rule (i.e., the part to the left of the $$\longrightarrow$$) must contain at least one nonterminal symbol.

Usually such a formal grammar $$G$$ is simply summarized as the quad-tuple $$(N, \Sigma, P, S)$$.
 * A symbol $$S$$ in $$N$$ that is indicated as the start symbol.

The language of a formal grammar $$G = (N, \Sigma, P, S)$$, denoted as $$\boldsymbol{L}(G)$$, is defined as all those strings over $$\Sigma$$ that can be generated by starting with the start symbol $$S$$ and then applying the production rules in $$P$$ until no more nonterminal symbols are present.

Example
For these examples, formal languages are specified using set-builder notation.

Consider, for example, the grammar $$G$$ with $$N = \left \{S, B\right \}$$, $$\Sigma = \left \{a, b, c\right \}$$, $$P$$ consisting of the following production rules


 * 1. $$S \longrightarrow aBSc$$
 * 2. $$S \longrightarrow abc$$
 * 3. $$Ba \longrightarrow aB$$
 * 4. $$Bb \longrightarrow bb $$

and the nonterminal symbol $$S$$ as the start symbol. Some examples of the derivation of strings in $$\boldsymbol{L}(G)$$ are:


 * $$\boldsymbol{S} \longrightarrow (2) abc$$
 * $$\boldsymbol{S} \longrightarrow (1) aB\boldsymbol{S}c \longrightarrow (2) a\boldsymbol{Ba}bcc \longrightarrow (3) aa\boldsymbol{Bb}cc \longrightarrow (4) aabbcc$$
 * $$\boldsymbol{S} \longrightarrow (1) aB\boldsymbol{S}c \longrightarrow (1) aBaB\boldsymbol{S}cc \longrightarrow (2) a\boldsymbol{Ba}Babccc \longrightarrow (3) aaB\boldsymbol{Ba}bccc\longrightarrow (3) aa\boldsymbol{Ba}Bbccc $$$$ \longrightarrow (3) aaaB\boldsymbol{Bb}ccc \longrightarrow (4) aaa\boldsymbol{Bb}bccc \longrightarrow (4) aaabbbccc$$

(where the used production rules are indicated in brackets and the replaced part is each time indicated in bold).

It is clear that this grammar defines the language $$\left \{ a^{n}b^{n}c^{n} | n > 0 \right \}$$ where $$a^{n}$$ denotes a string of n $$a$$'s. Thus, the entire language consists of any positive number of 'a's, followed by the same number of 'b's followed by the same number of 'c's.

Generative formal grammars are identical to Lindenmayer systems (L-systems), except that L-systems are not affected by a distinction between terminals and nonterminals, L-systems have restrictions on the order in which the rules are applied, and L-systems can run forever, generating an infinite sequence of strings. Typically, each string is associated with a set of points in space, and the "output" of the L-system is defined to be the limit of those sets.

The Chomsky Hierarchy
When Noam Chomsky first formalized generative grammars in 1956, he classified them into four types now known as the Chomsky hierarchy. The difference between these types is that they have increasingly strict production rules and can express fewer formal languages. Two important types are context-free grammars and regular grammars. The languages that can be described with such a grammar are called context-free languages and regular languages, respectively. Although much less powerful than unrestricted grammars, which can in fact express any language that can be accepted by a Turing machine, these two restricted types of grammars are most often used because parsers for them can be efficiently implemented. For example, for context-free grammars there are well-known algorithms to generate efficient LL parsers and LR parsers.

Context-free grammars
In context-free grammars, the left hand side of a production rule may only be formed by a single non-terminal symbol. The language defined above is not a context-free language, but for example the language $$\left \{ a^{n}b^{n} | n > 0 \right \}$$ (any positive number of 'a's followed by the same number of 'b's) is, as it can be defined by the grammar $$G2$$ with $$N=\left \{S\right \}$$, $$\Sigma=\left \{a,b\right \}$$, $$S$$ the start symbol, and the following production rules:


 * 1. $$S \longrightarrow aSb$$
 * 2. $$S \longrightarrow ab$$

Regular grammars
In regular grammars, the left hand side is again only a single non-terminal symbol, but now the right-hand side is also restricted: It may be nothing, or a single terminal symbol, or a single terminal symbol followed by a non-terminal symbol, but nothing else. (Sometimes a broader definition is used: one can allow longer strings of terminals or single non-terminals without anything else, making languages easier to denote while still defining the same class of languages.)

The language defined above is not regular, but the language $$\left \{ a^{n}b^{m} | m,n > 0 \right \}$$ (any positive number of 'a's followed by any positive number of 'b's, where the numbers may be different) is, as it can be defined by the grammar $$G3$$ with $$N=\left \{S, A,B\right \}$$, $$\Sigma=\left \{a,b\right \}$$, $$S$$ the start symbol, and the following production rules:


 * 1. $$S \longrightarrow aA$$
 * 2. $$A \longrightarrow aA$$
 * 3. $$A \longrightarrow bB$$
 * 4. $$B \longrightarrow bB$$
 * 5. $$B \longrightarrow \epsilon$$

(where $$\epsilon$$ denotes the empty string, i.e. the string of length 0).

In practice, regular grammars are commonly expressed using regular expressions.

Regular vs. Context-Free Languages
Aside from the differences in production rules required to generate the two languages, the key high-level difference between $$\left \{ a^{n}b^{n} | n > 0 \right \}$$ (context-free) and $$\left \{ a^{n}b^{m} | n,m > 0 \right \}$$ (regular) is the specification that the number of 'a's and the number of 'b's must be equal in the context-free language. Thus, any automaton attempting to recognize the context-free language must necessarily keep track of more information than one that is attempting to recognize the regular language. The latter does not have to count the number of 'a's or 'b's, it just has to make sure there are more than zero of each.

For more detail, see context-free language and regular language.

Other forms of generative grammars
Many extensions and variations on Chomsky's original hierarchy of formal grammars have been developed more recently, both by linguists and by computer scientists, usually either in order to increase their expressive power or in order to make them easier to analyze or parse. Of course these two goals tend to be at odds: the more expressive a grammar formalism is, the harder it is to analyze or parse using automated tools. Some forms of grammars more recently developed include:


 * Tree-adjoining grammars increase the expressiveness of conventional generative grammars by allowing rewrite rules to operate on parse trees instead of just strings.
 * Affix grammars and attribute grammars allow rewrite rules to be augmented with semantic attributes and operations, useful both for increasing grammar expressiveness and for constructing practical language translation tools.

A yearly conference is devoted to formal grammars:

Analytic grammars
Though there is a tremendous body of literature on parsing algorithms, most of these algorithms assume that the language to be parsed is initially described by means of a generative formal grammar, and that the goal is to transform this generative grammar into a working parser. An alternative approach is to formalize the language in terms of an analytic grammar in the first place, which more directly corresponds to the structure of a parser for the language. Examples of analytic grammar formalisms include the following:


 * The Language Machine directly implements unrestricted analytic grammars. Substitution rules are used to transform an input to produce outputs and behaviour. The system can also produce the lm-diagram which shows what happens when the rules of an unrestricted analytic grammar are being applied.
 * Top-down parsing language (TDPL): a highly minimalist analytic grammar formalism developed in the early 1970s to study the behavior of top-down parsers.
 * Parsing expression grammars (PEGs): a more recent generalization of TDPL designed around the practical expressiveness needs of programming language and compiler writers.
 * Link grammars: a form of analytic grammar designed for linguistics, which derives syntactic structure by examining the positional relationships between pairs of words.