
| Part ofa series on |
| Formal languages |
|---|
Aformal grammar is a set ofsymbols and theproduction rules for rewriting some of them into every possible string of aformal language over analphabet. A grammar does not describe themeaning of the strings — only their form.
Inapplied mathematics, formal language theory is the discipline that studies formal grammars and languages. Its applications are found intheoretical computer science,theoretical linguistics,formal semantics,mathematical logic, and other areas.
A formal grammar is aset of rules for rewriting strings, along with a "start symbol" from which rewriting starts. Therefore, a grammar is usually thought of as a language generator. However, it can also sometimes be used as the basis for a "recognizer"—a function in computing that determines whether a given string belongs to the language or is grammatically incorrect. To describe such recognizers, formal language theory uses separate formalisms, known asautomata theory. One of the interesting results of automata theory is that it is not possible to design a recognizer for certain formal languages.[1]Parsing is the process of recognizing an utterance (a string in natural languages) by breaking it down to aset of symbols and analyzing each one against the grammar of the language. Most languages have the meanings of their utterances structured according to their syntax—a practice known ascompositional semantics. As a result, the first step to describing the meaning of an utterance in language is to break it down part by part and look at its analyzed form (known as itsparse tree in computer science, and as itsdeep structure ingenerative grammar).
A grammar mainly consists of a set ofproduction rules, rewrite rules for transforming strings. Each rule specifies a replacement of a particular string (itsleft-hand side) with another (itsright-hand side). A rule can be applied to each string that contains its left-hand side and produces a string in which an occurrence of that left-hand side has been replaced with its right-hand side.
Unlike asemi-Thue system, which is wholly defined by these rules, a grammar further distinguishes between two kinds of symbols:nonterminal andterminal symbols; each left-hand side must contain at least one nonterminal symbol. It also distinguishes a special nonterminal symbol, called thestart symbol.
The language generated by the grammar is defined to be the set of all strings without any nonterminal symbols that can be generated from the string consisting of a single start symbol by (possibly repeated) application of its rules in whatever way possible.If there are essentially different ways of generating the same single string, the grammar is said to beambiguous.
In the following examples, the terminal symbols area andb, and the start symbol isS.
Suppose we have the following production rules:
then we start withS, and can choose a rule to apply to it. If we choose rule 1, we obtain the stringaSb. If we then choose rule 1 again, we replaceS withaSb and obtain the stringaaSbb. If we now choose rule 2, we replaceS withba and obtain the stringaababb, and are done. We can write this series of choices more briefly, using symbols:.
The language of the grammar is the infinite set, where is repeated times (and in particular represents the number of times production rule 1 has been applied). This grammar iscontext-free (only single nonterminals appear as left-hand sides) and unambiguous.
Suppose the rules are these instead:
This grammar is not context-free due to rule 3 and it is ambiguous due to the multiple ways in which rule 2 can be used to generate sequences ofs.
However, the language it generates is simply the set of all nonempty strings consisting ofs and/ors.This is easy to see: to generate a from an, use rule 2 twice to generate, then rule 1 twice and rule 3 once to produce. This means we can generate arbitrary nonempty sequences ofs and then replace each of them with or as we please.
That same language can alternatively be generated by a context-free, nonambiguous grammar; for instance, theregular grammar with rules
In the classic formalization of generative grammars first proposed byNoam Chomsky in the 1950s,[2][3] a grammarG consists of the following components:
A grammar is formally defined as thetuple. Such a formal grammar is often called arewriting system or aphrase structure grammar in the literature.[5][6]
The operation of a grammar can be defined in terms of relations on strings:
The grammar is effectively thesemi-Thue system, rewriting strings in exactly the same way; the only difference is in that we distinguish specificnonterminal symbols, which must be replaced in rewrite rules, and are only interested in rewritings from the designated start symbol to strings without nonterminal symbols.
For these examples, formal languages are specified usingset-builder notation.
Consider the grammar where,, is the start symbol, and consists of the following production rules:
This grammar defines the language where denotes a string ofn 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.
Some examples of the derivation of strings in are:
WhenNoam Chomsky first formalized generative grammars in 1956,[2] he classified them into types now known as theChomsky hierarchy. The difference between these types is that they have increasingly strict production rules and can therefore express fewer formal languages. Two important types arecontext-free grammars (Type 2) andregular grammars (Type 3). The languages that can be described with such a grammar are calledcontext-free languages andregular languages, respectively. Although much less powerful thanunrestricted grammars (Type 0), which can in fact express any language that can be accepted by aTuring machine, these two restricted types of grammars are most often used because parsers for them can be efficiently implemented.[8] For example, all regular languages can be recognized by afinite-state machine, and for useful subsets of context-free grammars there are well-known algorithms to generate efficientLL parsers andLR parsers to recognize the corresponding languages those grammars generate.
Acontext-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 calledcontext-free languages.
The language defined above is not a context-free language, and this can be strictly proven using thepumping 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:
A context-free language can be recognized in time (seeBig O notation) by an algorithm such asEarley's recogniser. 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.[9]Deterministic context-free languages is a subset of context-free languages that can be recognized in linear time.[10] There exist various algorithms that target either this set of languages or some subset of it.
Inregular grammars, the left hand side is again only a single nonterminal symbol, but now the right-hand side is also restricted. The right side 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 languageseasier 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 time by a finite-state machine. Although in practice, regular grammars are commonly expressed usingregular 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.
Many extensions and variations on Chomsky's original hierarchy of formal grammars have been developed, 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:
A recursive grammar is a grammar that contains production rules that arerecursive. For example, a grammar for acontext-free language isleft-recursive if there exists a non-terminal symbolA that can be put through the production rules to produce a string withA as the leftmost symbol.[15] An example of recursive grammar is a clause within a sentence separated by two commas.[16] All types of grammars in theChomsky hierarchy can be recursive.
Though there is a tremendous body of literature onparsing algorithms, most of these algorithms assume that the language to be parsed is initiallydescribed by means of agenerative 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: