In formal language theory, a grammar (when the context is not given, often called a formal grammar for clarity) is a set of production rules for strings in a formal language. The rules describe how to form strings from the language's alphabet that are valid according to the language's syntax. A grammar does not describe the meaning of the strings or what can be done with them in whatever context—only their form.
Formal language theory, the discipline which studies formal grammars and languages, is a branch of applied mathematics. Its applications are found in theoretical computer science, theoretical linguistics, formal semantics, mathematical logic, and other areas.
A formal grammar is a set 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 as automata 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 a set 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 as compositional 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 its parse tree in computer science, and as its deep structure in generative grammar).
Contents

Introductory example 1

Formal definition 2

The syntax of grammars 2.1

The semantics of grammars 2.2

Example 2.3

The Chomsky hierarchy 3

Contextfree grammars 3.1

Regular grammars 3.2

Other forms of generative grammars 3.3

Recursive grammars 3.4

Analytic grammars 4

See also 5

References 6

External links 7
Introductory example
A grammar mainly consists of a set of rules for transforming strings. (If it only consisted of these rules, it would be a semiThue system.) To generate a string in the language, one begins with a string consisting of only a single start symbol. The production rules are then applied in any order, until a string that contains neither the start symbol nor designated nonterminal symbols is produced. A production rule is applied to a string by replacing one occurrence of the production rule's lefthand side in the string by that production rule's righthand side (cf. the operation of the theoretical Turing machine). The language formed by the grammar consists of all distinct strings that can be generated in this manner. Any particular sequence of production rules on the start symbol yields a distinct string in the language. If there are essentially different ways of generating the same single string, 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 production rules:

1. S \rightarrow aSb

2. S \rightarrow ba
then we start with S, and can choose a rule to apply to it. If we choose rule 1, we obtain the string aSb. If we then choose rule 1 again, we replace S with aSb and obtain the string aaSbb. If we now choose rule 2, we replace S with ba and obtain the string aababb, and are done. We can write this series of choices more briefly, using symbols: S \Rightarrow aSb \Rightarrow aaSbb \Rightarrow aababb. The language of the grammar is then the infinite set \{a^nbab^n  n \ge 0 \} = \{ba, abab, aababb, aaababbb, \dotsc \}, where a^k is a repeated k times (and n in particular represents the number of times production rule 1 has been applied).
Formal definition
The syntax of grammars
In the classic formalization of generative grammars first proposed by Noam Chomsky in the 1950s,^{[2]}^{[3]} a grammar G consists of the following components:


(\Sigma \cup N)^{*} N (\Sigma \cup N)^{*} \rightarrow (\Sigma \cup N)^{*}

where {*} is the Kleene star operator and \cup denotes set union. That is, each production rule maps from one string of symbols to another, where the first string (the "head") contains an arbitrary number of symbols provided at least one of them is a nonterminal. In the case that the second string (the "body") consists solely of the empty string – i.e., that it contains no symbols at all – it may be denoted with a special notation (often \Lambda, e or \epsilon) in order to avoid confusion.

A distinguished symbol S \in N that is the start symbol, also called the sentence symbol.
A grammar is formally defined as the tuple (N, \Sigma, P, S). Such a formal grammar is often called a rewriting system or a phrase structure grammar in the literature.^{[4]}^{[5]}
The semantics of grammars
The operation of a grammar can be defined in terms of relations on strings:

Given a grammar G = (N, \Sigma, P, S), the binary relation \Rightarrow_G (pronounced as "G derives in one step") on strings in (\Sigma \cup N)^{*} is defined by:
x \Rightarrow_G y \mbox{ iff } \exists u, v, p, q \in (\Sigma \cup N)^*: (x = upv) \wedge (p \rightarrow q \in P) \wedge (y = uqv)

the relation {\Rightarrow_G}^* (pronounced as G derives in zero or more steps) is defined as the reflexive transitive closure of \Rightarrow_G

a sentential form is a member of (\Sigma \cup N)^* that can be derived in a finite number of steps from the start symbol S; that is, a sentential form is a member of \{ w \in (\Sigma \cup N)^* \mid S {\Rightarrow_G}^* w \}. A sentential form that contains no nonterminal symbols (i.e. is a member of \Sigma^*) is called a sentence.^{[6]}

the language of G, denoted as \boldsymbol{L}(G), is defined as all those sentences that can be derived in a finite number of steps from the start symbol S; that is, the set \{ w \in \Sigma^* \mid S {\Rightarrow_G}^* w \}.
Note that the grammar G = (N, \Sigma, P, S) is effectively the semiThue system (N \cup \Sigma, P), rewriting strings in exactly the same way; the only difference is in that we distinguish specific nonterminal symbols which must be rewritten in rewrite rules, and are only interested in rewritings from the designated start symbol S to strings without nonterminal symbols.
Example
For these examples, formal languages are specified using setbuilder notation.
Consider the grammar G where N = \left \{S, B\right \}, \Sigma = \left \{a, b, c\right \}, S is the start symbol, and P consists of the following production rules:

1. S \rightarrow aBSc

2. S \rightarrow abc

3. Ba \rightarrow aB

4. Bb \rightarrow bb
This grammar defines the language L(G) = \left \{ a^{n}b^{n}c^{n}  n \ge 1 \right \} where a^{n} denotes a string of n consecutive a's. Thus, the language is the set of strings that consist of 1 or more a's, followed by the same number of b's, followed by the same number of c's.
Some examples of the derivation of strings in L(G) are:

\boldsymbol{S} \Rightarrow_2 \boldsymbol{abc}

\boldsymbol{S} \Rightarrow_1 \boldsymbol{aBSc} \Rightarrow_2 aB\boldsymbol{abc}c \Rightarrow_3 a\boldsymbol{aB}bcc \Rightarrow_4 aa\boldsymbol{bb}cc

\boldsymbol{S} \Rightarrow_1 \boldsymbol{aBSc} \Rightarrow_1 aB\boldsymbol{aBSc}c \Rightarrow_2 aBaB\boldsymbol{abc}cc \Rightarrow_3 a\boldsymbol{aB}Babccc \Rightarrow_3 aaB\boldsymbol{aB}bccc \Rightarrow_3 aa\boldsymbol{aB}Bbccc \Rightarrow_4 aaaB\boldsymbol{bb}ccc \Rightarrow_4 aaa\boldsymbol{bb}bccc

(Note on notation: P \Rightarrow_i Q reads "String P generates string Q by means of production i", and the generated part is each time indicated in bold type.)
The Chomsky hierarchy
When Noam Chomsky first formalized generative grammars in 1956,^{[2]} 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 contextfree grammars (Type 2) and regular grammars (Type 3). The languages that can be described with such a grammar are called contextfree 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.^{[7]} For example, all regular languages can be recognized by a finite state machine, and for useful subsets of contextfree grammars there are wellknown algorithms to generate efficient LL parsers and LR parsers to recognize the corresponding languages those grammars generate.
Contextfree grammars
A contextfree grammar is a grammar in which the lefthand side of each production rule consists of only a single nonterminal symbol. This restriction is nontrivial; not all languages can be generated by contextfree grammars. Those that can are called contextfree languages.
The language L(G) = \left \{ a^{n}b^{n}c^{n}  n \ge 1 \right \} defined above is not a contextfree language, and this can be strictly proven using the pumping lemma for contextfree languages, but for example the language \left \{ a^{n}b^{n}  n \ge 1 \right \} (at least 1 a followed by the same number of b's) is contextfree, as it can be defined by the grammar G_2 with N=\left \{S\right \}, \Sigma=\left \{a,b\right \}, S the start symbol, and the following production rules:

1. S \rightarrow aSb

2. S \rightarrow ab
A contextfree language can be recognized in O(n^3) time (see Big O notation) by an algorithm such as Earley's algorithm. That is, for every contextfree language, a machine can be built that takes a string as input and determines in O(n^3) time whether the string is a member of the language, where n is the length of the string.^{[8]} Deterministic contextfree languages is a subset of contextfree languages that can be recognized in linear time.^{[9]} There exist various algorithms that target either this set of languages or some subset of it.
Regular grammars
In regular grammars, the left hand side is again only a single nonterminal symbol, but now the righthand 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 languages easier to denote while still defining the same class of languages.)
The language \left \{ a^{n}b^{n}  n \ge 1 \right \} defined above is not regular, but the language \left \{ a^{n}b^{m} \, \, m,n \ge 1 \right \} (at least 1 a followed by at least 1 b, where the numbers may be different) is, as it can be defined by the grammar G_3 with N=\left \{S, A,B\right \}, \Sigma=\left \{a,b\right \}, S the start symbol, and the following production rules:


S \rightarrow aA

A \rightarrow aA

A \rightarrow bB

B \rightarrow bB

B \rightarrow \epsilon
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, 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:

Treeadjoining grammars increase the expressiveness of conventional generative grammars by allowing rewrite rules to operate on parse trees instead of just strings.^{[10]}

Affix grammars^{[11]} and attribute grammars^{[12]}^{[13]} allow rewrite rules to be augmented with semantic attributes and operations, useful both for increasing grammar expressiveness and for constructing practical language translation tools.
Recursive grammars
A recursive grammar is a grammar which contains production rules that are recursive. For example, a grammar for a contextfree language is leftrecursive if there exists a nonterminal symbol A that can be put through the production rules to produce a string with A as the leftmost symbol.^{[14]} All types of grammars in the Chomsky hierarchy can be recursive.
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 wellformed.
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 directly implements unrestricted analytic grammars. Substitution rules are used to transform an input to produce outputs and behaviour. The system can also produce the lmdiagram which shows what happens when the rules of an unrestricted analytic grammar are being applied.

Topdown parsing language (TDPL): a highly minimalist analytic grammar formalism developed in the early 1970s to study the behavior of topdown parsers.^{[15]}

Link grammars: a form of analytic grammar designed for linguistics, which derives syntactic structure by examining the positional relationships between pairs of words.^{[16]}^{[17]}

Parsing expression grammars (PEGs): a more recent generalization of TDPL designed around the practical expressiveness needs of programming language and compiler writers.^{[18]}
See also
References

^ . For more on this subject, see undecidable problem.

^ ^{a} ^{b}

^

^

^

^ Sentential Forms, ContextFree Grammars, David Matuszek

^ Grune, Dick & Jacobs, Ceriel H., Parsing Techniques – A Practical Guide, Ellis Horwood, England, 1990.

^ Earley, Jay, "An Efficient ContextFree Parsing Algorithm," Communications of the ACM, Vol. 13 No. 2, pp. 94102, February 1970.

^

^ Joshi, Aravind K., et al., "Tree Adjunct Grammars," Journal of Computer Systems Science, Vol. 10 No. 1, pp. 136163, 1975.

^ Koster , Cornelis H. A., "Affix Grammars," in ALGOL 68 Implementation, North Holland Publishing Company, Amsterdam, p. 95109, 1971.

^ Knuth, Donald E., "Semantics of ContextFree Languages," Mathematical Systems Theory, Vol. 2 No. 2, pp. 127145, 1968.

^ Knuth, Donald E., "Semantics of ContextFree Languages (correction)," Mathematical Systems Theory, Vol. 5 No. 1, pp 9596, 1971.

^ Notes on Formal Language Theory and Parsing, James Power, Department of Computer Science National University of Ireland, Maynooth Maynooth, Co. Kildare, Ireland.JPR02

^ Birman, Alexander, The TMG Recognition Schema, Doctoral thesis, Princeton University, Dept. of Electrical Engineering, February 1970.

^ Sleator, Daniel D. & Temperly, Davy, "Parsing English with a Link Grammar," Technical Report CMUCS91196, Carnegie Mellon University Computer Science, 1991.

^ Sleator, Daniel D. & Temperly, Davy, "Parsing English with a Link Grammar," Third International Workshop on Parsing Technologies, 1993. (Revised version of above report.)

^ Ford, Bryan, Packrat Parsing: a Practical LinearTime Algorithm with Backtracking, Master’s thesis, Massachusetts Institute of Technology, Sept. 2002.
External links

Yearly Formal Grammar conference




Each category of languages, except those marked by a ^{*}, is a proper subset of the category directly above it. Any language in each category is generated by a grammar and by an automaton in the category in the same line.


This article was sourced from Creative Commons AttributionShareAlike License; additional terms may apply. World Heritage Encyclopedia content is assembled from numerous content providers, Open Access Publishing, and in compliance with The Fair Access to Science and Technology Research Act (FASTR), Wikimedia Foundation, Inc., Public Library of Science, The Encyclopedia of Life, Open Book Publishers (OBP), PubMed, U.S. National Library of Medicine, National Center for Biotechnology Information, U.S. National Library of Medicine, National Institutes of Health (NIH), U.S. Department of Health & Human Services, and USA.gov, which sources content from all federal, state, local, tribal, and territorial government publication portals (.gov, .mil, .edu). Funding for USA.gov and content contributors is made possible from the U.S. Congress, EGovernment Act of 2002.
Crowd sourced content that is contributed to World Heritage Encyclopedia is peer reviewed and edited by our editorial staff to ensure quality scholarly research articles.
By using this site, you agree to the Terms of Use and Privacy Policy. World Heritage Encyclopedia™ is a registered trademark of the World Public Library Association, a nonprofit organization.