Backus-Naur Form
Backus-Naur Form and its extended variant as notations for specifying the syntax of programming languages.
Specifying the syntax of a programming language calls for a notation that is mathematically precise and also of practical use in generating parsers. Backus-Naur Form (BNF), introduced by John Backus in 1959 and adapted by Peter Naur for the ALGOL 60 report, is a notation for context-free grammars that meets both requirements, and it has become the standard way of describing syntax.
Notation
A BNF grammar is written with a small set of symbols:
::=means “is defined as”|means “or” (alternative productions)<symbol>denotes a non-terminal (to be further defined)- Literal text appears without brackets
A complete grammar consists of production rules, which define the structure of the language recursively. A grammar for simple arithmetic expressions:
Operator precedence is captured by the hierarchy of the rules: multiplication and division bind more tightly than addition and subtraction, and parentheses override both.
Statements
The same notation scales to the definition of whole languages. A simplified assignment statement might be specified as follows:
The recursive definition of permits names of any length, and the alternatives in admit different kinds of statement within the same syntactic category.
Extended BNF
Extended BNF (EBNF) adds operators that cut down the verbosity:
[ ]denotes optional elements{ }denotes zero or more repetitions( )groups alternatives+denotes one or more repetitions
In EBNF the arithmetic grammar becomes:
Standard ML
The Standard ML definition is an exemplary use of BNF in the specification of a whole language. Its grammar covers pattern matching, type expressions and the functional constructs with mathematical precision:
The excerpt shows BNF capturing both concrete syntax (val, fun) and recursive structure, in that declarations may be sequenced with semicolons. The complete specification runs to hundreds of such rules and remains precise enough for implementation and proof.
Parser generation
BNF grammars are the direct input to parser generators such as Yacc, Bison and ANTLR. Their formal structure allows parsing tables and recursive descent parsers to be generated automatically, so that most languages need no hand-written parsing logic. Modern parser generators often accept variants of EBNF, translating the more concise notation into the pushdown automata that context-free parsing requires, usually realised as parsing tables driving a stack.
Limits
Pure BNF describes only context-free languages, and so cannot express constraints such as declaration before use or the consistency of types. These semantic properties call for other methods of specification. Attribute grammars augment productions with semantic actions; two-phase compilation separates syntactic parsing from semantic analysis; and constraint languages state further conditions of well-formedness.
Still, BNF remains the basis of syntax specification for virtually every programming language, from the formal grammar of C in the ISO standard to the ECMAScript specification for JavaScript. It owes that longevity to being grounded in formal language theory while remaining of immediate use to anyone building a compiler, a combination rarer than it ought to be.