- Backus–Naur Form
In
computer science , Backus–Naur Form (BNF) is ametasyntax used to expresscontext-free grammar s: that is, a formal way to describeformal language s.John Backus andPeter Naur developed a context free grammar to define the syntax of a programming language by using two sets of rules: i.e., lexical rules and syntactic rules.BNF is widely used as a
notation for thegrammar s of computerprogramming language s,instruction set s andcommunication protocol s, as well as a notation for representing parts ofnatural language grammars. Many textbooks for programming language theory and/or semantics document the programming language in BNF.There are many extensions and variants of BNF, including "Extended" and "Augmented" Backus–Naur Forms (EBNF and ABNF).
History
John Backus created the notation in order to express the grammar ofALGOL . At the first World Computer Congress, which took place in Paris in1959 , Backus presented "The syntax and semantics of the proposed international algebraic language of the Zurich ACM-GAMM Conference", a formal description of the international algebraic language(IAL) which was later calledALGOL 58 . The formal language he presented was based onEmil Post 's production system. Generative grammars were an active subject of mathematical study, e.g. byNoam Chomsky , who was applying them to the grammar of natural language.cite journal
last= Chomsky
first= Noam
authorlink= Noam Chomsky
title= Three Models for the Description of Language
journal= IRE Transactions on Information Theory
volume= Vol. 2
issue= No. 2
pages= pp. 113–123
year= 1956
doi= 10.1109/TIT.1956.1056813] cite book
last= Chomsky
first= Noam
title= Syntactic Structures
publisher= Mouton
location= The Hague
year=1957]Peter Naur (ALGOL 60, 1963) identified Backus's notation as Backus Normal Form, and simplified it to minimize the character set used, and, at the suggestion ofDonald Knuth , his name was added in recognition of his contribution, his initial replacing the N for "Normal" since, Knuth argued, the BNF is "not a normal form in any sense". [cite journal
last= Knuth
first= Donald E.
authorlink= Donald Knuth
title= Backus Normal Form vs. Backus Naur Form
journal= Communications of the ACM
volume= 7
issue= 12
pages= pp. 735–736
year= 1964
doi= 10.1145/355588.365140] The Backus–Naur Form or BNF grammars have significant similarities to Unicode|Pāṇini 's grammar rules, and the notation is sometimes also referred to as Panini–Backus Form. [P.Z. Ingerman (1967)]Introduction
A BNF specification is a set of derivation rules, written as
where <
symbol > is a "nonterminal ", and the __expression__ consists of one or more sequences of symbols; more sequences are separated by thevertical bar , '|', indicating achoice , the whole being a possiblesubstitution for the symbol on the left. Symbols that never appear on a left side are "terminals". On the other hand, symbols that appear on a left side are "non-terminals" and are always enclosed between the pair <>.Example
As an example, consider this possible BNF for a U.S. postal address: This translates into English as: :* A postal address consists of a name-part, followed by a street-address part, followed by a zip-code part. :* A name-part consists of either: a personal-part followed by a
last name followed by an optional "jr-part" (Jr., Sr., or dynastic number) andend-of-line , or a personal part followed by a name part (this rule illustrates the use of recursion in BNFs, covering the case of people who use multiple first and middle names and/or initials). :* A personal-part consists of either afirst name or aninitial followed by a dot. :* A street address consists of an optionalapartment specifier, followed by a house number, followed by a street name, followed by an end-of-line.:* A zip-part consists of atown -name, followed by a comma, followed by a state code, followed by a ZIP-code followed by an end-of-line.Note that many things (such as the format of a first-name, apartment specifier, ZIP-code, and Roman numeral) are left unspecified here. If necessary, they may be described using additional BNF rules.
Further examples
BNF's syntax itself may be represented with a BNF like the following:This assumes that no
whitespace is necessary for proper interpretation of the rule.represents the appropriate line-end specifier (in ASCII , carriage-return and/or line-feed, depending on theoperating system ).and are to be substituted with a declared rule's name/label or literal text, respectively. Variants
There are many variants and extensions of BNF, generally either for the sake of simplicity and succinctness, or to adapt it to a specific application. One common feature of many variants is the use of
regexp repetition operators such as*
and+
. TheExtended Backus–Naur Form (EBNF) is a common one. In fact the example above is not the pure form invented for the ALGOL 60 report. The bracket notation " [] " was introduced a few years later in IBM'sPL/I definition but is now universally recognised. ABNF is another extension commonly used to describeIETF protocols.Parsing expression grammar s build on the BNF andregular expression notations to form an alternative class offormal grammar , which is essentially analytic rather than generative in character.Many BNF specifications found online today are intended to be human readable and are non-formal. These often include many of the following syntax rules and extensions:
* Optional items enclosed in square brackets. E.g. [
]
* Items repeating 0 or more times are enclosed in curly brackets or suffixed with an asterisk. E.g.::= { }
* Items repeating 1 or more times are followed by a '+'
* Terminals may appear in bold and NonTerminals in plain text rather than using italics and angle brackets
* Alternative choices in a production are separated by the ‘|’ symbol. E.g.,|
* Where items need to be grouped they are enclosed in simple parenthesesee also
*
Extended Backus–Naur Form .
*Ashtadhyayi (Sanskrit grammar with mathematical structure).
*Syntax diagram (Railroad diagram).
* GOLD BNF parser.
*GNU bison GNU version of yacc.
*Wirth syntax notation An alternative to BNF from 1977.
*Yacc parser generator (used with Lex pre-processor).
*ANTLR Another parser generator written in Java.
* John BackusReferences
External links
* [http://www.lrz-muenchen.de/~bernhard/Algol-BNF.html Algol-60 BNF] , the original BNF.
* Sample grammars at the [http://cui.unige.ch/db-research/Enseignement/analyseinfo/BNFweb.html BNF Web club] .
* [http://compilers.iecc.com/comparch/article/93-07-017] contains a posting on news:comp.compilers that explains some of the history of the two names (Backus–Naur Form vs. Backus Normal Form).
* Article " [http://www.garshol.priv.no/download/text/bnf.html BNF and EBNF: What are they and how do they work?] " by Lars Marius Garshol.
* [ftp://ftp.rfc-editor.org/in-notes/rfc4234.txt RFC 4234] Augmented BNF for Syntax Specifications: ABNF
* [http://www-cgi.uni-regensburg.de/~brf09510/grammartypes.html Comparision of different variants of BNF]
* [http://www-cgi.uni-regensburg.de/~brf09510/syntax/lazyebnf.ebnf.html Syntax diagram of EBNF]
* [http://www-cgi.uni-regensburg.de/~brf09510/syntax.html Generation of syntax diagrams from EBNF]
Wikimedia Foundation. 2010.