Information about Formal Grammar

In computer science and linguistics, a formal grammar, or sometimes simply grammar, is a precise description of a formal language — that is, of a set of strings over some alphabet. The two main categories of formal grammar are that of generative grammars, which are sets of rules for how strings in a language can be generated, and that of analytic grammars, which are sets of rules for how a string can be analyzed to determine whether it is a member of the language. In short, an analytic grammar describes how to recognize when strings are members in the set, whereas a generative grammar describes how to write only those strings in the set.

Generative grammars

Main article: generative grammar
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. 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 and , the start symbol is and we have the following rules:

1.
2.


then we start with , and can choose a rule to apply to it. If we choose rule 1, we replace with and obtain the string . If we choose rule 1 again, we replace with and obtain the string . This process is repeated until we only have symbols from the alphabet (i.e., and ). If we now choose rule 2, we replace with and obtain the string , 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 definition

In the classic formalization of generative grammars first proposed by Noam Chomsky in the 1950s,[1][2] a grammar G consists of the following components:
:
where is the Kleene star operator and denotes set union. That is, each production rule maps from one string of symbols to another, where the first string contains at least one nonterminal symbol. In the case that the second string is the empty string — that is, that it contains no symbols at all — an epsilon () is typically written in its place to avoid ambiguity.
  • A distinguished symbol that is the start symbol.
Usually such a formal grammar is simply summarized as the quad-tuple .

The language of a formal grammar , denoted as , is defined as all those strings over that can be generated by starting with the start symbol and then applying the production rules in until no more nonterminal symbols are present.

Example

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

Consider the grammar where , , is the start symbol, and consists of the following production rules:

1.
2.
3.
4.


Some examples of the derivation of strings in are:
(Note on notation: reads "L generates R by means of production i" and the generated part is each time indicated in bold.)


This grammar defines the language where denotes a string of n consecutive 's. Thus, the language is the set of strings that consist of 1 or more 's, followed by the same number of 's, followed by the same number of 's.

The Chomsky hierarchy

Main article: Chomsky hierarchy
When Noam Chomsky first formalized generative grammars in 1956,[1] he classified them into 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 (Type 2) and regular grammars (Type 3). 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 (Type 0), 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.[3] For example, all regular languages can be recognized by a finite state machine, and for useful subsets of context-free grammars there are well-known algorithms to generate efficient LL parsers and LR parsers to recognize the corresponding languages those grammars generate.

Context-free grammars

A context-free grammar is a grammar in which the left-hand side of each production rule consists of only a single nonterminal symbol. This restriction is non-trivial; not all languages can be generated by context-free grammars. Those that can are called context-free languages.

The language defined above is not a context-free language, and this can be strictly proven using the pumping lemma for context-free languages, but for example the language (at least 1 followed by the same number of 's) is context-free, as it can be defined by the grammar with , , the start symbol, and the following production rules:

1.
2.


A context-free language can be recognized in time (see Big O notation) by an algorithm such as Earley's algorithm. That is, for every context-free language, a machine can be built that takes a string as input and determines in time whether the string is a member of the language, where is the length of the string.[4] Further, some important subsets of the context-free languages can be recognized in linear time using other algorithms.

Regular grammars

In regular grammars, the left hand side is again only a single nonterminal symbol, but now the right-hand side is also restricted: It may be the empty string, 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, making languages easier to denote while still defining the same class of languages.)

The language defined above is not regular, but the language (at least 1 followed by at least 1 , where the numbers may be different) is, as it can be defined by the grammar with , , the start symbol, and the following production rules:

#
#
#
#
#


All languages generated by a regular grammar can be recognized in linear time by a finite state machine. Although, in practice, regular grammars are commonly expressed using regular expressions, some forms of regular expression used in practice do not strictly generate the regular languages and do not show linear recognitional performance due to those deviations.

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. Some forms of grammars 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.[5]
  • Affix grammars[6] and attribute grammars[7][8] allow rewrite rules to be augmented with semantic attributes and operations, useful both for increasing grammar expressiveness and for constructing practical language translation tools.

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. Strictly speaking, a generative grammar does not in any way correspond to the algorithm used to parse a language, and various algorithms have different restrictions on the form of production rules that are considered well-formed.

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 and semantics of a parser for the language. Examples of analytic grammar formalisms include the following:
  • The Language Machine [9] 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.[10]
  • Link grammars: a form of analytic grammar designed for linguistics, which derives syntactic structure by examining the positional relationships between pairs of words.[11][12]
  • Parsing expression grammars (PEGs): a more recent generalization of TDPL designed around the practical expressiveness needs of programming language and compiler writers.[13]

References

1. ^ Chomsky, Noam, "Three Models for the Description of Language," IRE Transactions on Information Theory, Vol. 2 No. 2, pp. 113-123, 1956.
2. ^ Chomsky, Noam, Syntactic Structures, Mouton, The Hague, 1957.
3. ^ Grune, Dick & Jacobs, Ceriel H., Parsing Techniques—A Practical Guide, Ellis Horwood, England, 1990.
4. ^ Earley, Jay, "An Efficient Context-Free Parsing Algorithm," Communications of the ACM, Vol. 13 No. 2, pp. 94-102, February 1970.
5. ^ Joshi, Aravind K., et al., "Tree Adjunct Grammars," Journal of Computer Systems Science, Vol. 10 No. 1, pp. 136-163, 1975.
6. ^ Koster , Cornelis H. A., "Affix Grammars," in ALGOL 68 Implementation, North Holland Publishing Company, Amsterdam, p. 95-109, 1971.
7. ^ Knuth, Donald E., "Semantics of Context-Free Languages," Mathematical Systems Theory, Vol. 2 No. 2, pp. 127-145, 1968.
8. ^ Knuth, Donald E., "Semantics of Context-Free Languages (correction)," Mathematical Systems Theory, Vol. 5 No. 1, pp 95-96, 1971.
9. ^ [1]
10. ^ Birman, Alexander, The TMG Recognition Schema, Doctoral thesis, Princeton University, Dept. of Electrical Engineering, February 1970.
11. ^ Sleator, Daniel D. & Temperly, Davy, "Parsing English with a Link Grammar," Technical Report CMU-CS-91-196, Carnegie Mellon University Computer Science, 1991.
12. ^ Sleator, Daniel D. & Temperly, Davy, "Parsing English with a Link Grammar," Third International Workshop on Parsing Technologies, 1993. (Revised version of above report.)
13. ^ Ford, Bryan, Packrat Parsing: a Practical Linear-Time Algorithm with Backtracking, Master’s thesis, Massachusetts Institute of Technology, Sept. 2002.

See also

External links

Automata theory: formal languages and formal grammars
Chomsky
hierarchy
Grammars Languages Minimal
automaton
Type-0UnrestrictedRecursively enumerableTuring machine
n/a(no common name)RecursiveDecider
Type-1Context-sensitiveContext-sensitiveLinear-bounded
n/aIndexedIndexedNested stack
n/aTree-adjoiningMildly context-sensitiveThread
Type-2Context-freeContext-freeNondeterministic pushdown
n/aDeterministic context-freeDeterministic context-freeDeterministic pushdown
Type-3RegularRegularFinite
Each category of languages or grammars is a proper subset of the category directly above it.
Computer science, or computing science, is the study of the theoretical foundations of information and computation and their implementation and application in computer systems.
..... Click the link for more information.
Linguistics is the scientific study of language, which can be theoretical or applied. Someone who engages in this study is called a linguist.
..... Click the link for more information.
This article is about the term formal language as it is used in mathematics, logic and computer science. For information about a mode of expression that is more disciplined or precise than everyday speech, see Register (linguistics).

..... Click the link for more information.
SET may stand for:
  • Sanlih Entertainment Television, a television channel in Taiwan
  • Secure electronic transaction, a protocol used for credit card processing,

..... Click the link for more information.
string is an ordered sequence of symbols. These symbols are chosen from a predetermined set.

In programming, when stored in memory each symbol is represented using a numeric value.
..... Click the link for more information.
In computer science, an alphabet is a finite set of characters or digits. The most common alphabet is {0,1}, the binary alphabet. A finite string is a finite sequence of characters from an alphabet; for instance a binary string is a string drawn from the alphabet {0,1}.
..... Click the link for more information.
In theoretical linguistics, generative grammar refers to a proof-theoretic framework for the study of syntax partially inspired by formal grammar theory and popularized by Noam Chomsky, and more specifically to particular instantiations of this general framework, that is,
..... Click the link for more information.
In computer science, a grammar is said to be an ambiguous grammar if there is some string that it can generate in more than one way (i.e., the string has more than one parse tree or more than one leftmost derivation).
..... Click the link for more information.
Avram Noam Chomsky (Hebrew: אברם נועם חומסקי Yiddish: אברם נועם כאמסקי) (born December 7, 1928) is an American
..... Click the link for more information.
worldwide view.


2nd millennium
Centuries: 19th century - 20th century - 21st century

1920s 1930s 1940s - 1950s - 1960s 1970s 1980s
1950 1951 1952 1953 1954
1955 1956 1957 1958 1959

- -
- The 1950s
..... Click the link for more information.
In computer science, terminal and nonterminal symbols are those symbols that are used to construct production rules in a formal grammar. Whereas terminal symbols form the parts of strings generated by the grammar, nonterminal
..... Click the link for more information.
In computer science, terminal and nonterminal symbols are those symbols that are used to construct production rules in a formal grammar. Whereas terminal symbols form the parts of strings generated by the grammar, nonterminal
..... Click the link for more information.
In mathematics, two sets are said to be disjoint if they have no element in common. For example, and are disjoint sets.

Explanation

Formally, two sets A and B are disjoint if their intersection is the empty set, i.e.
..... Click the link for more information.
In mathematical logic and computer science, the Kleene star (or Kleene closure) is a unary operation, either on sets of strings or on sets of symbols or characters. The application of the Kleene star to a set V is written as V*.
..... Click the link for more information.
In set theory and other branches of mathematics, the union of a collection of sets is the set that contains everything that belongs to any of the sets, but nothing else.

Basic definition

If A and B are sets, then the union of A and B
..... Click the link for more information.
In computer science, the empty string is the unique string of no characters over some alphabet Σ, and is denoted ε or λ. The length of this empty string is 0.
..... Click the link for more information.
In set theory and its applications to logic, mathematics, and computer science, set-builder notation (or commonly, "set notation") is a mathematical notation for describing a set by stating the properties that its members must satisfy.
..... Click the link for more information.
Within the field of computer science, specifically in the area of formal languages, the Chomsky hierarchy (occasionally referred to as Chomsky–Schützenberger hierarchy) is a containment hierarchy of classes of formal grammars.
..... Click the link for more information.
Avram Noam Chomsky (Hebrew: אברם נועם חומסקי Yiddish: אברם נועם כאמסקי) (born December 7, 1928) is an American
..... Click the link for more information.
19th century - 20th century - 21st century
1920s  1930s  1940s  - 1950s -  1960s  1970s  1980s
1953 1954 1955 - 1956 - 1957 1958 1959

Year 1956 (MCMLVI
..... Click the link for more information.
Within the field of computer science, specifically in the area of formal languages, the Chomsky hierarchy (occasionally referred to as Chomsky–Schützenberger hierarchy) is a containment hierarchy of classes of formal grammars.
..... Click the link for more information.
In formal language theory, a context-free grammar (CFG) is a grammar in which every production rule is of the form
Vw
where V is a single nonterminal symbol, and w
..... Click the link for more information.
In computer science a right regular grammar is a formal grammar (N, Σ, P, S) such that all the production rules in P are of one of the following forms:
  1. Aa - where A is a non-terminal in N

..... Click the link for more information.

..... Click the link for more information.
In theoretical computer science, a regular language is a formal language (i.e., a possibly infinite set of finite sequences of symbols from a finite alphabet) that satisfies the following equivalent properties:
  • it can be accepted by a deterministic finite state machine

..... Click the link for more information.
Turing machines are extremely basic abstract symbol-manipulating devices which, despite their simplicity, can be adapted to simulate the logic of any computer that could possibly be constructed. They were described in 1936 by Alan Turing.
..... Click the link for more information.
parsing (more formally syntactic analysis) is the process of analyzing a sequence of tokens to determine its grammatical structure with respect to a given formal grammar. A parser is the component of a compiler that carries out this task.
..... Click the link for more information.
finite state machine (FSM) or finite state automaton (plural: automata) or simply a state machine is a model of behavior composed of a finite number of states, transitions between those states, and actions.
..... Click the link for more information.
A LL parser is a top-down parser for a subset of the context-free grammars. It parses the input from Left to right, and constructs a Leftmost derivation of the sentence (hence LL, compare with LR parser).
..... Click the link for more information.
In computer science, an LR parser is a parser for context-free grammars that reads input from Left to right and produces a Rightmost derivation. The term LR(k) parser is also used; here the k
..... Click the link for more information.


This article is copied from an article on Wikipedia.org - the free encyclopedia created and edited by online user community. The text was not checked or edited by anyone on our staff. Although the vast majority of the wikipedia encyclopedia articles provide accurate and timely information please do not assume the accuracy of any particular article. This article is distributed under the terms of GNU Free Documentation License.
Herod_Archelaus


page counter