BNF specifications outline how symbols are combined to form syntactically valid sequences. Each BNF consists of three core components: a set of non-terminal symbols, a set of terminal symbols, and a series of derivation rules.1 Non-terminal symbols represent categories or variables that can be replaced, while terminal symbols are the fixed, literal elements (such as keywords or punctuation) that appear in the final sequence. Derivation rules provide the instructions for replacing non-terminal symbols with specific combinations of symbols.
A derivation rule is written in the format:
where:
For example, in the rule <opt-suffix-part> ::= "Sr." | "Jr." | "", the entire line is the derivation rule, "Sr.", "Jr.", and "" (an empty string) are terminal symbols, and <opt-suffix-part> is a non-terminal symbol.
Generating a valid sequence involves starting with a designated start symbol and iteratively applying the derivation rules.3 This process can extend sequences incrementally. To allow flexibility, some BNF definitions include an optional "delete" symbol (represented as an empty alternative, e.g., <item> ::= <thing> | ), enabling the removal of certain elements while maintaining syntactic validity.4
A practical illustration of BNF is a specification for a simplified U.S. postal address:
This translates into English as:
Note that many things (such as the format of a first-name, apartment number, ZIP-code, and Roman numeral) are left unspecified here. If necessary, they may be described using additional BNF rules.
The concept of using rewriting rules to describe language structure traces back to at least Pāṇini, an ancient Indian Sanskrit grammarian who lived sometime between the 6th and 4th centuries BC.6 His notation for describing Sanskrit word structure is equivalent in power to that of BNF and exhibits many similar properties.7
In Western society, grammar was long regarded as a subject for teaching rather than scientific study; descriptions were informal and targeted at practical usage. This perspective shifted in the first half of the 20th century, when linguists such as Leonard Bloomfield and Zellig Harris began attempts to formalize language description, including phrase structure. Meanwhile, mathematicians explored related ideas through string rewriting rules as formal logical systems, such as Axel Thue in 1914, Emil Post in the 1920s–40s,8 and Alan Turing in 1936. Noam Chomsky, teaching linguistics to students of information theory at MIT combined linguistics and mathematics, adapting Thue's formalism to describe natural language syntax. In 1956, he introduced a clear distinction between generative rules (those of context-free grammars) and transformation rules.910
BNF itself emerged when John Backus, a programming language designer at IBM, proposed a metalanguage of metalinguistic formulas to define the syntax of the new programming language IAL, known today as ALGOL 58, in 1959.11 This notation was formalized in the ALGOL 60 report, where Peter Naur named it Backus normal form in the committee's 1963 report.12 Whether Backus was directly influenced by Chomsky's work is uncertain.1314
Donald Knuth argued in 1964 that BNF should be read as Backus–Naur form, as it is "not a normal form in the conventional sense," unlike Chomsky normal form.15 In 1967, Peter Zilahy Ingerman suggested renaming it Pāṇini Backus form to acknowledge Pāṇini's earlier, independent development of a similar notation.16
In the ALGOL 60 report, Naur described BNF as a metalinguistic formula:17
Sequences of characters enclosed in the brackets <> represent metalinguistic variables whose values are sequences of symbols. The marks "::=" and "|" (the latter with the meaning of "or") are metalinguistic connectives. Any mark in a formula, which is not a variable or a connective, denotes itself. Juxtaposition of marks or variables in a formula signifies juxtaposition of the sequence denoted.
This is exemplified in the report's section 2.3, where comments are specified:
For the purpose of including text among the symbols of a program the following "comment" conventions hold: The sequence of basic symbols:is equivalent to; comment <any sequence not containing ';'>;;begin comment <any sequence not containing ';'>;beginend <any sequence not containing 'end' or ';' or 'else'>end Equivalence here means that any of the three structures shown in the left column may be replaced, in any occurrence outside of strings, by the symbol shown in the same line in the right column without any effect on the action of the program.
For the purpose of including text among the symbols of a program the following "comment" conventions hold:
Equivalence here means that any of the three structures shown in the left column may be replaced, in any occurrence outside of strings, by the symbol shown in the same line in the right column without any effect on the action of the program.
Naur altered Backus's original symbols for ALGOL 60, changing :≡ to ::= and the overbarred "or" to |, using commonly available characters.18: 14
BNF is very similar to canonical-form Boolean algebra equations (used in logic-circuit design), reflecting Backus's mathematical background as a FORTRAN designer.19 Studies of Boolean algebra were commonly part of a mathematics curriculum, which may have informed Backus's approach. Neither Backus nor Naur described the names enclosed in < > as non-terminals—Chomsky's terminology was not originally used in describing BNF. Naur later called them "classes" in 1961 course materials.20 In the ALGOL 60 report, they were "metalinguistic variables," with other symbols defining the target language.
Saul Rosen, involved with the Association for Computing Machinery since 1947, contributed to the transition from IAL to ALGOL and edited Communications of the ACM. He described BNF as a metalanguage for ALGOL in his 1967 book.21 Early ALGOL manuals from IBM, Honeywell, Burroughs, and Digital Equipment Corporation followed this usage.
BNF significantly influenced programming language development, notably as the basis for early compiler-compiler systems. Examples include Edgar T. Irons' "A Syntax Directed Compiler for ALGOL 60" and Brooker and Morris' "A Compiler Building System," which directly utilized BNF.22 Others, like Schorre's META II, adapted BNF into a programming language, replacing < > with quoted strings and adding operators like $ for repetition, as in:
This influenced tools like yacc, a widely used parser generator rooted in BNF principles, and Unix utilities like yacc.23 BNF remains one of the oldest computer-related notations still referenced today, though its variants often dominate modern applications. Examples of its use as a metalanguage include defining arithmetic expressions:
Here, <expr> can recursively include itself, allowing repeated additions.
BNF today is one of the oldest computer-related languages still in use.
BNF's syntax itself may be represented with a BNF like the following:
Note that "" is the empty string.
The original BNF did not use quotes as shown in <literal> rule. This assumes that no whitespace is necessary for proper interpretation of the rule.
<EOL> represents the appropriate line-end specifier (in ASCII, carriage-return, line-feed or both depending on the operating system). <rule-name> and <text> are to be substituted with a declared rule's name/label or literal text, respectively.
In the U.S. postal address example above, the entire block-quote is a <syntax>. Each line or unbroken grouping of lines is a rule; for example one rule begins with <name-part> ::=. The other part of that rule (aside from a line-end) is an expression, which consists of two lists separated by a vertical bar |. These two lists consists of some terms (three terms and two terms, respectively). Each term in this particular rule is a rule-name.
Main article: Extended Backus–Naur form
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 regular expression repetition operators such as * and +. The extended Backus–Naur form (EBNF) is a common one.
Another common extension is the use of square brackets around optional items. Although not present in the original ALGOL 60 report (instead introduced a few years later in IBM's PL/I definition), the notation is now universally recognised.
Main article: ABNF
Augmented Backus–Naur form (ABNF) and Routing Backus–Naur form (RBNF)24 are extensions commonly used to describe Internet Engineering Task Force (IETF) protocols.
Parsing expression grammars build on the BNF and regular expression notations to form an alternative class of formal 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:
Janikow, Cezary Z. "What is BNF?" (PDF). http://www.cs.umsl.edu/~janikow/cs4280/bnf.pdf ↩
Naur, Peter (1961). "A COURSE OF ALGOL 60 PROGRAMMING with special reference to the DASK ALGOL system" (PDF). Copenhagen: Regnecentralen. Retrieved 26 March 2015. http://archive.computerhistory.org/resources/text/algol/ACM_Algol_bulletin/1064048/frontmatter.pdf ↩
This article is based on material taken from Backus-Naur+Form at the Free On-line Dictionary of Computing prior to 1 November 2008 and incorporated under the "relicensing" terms of the GFDL, version 1.3 or later. https://foldoc.org/Backus-Naur+Form ↩
"Panini biography". School of Mathematics and Statistics, University of St Andrews, Scotland. Retrieved 2014-03-22. http://www-gap.dcs.st-and.ac.uk/~history/Biographies/Panini.html ↩
Ingerman, Peter Zilahy (March 1967). ""Pāṇini-Backus Form" Suggested". Communications of the ACM. 10 (3): 137. doi:10.1145/363162.363165. S2CID 52817672. https://doi.org/10.1145%2F363162.363165 ↩
Post, Emil L. (1943). "Formal Reductions of the General Combinatorial Decision Problem". American Journal of Mathematics. 65 (2): 197–215. doi:10.2307/2371804. /wiki/Doi_(identifier) ↩
Chomsky, Noam (1956). "Three models for the description of language". IRE Transactions on Information Theory. 2 (3): 113–24. doi:10.1109/TIT.1956.1056813. S2CID 19519474. /wiki/Doi_(identifier) ↩
Chomsky, Noam (1957). Syntactic Structures. The Hague: Mouton. /wiki/Syntactic_Structures ↩
Backus, J. W. (1959). "The syntax and semantics of the proposed international algebraic language of the Zurich ACM-GAMM Conference". Proceedings of the International Conference on Information Processing. UNESCO. pp. 125–132. ↩
Revised ALGOL 60 report section 1.1. "ALGOL 60". Retrieved April 18, 2015. http://www.masswerk.at/algol60/report.htm ↩
Fulton, Scott M., III (20 March 2007). "John W. Backus (1924 - 2007)". BetaNews, Inc. Retrieved Jun 3, 2014.{{cite web}}: CS1 maint: multiple names: authors list (link) http://betanews.com/2007/03/20/john-w-backus-1924-2007 ↩
John Backus (Sep 2006). Grady Booch (ed.). Oral History of John Backus (PDF) (Report). Computer History Museum. Here: p.25 https://archive.computerhistory.org/resources/text/Oral_History/Backus_John/Backus_John_1.oral_history.2006.102657970.pdf ↩
Knuth, Donald E. (1964). "Backus Normal Form vs. Backus Naur Form". Communications of the ACM. 7 (12): 735–736. doi:10.1145/355588.365140. S2CID 47537431. https://doi.org/10.1145%2F355588.365140 ↩
Ingerman, P. Z. (1967). ""Pāṇini Backus Form" suggested". Communications of the ACM. 10 (3): 137. doi:10.1145/363162.363165. S2CID 52817672. https://doi.org/10.1145%2F363162.363165 ↩
Revised ALGOL 60 report section. 1.1."ALGOL 60". Retrieved April 18, 2015. http://www.masswerk.at/algol60/report.htm ↩
Backus, J. W. (1959). "The syntax and semantics of the proposed international algebraic language of the Zurich ACM-GAMM Conference". Proceedings of the International Conference on Information Processing. UNESCO. pp. 125–132. /wiki/John_W._Backus ↩
Saul Rosen (Jan 1967). Programming Systems and Languages. McGraw Hill Computer Science Series. New York: McGraw Hill. ISBN 978-0070537088. 978-0070537088 ↩
McKeeman, W. M.; Horning, J.J.; Wortman, D. B. (1970). A Compiler Generator. Prentice-Hall. ISBN 978-0-13-155077-3. 978-0-13-155077-3 ↩
"BNF parser²", Source forge (project) http://bnfparser2.sourceforge.net/ ↩
RBNF. http://tools.ietf.org/html/rfc5511 ↩
"Online demo", RPatk, archived from the original on 2012-11-02, retrieved 2011-07-03 https://web.archive.org/web/20121102161746/http://www.rpatk.net/web/en/onlinedemo.php ↩
"Tools", Act world, archived from the original on 2013-01-29 https://web.archive.org/web/20130129075050/http://www.actworld.com/tools/ ↩
If the target processor is System/360, or related, even up to z/System, and the target language is similar to PL/I (or, indeed, XPL), then the required code "emitters" may be adapted from XPL's "emitters" for System/360. ↩
bnf2xml http://sourceforge.net/projects/bnf2xml/ ↩
"JavaCC". Archived from the original on 2013-06-08. Retrieved 2013-09-25. https://web.archive.org/web/20130608172614/https://javacc.java.net/ ↩
"Script Syntax - Qlik Sense on Windows". Qlik.com. QlikTech International AB. Retrieved 10 January 2022. https://help.qlik.com/en-US/sense/May2021/Subsystems/Hub/Content/Sense_Hub/Scripting/script-syntax.htm ↩
"BNFC", Language technology, SE: Chalmers http://bnfc.digitalgrammars.com/ ↩