Formal grammar Guide, Meaning , Facts, Information and Description
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 yield 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.
| Table of contents |
|
2 Analytic grammars 3 See also |
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:
In the classic formalization of generative grammars first proposed by Noam Chomsky in the 1950s, a 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
It is clear that this grammar defines the language { anbncn | n > 0 } where an denotes a string of n a'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.
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 stricter 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 { anbn | 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 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 while still defining the same class of languages.)
The language defined above is not regular, but the language { anbm | 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:
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:
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:
This is an Article on Formal grammar. Page Contains Information, Facts Details or Explanation Guide About Formal grammar Generative Grammars
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 process 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.Formal definition
string in (Σ U N)* → string in (Σ U N)*
Usually such a formal grammar G is simply summarized as (N, Σ, P, S).Example
and the nonterminal symbol S as the start symbol. Some examples of the derivation of strings in L(G) are:
(where the used production rules are indicated in brackets and the replaced part is each time indicated in bold).The Chomsky Hierarchy
Context-free grammars
Regular grammars
Other forms of generative grammars
Analytic grammars
See also
