Other meanings of Formal grammar
Computer science & linguistics
A formal grammar is a mathematical specification of how strings are generated from symbols by applying finite rules. It defines a formal language, whether that language describes valid program expressions, logical formulas, or patterns of natural-language structure.1
A formal grammar generates strings by repeatedly replacing symbols according to production rules. It is commonly written as G = (N, Σ, P, S): N is a finite set of nonterminals, Σ is a terminal alphabet, P is a finite set of productions, and S is the start symbol.1
For example, the rules S → aSb and S → ε generate strings containing equal numbers of as followed by bs, including the empty string. A derivation records the successive replacements, while the language L(G) is the set of terminal strings that can be derived from S. The grammar is therefore a generator and a precise description of structural well-formedness.
The Chomsky hierarchy classifies grammars by the restrictions placed on their productions and by the computational resources needed to recognize their languages.2 Regular grammars correspond to finite automata and describe relatively simple patterns; context-free grammars correspond to pushdown automata and can express nested constructions such as balanced parentheses. Context-sensitive grammars are associated with bounded linear-space computation, while unrestricted grammars characterize the recursively enumerable languages recognized by Turing machines.
Each successive class contains the preceding one in expressive power, although membership and parsing can become more difficult. The hierarchy is a classification of formal mechanisms, not a claim that every natural language belongs neatly to one level. In practical software, regular and context-free methods account for much lexical analysis and programming-language syntax.
Formal grammars support recognition and parsing: a recognizer tests whether a string belongs to a language, while a parser constructs a derivation or syntax tree explaining its structure. Compiler front ends commonly use regular expressions or finite automata for tokens and context-free grammars for expressions, statements, and nested blocks.3
A grammar is ambiguous when some string has more than one distinct parse tree. Arithmetic expressions illustrate the issue: without precedence and associativity rules, a string such as a + b × c may receive competing structures. Parser designers remove ambiguity, impose disambiguation conventions, or preserve multiple parses when uncertainty is meaningful. Grammars also appear in protocol specifications, markup languages, natural-language processing, and formal verification.
Formal grammars can be equivalent as language generators even when their rules look very different, and deciding equivalence is generally undecidable for unrestricted grammars.4 Some apparently minor choices also matter: allowing ε-productions affects empty-string behavior, and useless or unreachable symbols can be removed without changing the generated language.
Context-free grammars have several normal forms, including Chomsky normal form, that simplify theoretical proofs and algorithm design without changing the language except for controlled treatment of ε. More specialized systems extend ordinary productions with attributes, probabilities, or mildly context-sensitive mechanisms. These variants connect grammar theory with syntax-directed translation, statistical language processing, and descriptions of dependencies that ordinary context-free rules cannot capture efficiently.5
Notation and terminology follow standard formal-language and automata-theory usage.
Help improve the encyclopedia. Reports go straight to the site manager.