![Formal grammar](https://www.english.nina.az/wikipedia/image/aHR0cHM6Ly91cGxvYWQud2lraW1lZGlhLm9yZy93aWtpcGVkaWEvY29tbW9ucy90aHVtYi8xLzFhL1Rlcm1pbmFsX2FuZF9ub24tdGVybWluYWxfc3ltYm9sc19leGFtcGxlLnBuZy8xNjAwcHgtVGVybWluYWxfYW5kX25vbi10ZXJtaW5hbF9zeW1ib2xzX2V4YW1wbGUucG5n.png )
A formal grammar describes which strings from an alphabet of a formal language 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. A formal grammar is defined as a set of production rules for such strings in a formal language.
![image](https://www.english.nina.az/wikipedia/image/aHR0cHM6Ly93d3cuZW5nbGlzaC5uaW5hLmF6L3dpa2lwZWRpYS9pbWFnZS9hSFIwY0hNNkx5OTFjR3h2WVdRdWQybHJhVzFsWkdsaExtOXlaeTkzYVd0cGNHVmthV0V2WTI5dGJXOXVjeTkwYUhWdFlpOHhMekZoTDFSbGNtMXBibUZzWDJGdVpGOXViMjR0ZEdWeWJXbHVZV3hmYzNsdFltOXNjMTlsZUdGdGNHeGxMbkJ1Wnk4MU1EQndlQzFVWlhKdGFXNWhiRjloYm1SZmJtOXVMWFJsY20xcGJtRnNYM041YldKdmJITmZaWGhoYlhCc1pTNXdibWM9LnBuZw==.png)
Formal language theory, the discipline that 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.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).
Introductory example
A grammar mainly consists of a set of production rules, rewriting rules for transforming strings. Each rule specifies a replacement of a particular string (its left-hand side) with another (its right-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 a semi-Thue system, which is wholly defined by these rules, a grammar further distinguishes between two kinds of symbols: nonterminal and terminal symbols; each left-hand side must contain at least one nonterminal symbol. It also distinguishes a special nonterminal symbol, called the start 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 be ambiguous.
In the following examples, the terminal symbols are a and b, and the start symbol is S.
Example 1
Suppose we have the following production rules:
- 1.
- 2.
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: .
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 is context-free (only single nonterminals appear as left-hand sides) and unambiguous.
Examples 2 and 3
Suppose the rules are these instead:
- 1.
- 2.
- 3.
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 of s.
However, the language it generates is simply the set of all nonempty strings consisting of s and/or
s. 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 of
s 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, the regular grammar with rules
- 1.
- 2.
- 3.
- 4.
Formal definition
The syntax of grammars
In the classic formalization of generative grammars first proposed by Noam Chomsky in the 1950s, a grammar G consists of the following components:
- A finite set N of nonterminal symbols, that is disjoint with the strings formed from G.
- A finite set
of terminal symbols that is disjoint from N.
- A finite set P of production rules, each rule of the form
- 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 (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
, e or
) in order to avoid confusion. Such a rule is called an erasing rule.
- A distinguished symbol
that is the start symbol, also called the sentence symbol.
A grammar is formally defined as the tuple . Such a formal grammar is often called a rewriting system or a phrase structure grammar in the literature.
Some mathematical constructs regarding formal grammars
The operation of a grammar can be defined in terms of relations on strings:
- Given a grammar
, the binary relation
(pronounced as "G derives in one step") on strings in
is defined by:
- the relation
(pronounced as G derives in zero or more steps) is defined as the reflexive transitive closure of
- a sentential form is a member of
that can be derived in a finite number of steps from the start symbol
; that is, a sentential form is a member of
. A sentential form that contains no nonterminal symbols (i.e. is a member of
) is called a sentence.
- the language of
, denoted as
, is defined as the set of sentences built by
.
The grammar is effectively the semi-Thue system
, 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
to strings without nonterminal symbols.
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.
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.
Some examples of the derivation of strings in are:
- (On notation:
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, 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 therefore 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. 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 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.Deterministic context-free languages is a subset of context-free languages that can be recognized in linear time. 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 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 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 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:
- Tree-adjoining grammars increase the expressiveness of conventional generative grammars by allowing rewrite rules to operate on parse trees instead of just strings.
- Affix grammars and attribute grammars 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 that contains production rules that are recursive. For example, a grammar for a context-free language is left-recursive if there exists a non-terminal symbol A that can be put through the production rules to produce a string with A as the leftmost symbol. An example of recursive grammar is a clause within a sentence separated by two commas. 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 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:
- Top-down parsing language (TDPL): a highly minimalist analytic grammar formalism developed in the early 1970s to study the behavior of top-down parsers.
- Link grammars: a form of analytic grammar designed for linguistics, which derives syntactic structure by examining the positional relationships between pairs of words.
- Parsing expression grammars (PEGs): a more recent generalization of TDPL designed around the practical expressiveness needs of programming language and compiler writers.
See also
- Abstract syntax tree
- Adaptive grammar
- Ambiguous grammar
- Backus–Naur form (BNF)
- Categorial grammar
- Concrete syntax tree
- Extended Backus–Naur form (EBNF)
- Grammar
- Grammar framework
- L-system
- Lojban
- Post canonical system
- Shape grammar
- Well-formed formula
References
- Meduna, Alexander (2014), Formal Languages and Computation: Models and Their Applications, CRC Press, p. 233, ISBN 9781466513457. For more on this subject, see undecidable problem.
- Chomsky, Noam (Sep 1956). "Three models for the description of language". IRE Transactions on Information Theory. 2 (3): 113–124. doi:10.1109/TIT.1956.1056813. S2CID 19519474.
- Chomsky, Noam (1957). Syntactic Structures. The Hague: Mouton.
- Ashaari, S.; Turaev, S.; Okhunov, A. (2016). "Structurally and Arithmetically Controlled Grammars" (PDF). International Journal on Perceptive and Cognitive Computing. 2 (2): 27. doi:10.31436/ijpcc.v2i2.39. Retrieved 2024-11-05.
- Ginsburg, Seymour (1975). Algebraic and automata theoretic properties of formal languages. North-Holland. pp. 8–9. ISBN 978-0-7204-2506-2.
- Harrison, Michael A. (1978). Introduction to Formal Language Theory. Reading, Mass.: Addison-Wesley Publishing Company. p. 13. ISBN 978-0-201-02955-0.
- Sentential Forms Archived 2019-11-13 at the Wayback Machine, Context-Free Grammars, David Matuszek
- Grune, Dick & Jacobs, Ceriel H., Parsing Techniques – A Practical Guide, Ellis Horwood, England, 1990.
- Earley, Jay, "An Efficient Context-Free Parsing Algorithm Archived 2020-05-19 at the Wayback Machine," Communications of the ACM, Vol. 13 No. 2, pp. 94-102, February 1970.
- Knuth, D. E. (July 1965). "On the translation of languages from left to right". Information and Control. 8 (6): 607–639. doi:10.1016/S0019-9958(65)90426-2.
- Joshi, Aravind K., et al., "Tree Adjunct Grammars," Journal of Computer Systems Science, Vol. 10 No. 1, pp. 136-163, 1975.
- Koster , Cornelis H. A., "Affix Grammars," in ALGOL 68 Implementation, North Holland Publishing Company, Amsterdam, p. 95-109, 1971.
- Knuth, Donald E., "Semantics of Context-Free Languages," Mathematical Systems Theory, Vol. 2 No. 2, pp. 127-145, 1968.
- Knuth, Donald E., "Semantics of Context-Free Languages (correction)," Mathematical Systems Theory, Vol. 5 No. 1, pp 95-96, 1971.
- Notes on Formal Language Theory and Parsing Archived 2017-08-28 at the Wayback Machine, James Power, Department of Computer Science National University of Ireland, Maynooth Maynooth, Co. Kildare, Ireland.
- Borenstein, Seth (April 27, 2006). "Songbirds grasp grammar, too". Northwest Herald. p. 2 – via Newspapers.com.
- 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 CMU-CS-91-196, 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 Linear-Time Algorithm with Backtracking, Master’s thesis, Massachusetts Institute of Technology, Sept. 2002.
A formal grammar describes which strings from an alphabet of a formal language 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 A formal grammar is defined as a set of production rules for such strings in a formal language An example of a formal grammar with parsed sentence Formal grammars consist of a set of non terminal symbols terminal symbols production rules and a designated start symbol Formal language theory the discipline that 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 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 Introductory exampleA grammar mainly consists of a set of production rules rewriting rules for transforming strings Each rule specifies a replacement of a particular string its left hand side with another its right 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 a semi Thue system which is wholly defined by these rules a grammar further distinguishes between two kinds of symbols nonterminal and terminal symbols each left hand side must contain at least one nonterminal symbol It also distinguishes a special nonterminal symbol called the start 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 be ambiguous In the following examples the terminal symbols are a and b and the start symbol is S Example 1 Suppose we have the following production rules 1 S aSb displaystyle S rightarrow aSb 2 S ba displaystyle 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 aSb aaSbb aababb displaystyle S Rightarrow aSb Rightarrow aaSbb Rightarrow aababb The language of the grammar is the infinite set anbabn n 0 ba abab aababb aaababbb displaystyle a n bab n mid n geq 0 ba abab aababb aaababbb dotsc where ak displaystyle a k is a displaystyle a repeated k displaystyle k times and n displaystyle n in particular represents the number of times production rule 1 has been applied This grammar is context free only single nonterminals appear as left hand sides and unambiguous Examples 2 and 3 Suppose the rules are these instead 1 S a displaystyle S rightarrow a 2 S SS displaystyle S rightarrow SS 3 aSa b displaystyle aSa rightarrow b 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 of S displaystyle S s However the language it generates is simply the set of all nonempty strings consisting of a displaystyle a s and or b displaystyle b s This is easy to see to generate a b displaystyle b from an S displaystyle S use rule 2 twice to generate SSS displaystyle SSS then rule 1 twice and rule 3 once to produce b displaystyle b This means we can generate arbitrary nonempty sequences of S displaystyle S s and then replace each of them with a displaystyle a or b displaystyle b as we please That same language can alternatively be generated by a context free nonambiguous grammar for instance the regular grammar with rules 1 S aS displaystyle S rightarrow aS 2 S bS displaystyle S rightarrow bS 3 S a displaystyle S rightarrow a 4 S b displaystyle S rightarrow b Formal definitionThe syntax of grammars In the classic formalization of generative grammars first proposed by Noam Chomsky in the 1950s a grammar G consists of the following components A finite set N of nonterminal symbols that is disjoint with the strings formed from G A finite set S displaystyle Sigma of terminal symbols that is disjoint from N A finite set P of production rules each rule of the form S N N S N S N displaystyle Sigma cup N N Sigma cup N rightarrow Sigma cup N dd where displaystyle is the Kleene star operator and displaystyle 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 L displaystyle Lambda e or ϵ displaystyle epsilon in order to avoid confusion Such a rule is called an erasing rule A distinguished symbol S N displaystyle S in N that is the start symbol also called the sentence symbol A grammar is formally defined as the tuple N S P S displaystyle N Sigma P S Such a formal grammar is often called a rewriting system or a phrase structure grammar in the literature Some mathematical constructs regarding formal grammars The operation of a grammar can be defined in terms of relations on strings Given a grammar G N S P S displaystyle G N Sigma P S the binary relation G displaystyle underset G Rightarrow pronounced as G derives in one step on strings in S N displaystyle Sigma cup N is defined by x Gy u v p q S N x upv p q P y uqv displaystyle x underset G Rightarrow y iff exists u v p q in Sigma cup N x upv wedge p rightarrow q in P wedge y uqv the relation G displaystyle overset underset G Rightarrow pronounced as G derives in zero or more steps is defined as the reflexive transitive closure of G displaystyle underset G Rightarrow a sentential form is a member of S N displaystyle Sigma cup N that can be derived in a finite number of steps from the start symbol S displaystyle S that is a sentential form is a member of w S N S G w displaystyle left w in Sigma cup N mid S overset underset G Rightarrow w right A sentential form that contains no nonterminal symbols i e is a member of S displaystyle Sigma is called a sentence the language of G displaystyle G denoted as L G displaystyle boldsymbol L G is defined as the set of sentences built by G displaystyle G The grammar G N S P S displaystyle G N Sigma P S is effectively the semi Thue system N S P displaystyle 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 displaystyle S to strings without nonterminal symbols Example For these examples formal languages are specified using set builder notation Consider the grammar G displaystyle G where N S B displaystyle N left S B right S a b c displaystyle Sigma left a b c right S displaystyle S is the start symbol and P displaystyle P consists of the following production rules 1 S aBSc displaystyle S rightarrow aBSc 2 S abc displaystyle S rightarrow abc 3 Ba aB displaystyle Ba rightarrow aB 4 Bb bb displaystyle Bb rightarrow bb This grammar defines the language L G anbncn n 1 displaystyle L G left a n b n c n mid n geq 1 right where an displaystyle a n denotes a string of n consecutive a displaystyle a s Thus the language is the set of strings that consist of 1 or more a displaystyle a s followed by the same number of b displaystyle b s followed by the same number of c displaystyle c s Some examples of the derivation of strings in L G displaystyle L G are S 2abc displaystyle boldsymbol S underset 2 Rightarrow boldsymbol abc S 1aBSc 2aBabcc 3aaBbcc 4aabbcc displaystyle begin aligned boldsymbol S amp underset 1 Rightarrow boldsymbol aBSc amp underset 2 Rightarrow aB boldsymbol abc c amp underset 3 Rightarrow a boldsymbol aB bcc amp underset 4 Rightarrow aa boldsymbol bb cc end aligned S 1aBSc 1aBaBScc 2aBaBabccc 3aaBBabccc 3aaBaBbccc 3aaaBBbccc 4aaaBbbccc 4aaabbbccc displaystyle begin aligned boldsymbol S amp underset 1 Rightarrow boldsymbol aBSc underset 1 Rightarrow aB boldsymbol aBSc c amp underset 2 Rightarrow aBaB boldsymbol abc cc amp underset 3 Rightarrow a boldsymbol aB Babccc underset 3 Rightarrow aaB boldsymbol aB bccc underset 3 Rightarrow aa boldsymbol aB Bbccc amp underset 4 Rightarrow aaaB boldsymbol bb ccc underset 4 Rightarrow aaa boldsymbol bb bccc end aligned On notation P iQ displaystyle P underset i Rightarrow 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 hierarchyWhen Noam Chomsky first formalized generative grammars in 1956 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 therefore 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 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 L G anbncn n 1 displaystyle L G left a n b n c n mid n geq 1 right 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 anbn n 1 displaystyle left a n b n mid n geq 1 right at least 1 a displaystyle a followed by the same number of b displaystyle b s is context free as it can be defined by the grammar G2 displaystyle G 2 with N S displaystyle N left S right S a b displaystyle Sigma left a b right S displaystyle S the start symbol and the following production rules 1 S aSb displaystyle S rightarrow aSb 2 S ab displaystyle S rightarrow ab A context free language can be recognized in O n3 displaystyle O n 3 time see Big O notation by an algorithm such as Earley s recogniser That is for every context free language a machine can be built that takes a string as input and determines in O n3 displaystyle O n 3 time whether the string is a member of the language where n displaystyle n is the length of the string Deterministic context free languages is a subset of context free languages that can be recognized in linear time 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 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 languages easier to denote while still defining the same class of languages The language anbn n 1 displaystyle left a n b n mid n geq 1 right defined above is not regular but the language anbm m n 1 displaystyle left a n b m mid m n geq 1 right at least 1 a displaystyle a followed by at least 1 b displaystyle b where the numbers may be different is as it can be defined by the grammar G3 displaystyle G 3 with N S A B displaystyle N left S A B right S a b displaystyle Sigma left a b right S displaystyle S the start symbol and the following production rules S aA displaystyle S rightarrow aA A aA displaystyle A rightarrow aA A bB displaystyle A rightarrow bB B bB displaystyle B rightarrow bB B ϵ displaystyle B rightarrow epsilon All languages generated by a regular grammar can be recognized in O n displaystyle O n 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 Tree adjoining grammars increase the expressiveness of conventional generative grammars by allowing rewrite rules to operate on parse trees instead of just strings Affix grammars and attribute grammars 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 that contains production rules that are recursive For example a grammar for a context free language is left recursive if there exists a non terminal symbol A that can be put through the production rules to produce a string with A as the leftmost symbol An example of recursive grammar is a clause within a sentence separated by two commas All types of grammars in the Chomsky hierarchy can be recursive Analytic grammarsThough 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 Top down parsing language TDPL a highly minimalist analytic grammar formalism developed in the early 1970s to study the behavior of top down parsers Link grammars a form of analytic grammar designed for linguistics which derives syntactic structure by examining the positional relationships between pairs of words Parsing expression grammars PEGs a more recent generalization of TDPL designed around the practical expressiveness needs of programming language and compiler writers See alsoAbstract syntax tree Adaptive grammar Ambiguous grammar Backus Naur form BNF Categorial grammar Concrete syntax tree Extended Backus Naur form EBNF Grammar Grammar framework L system Lojban Post canonical system Shape grammar Well formed formulaReferencesMeduna Alexander 2014 Formal Languages and Computation Models and Their Applications CRC Press p 233 ISBN 9781466513457 For more on this subject see undecidable problem Chomsky Noam Sep 1956 Three models for the description of language IRE Transactions on Information Theory 2 3 113 124 doi 10 1109 TIT 1956 1056813 S2CID 19519474 Chomsky Noam 1957 Syntactic Structures The Hague Mouton Ashaari S Turaev S Okhunov A 2016 Structurally and Arithmetically Controlled Grammars PDF International Journal on Perceptive and Cognitive Computing 2 2 27 doi 10 31436 ijpcc v2i2 39 Retrieved 2024 11 05 Ginsburg Seymour 1975 Algebraic and automata theoretic properties of formal languages North Holland pp 8 9 ISBN 978 0 7204 2506 2 Harrison Michael A 1978 Introduction to Formal Language Theory Reading Mass Addison Wesley Publishing Company p 13 ISBN 978 0 201 02955 0 Sentential Forms Archived 2019 11 13 at the Wayback Machine Context Free Grammars David Matuszek Grune Dick amp Jacobs Ceriel H Parsing Techniques A Practical Guide Ellis Horwood England 1990 Earley Jay An Efficient Context Free Parsing Algorithm Archived 2020 05 19 at the Wayback Machine Communications of the ACM Vol 13 No 2 pp 94 102 February 1970 Knuth D E July 1965 On the translation of languages from left to right Information and Control 8 6 607 639 doi 10 1016 S0019 9958 65 90426 2 Joshi Aravind K et al Tree Adjunct Grammars Journal of Computer Systems Science Vol 10 No 1 pp 136 163 1975 Koster Cornelis H A Affix Grammars in ALGOL 68 Implementation North Holland Publishing Company Amsterdam p 95 109 1971 Knuth Donald E Semantics of Context Free Languages Mathematical Systems Theory Vol 2 No 2 pp 127 145 1968 Knuth Donald E Semantics of Context Free Languages correction Mathematical Systems Theory Vol 5 No 1 pp 95 96 1971 Notes on Formal Language Theory and Parsing Archived 2017 08 28 at the Wayback Machine James Power Department of Computer Science National University of Ireland Maynooth Maynooth Co Kildare Ireland Borenstein Seth April 27 2006 Songbirds grasp grammar too Northwest Herald p 2 via Newspapers com Birman Alexander The TMG Recognition Schema Doctoral thesis Princeton University Dept of Electrical Engineering February 1970 Sleator Daniel D amp Temperly Davy Parsing English with a Link Grammar Technical Report CMU CS 91 196 Carnegie Mellon University Computer Science 1991 Sleator Daniel D amp 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 Linear Time Algorithm with Backtracking Master s thesis Massachusetts Institute of Technology Sept 2002