The basic idea behind these grammars is that we generate strings by beginning with a special start symbol and then apply rules that indicate how certain combinations of symbols may be replaced with other combinations of symbols. For example, assume the alphabet consists of 'a' and 'b', the start symbol is 'S' and we have the following rules:
then we can rewrite "S" to "aSb" by replacing 'S' with "aSb" (rule 1), and we can then rewrite "aSb" to "aaSbb" by again applying the same rule. This is repeated until the result contains only symbols from the alphabet. In our example we can rewrite S as follows: S > aSb > aaSbb > aababb. The language of the grammar then consists of all the strings that can be generated that way; in this case: ba, abab, aababb, aaababbb, etc.

A formal grammar G consists of the following components:
The language of a formal grammar G = (N, Σ, P, S), denoted as L(G), is defined as all those strings over Σ 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.
Consider, for example, the grammar G with N = {S, B}, Σ = {a, b, c}, P consisting of the following production rules
and the nonterminal symbol S as the start symbol. Some examples of the derivation of strings in L(G) are: (The used production rules are indicated in brackets and replaced part is each time indicated in bold.)
Formal grammars are identical to Lindenmayer systems (Lsystems), except that Lsystems are not affected by a distinction between terminals and nonterminals, Lsystems have restrictions on the order in which the rules are applied, and Lsystems 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 Lsystem is defined to be the limit of those sets.
Some restricted classes of grammars, and the languages that can be derived with them, have special names and are studied separately. One common classification system for grammars is the Chomsky hierarchy, a set of four types of grammars developed by Noam Chomsky in the 1950s. The difference between these types is that they have increasingly stricter production rules and can express less formal languages. Two important types are contextfree grammars and regular grammars. The languages that can be described with such a grammar are called contextfree 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 types of grammars are most often used because parsers for them can be efficiently implemented. For example, for contextfree grammars there are wellknown algorithms to generate efficient LL parsers and LR parsers.
In contextfree grammars, the left hand side of a production rule may only be formed by a single nonterminal symbol. The language defined above is not a contextfree language, but for example the language { a^{n}b^{n}  n > 0 } is, as it can be defined by the grammar G2 with N={S}, Σ={a,b}, S the start symbol, and the following production rules:
In regular grammars, the left hand side is again only a single nonterminal symbol, but now the righthand side is also restricted: It may be nothing, or a single terminal symbol, or a single terminal symbol followed by a nonterminal symbol, but nothing else (sometimes a broader definition is used, one can allow longer strings of terminals or single nonterminals without anything else while still defining the same class of languages).
The language defined above is not regular, but the language { a^{n}b^{m}  m,n > 0 } is, as it can be defined by the grammar G3 with N={S,A,B}, Σ={a,b}. S the start symbol, and the following production rules:
Yet to write
Search Encyclopedia

Featured Article
