Login
formal grammar
Introduction
A grammar, loosely speaking, is a set of rules that can be applied to words to generate sentences in a language. For example, with the grammar of the English language, one can form syntactically correct sentences such as ``The elephant drove his bicycle to the moon,'' regardless whether the sentence is meaningful or not.
The mathematical abstraction of a grammar is known as a formal grammar. Instead of generating sentences from words, a formal grammar generates words from symbols and other words. The following basic ingredients are necessary in a formal grammar:
- a collection of symbols called an alphabet,
- a collection of rules, called rewriting rules, specifying how one can generate new words from existing ones, and
- a collection of initial words that serve to initialize the generation of new words.
Note that by adding an extra symbol $\sigma$ to the above alphabet, and two additional rewriting rules given by ``from $\sigma$ form $a$ '' and ``from $\sigma$ form $b$ '', it is not hard to see that any word that can be generated by the first grammar can be generated by this new grammar.
Definition
Formalizing what we have discussed above, we say that a formal grammar $G$ is a quadruple $(\Sigma,N,P,\sigma)$ , where
- $(\Sigma,P)$ is a rewriting system;
- $N$ is a subset of $\Sigma$ whose elements are called non-terminals, and $T:=\Sigma-N$ the set of terminals;
- an element $\sigma\in N$ called the starting symbol.
A formal grammar is variously known as a phrase-structure grammar, an unrestricted grammar, or simply a grammar. A formal grammar is sometimes also called a rewriting system in the literature, although the two notions are distinct on PlanetMath.
Given a formal grammar $G$ , a word $W$ over $\Sigma$ such that $\sigma \derive W$ is called a sentential form of $G$ . A sentential form over $T$ is called a word generated by $G$ . The set of all words generated by $G$ is called the formal language generated by $G$ , and is denoted by $L(G)$ . In other words, $$L(G):=\lbrace w\in T^*\mid \sigma \derive w\rbrace,$$ where $\derive$ is the derivability relation in the rewriting system $(\Sigma,P)$ . A formal language is also known as a phrase-structure language.
A language $L$ over an alphabet $A$ is said to be generable by a formal grammar if there is a formal grammar $G$ such that $L=L(G)\cap A^*$ .
Example. Continuing from the example in the previous section, we can put $T=\lbrace a,b\rbrace$ and $N=\lbrace \sigma\rbrace$ . For the set $P$ of productions, we have four
- $\sigma \to \sigma a$
- $\sigma \to a\sigma$
- $\sigma \to a$
- $\sigma \to b$
Remarks.
- Not every language can be generated by a formal grammar. Given a finite alphabet $\Sigma$ , $\Sigma^*$ is countably infinite, and therefore there are uncountably many languages over $\Sigma$ . However, there are only a countably infinitely many languages that can be generated by formal grammars.
- Every language generated by a formal grammar is recursively enumerable.
- Every context-sensitive grammar is equivalent to a formal grammar, and under the Chomsky hierarchy, the class of formal languages is of class $0$ .
Bibliography
- 1
- H.R. Lewis, C.H. Papadimitriou Elements of the Theory of Computation. Prentice-Hall, Englewood Cliffs, New Jersey (1981).
