 | Formal grammar: Encyclopedia II - Formal grammar - Generative grammars
Formal grammar - 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.
2.
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: . The language of the grammar is the set of all the strings that can be generated using this process: .
Formal grammar - 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 Σ of terminal symbols that is disjoint from N.
- A finite set P of production rules where a rule is of the form
string in string in
(where * is the Kleene star and is union) with the restriction that the left-hand side of a rule (i.e., the part to the left of the ) must contain at least one nonterminal symbol.
- A symbol S in N that is indicated as the start symbol.
Usually such a formal grammar G is simply summarized as the quad-tuple (N,Σ,P,S).
The language of a formal grammar G = (N,Σ,P,S), denoted as , 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.
Formal grammar - Example
For these examples, formal languages are specified using set-builder notation.
Consider, for example, the grammar G with , , P consisting of the following production rules
1.
2.
3.
4.
and the nonterminal symbol S as the start symbol. Some examples of the derivation of strings in are:
(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 where an 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.
Formal grammar - The Chomsky Hierarchy
When Noam Chomsky first formalized generative grammars in the 1950s, 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.
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 (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 , , S the start symbol, and the following production rules:
1.
2.
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 (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 , , S the start symbol, and the following production rules:
1.
2.
3.
4.
5.
In practice, regular grammars are commonly expressed using regular expressions.
Aside from the differences in production rules required to generate the two languages, the key high-level difference between (context-free) and (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, just to make sure there are more than zero of each.
For more detail, see context-free language and regular language.
Formal grammar - 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: [1]
Other related archives1950s, 1970s, Abstract syntax tree, Ambiguous grammar, Chomsky hierarchy, Concrete syntax tree, Formal languages, Kleene star, LL parsers, LR parsers, Lindenmayer systems, Link grammars, Noam Chomsky, Parsing expression grammars, Top-down parsing language, Tree-adjoining grammars, Turing machine, abstract structure, algorithm, algorithms, alphabet, ambiguous, attribute grammars, automaton, boolean, compiler, computer science, context-free grammars, context-free language, context-free languages, easier to denote, finite, formal language, generative grammar, grammar, infinite, linguistics, parse, parse trees, parser, parsers, parsing, programming language, regular expressions, regular grammars, regular language, regular languages, rewriting, set, set-builder notation, strings, top-down parsers, union
 Adapted from the Wikipedia article "Generative grammars", under the G.N U Free Docmentation License. Please also see http://en.wikipedia.org/wiki |