Formal grammar
In
computer science a
formal grammar is a way to describe a
formal language, i.e., a
set of finite-length
strings over a certain finite
alphabet. Formal grammars are so named by analogy to
grammar in human languages.
A formal 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 applies the rules (any number of times, in any order) to this string. The language consists of all the strings that can be generated in this manner.
For example, assume the alphabet consists of 'a' and 'b', the start symbol is 'S' and we have the following rules:
- 1. S → aSb\n: 2. S → ba
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.
Formal definition
A formal grammar G consists of the following components:\n* A finite set N of nonterminal symbols.\n* A finite set Σ of terminal symbols that is disjoint from N.\n* A finite set P of production rules where a rule is of the form\n:: string in (Σ U N)* → string in (Σ U N)*
- (where * is the Kleene star and U 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.\n* A symbol S in N that is indicated as the start symbol. \nUsually such a formal grammar G is simply summarized as (N, Σ, P, S).
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.
Example
Consider, for example, the grammar G with N = {S, B}, Σ = {a, b, c}, P consisting of the following production rules
- 1. S → aBSc\n: 2. S → abc\n: 3. Ba → aB\n: 4. Bb → bb
and the nonterminal symbol S as the start symbol. Some examples of the derivation of strings in L(G) are:
- S → (2) abc\n* S → (1) aBSc → (2) aBabcc → (3) aaBbcc → (4) aabbcc\n* S → (1) aBSc → (1) aBaBScc → (2) aBaBabccc → (3) aaBBabccc → (3) aaBaBbccc → (3) aaaBBbccc → (4) aaaBbbccc → (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 { anbncn | n > 0 } where an denotes a string of n a's.
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.
Classes of grammars
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 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 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 { 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:
- 1. S → aSb\n: 2. S → 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 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:
- 1. S → aA\n: 2. A → aA\n: 3. A → bB\n: 4. B → bB\n: 5. B → ε
See also
\n\nzh-cn:形式文法\nzh-tw:形式文法/繁
Category:Formal languages