Backus–Naur Form
In computer science, BNF (Backus Normal Form or Backus–Naur Form) is one of the two main notation techniques for context-free grammars, often used to describe the syntax of languages used in computing, such as computer programming languages, document formats, instruction sets and communication protocols; the other main technique for writing context-free grammars is the van Wijngaarden form.[1] They are applied wherever exact descriptions of languages are needed: for instance, in official language specifications, in manuals, and in textbooks on programming language theory.
Many extensions and variants of the original Backus–Naur notation are used; some are exactly defined, including Extended Backus–Naur Form (EBNF) and Augmented Backus–Naur Form (ABNF).
History
The idea of describing the structure of language using rewriting rules can be traced back to at least the work of Pāṇini (who lived sometime between the 7th and 4th century BCE).[2][3] His notation to describe Sanskrit word structure notation is equivalent in power to that of Backus and has many similar properties.
In Western society, grammar was long regarded as a subject for teaching, rather than scientific study; descriptions were informal and targeted at practical usage. In the first half of the 20th century, linguists such as Leonard Bloomfield and Zellig Harris started attempts to formalize the description of language, including phrase structure.
Meanwhile, string rewriting rules as formal, abstract systems were introduced and studied by mathematicians such as Axel Thue (in 1914), Emil Post (1920s–40s) and Alan Turing (1936). Noam Chomsky, teaching linguistics to students of information theory at MIT, combined linguistics and mathematics, by taking what is essentially Thue's formalism as the basis for the description of the syntax of natural language. He also introduced a clear distinction between generative rules (those of context-free grammars) and transformation rules (1956).[4][5]
John Backus, a programming language designer at IBM, proposed a metalanguage of "metalinguistic formulas"[6][7][8] to describe the syntax of the new programming language IAL, known today as ALGOL 58 (1959), and first used in the ALGOL 60 report.
BNF is a notation for Chomsky's context-free grammars. Apparently, Backus was familiar with Chomsky's work.[9]
As proposed by Backus, the formula defined "classes" whose names are enclosed in angle brackets. For example, <ab>. Each of these names denotes a class of basic symbols.[6]
Further development of ALGOL led to ALGOL 60. In the committee's 1963 report, Peter Naur called Backus's notation Backus Normal Form. Donald Knuth argued that BNF should rather be read as Backus–Naur Form, as it is "not a normal form in the conventional sense",[10] unlike, for instance, Chomsky Normal Form. The name Pāṇini Backus form has also been suggested in view of the fact that the expansion Backus Normal Form may not be accurate, and that Pāṇini had independently developed a similar notation earlier. [11]
BNF, as described by Peter Naur in the ALGOL 60 report are metalinguistic formula. "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 metalingustic connectives. Any mark in a formula, which is not a variable or a connective, denotes itself. Juxtaposition of marks and/or variables in a formula signifies juxtaposition of the sequence denoted." [12]
Another example from the ALGOL 60 report illustrates a major difference of the BNF metalanguage and a Chomsky context free grammar. Metalingustic variables do not require a rule defining their formation. Their formation may simply be described in natural language within the <> brackets. The following ALGOL 60 report section 2.3 comments specification, exemplifies how this works:
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 ;>; | begin |
end <any sequence not containing end or ; or else> | end |
By equivalence is here meant that any of the three structures shown in the left hand column may be replaced, in any occurrence outside of strings, by the symbol shown in the same line in the right hand column without any effect on the action of the program. (end ALGOL quote)
Naur changed two of Backus's symbols to commonly available characters. The "::=" symbol was originally a ":≡". The "|" symbol was originally the word "or" (with a bar over it).[7]:14
Working for IBM, Backus would have had a non-disclosure agreement and couldn't have talked about his source if it came from an IBM proprietary project. BNF is very similar to canonical form boolean algebra equations that are, and were at the time, used in logic circuit design. Backus was a mathematician and the designer of the FORTRAN programming language. Studies of boolean algebra is commonly part of a mathematics. What we do know is that neither Backus nor Naur described the names enclosed in < > as non-terminals. Chomsky terminology was not originally used in describing BNF. Naur later described them as classes in ALGOL course materials.[6] In the ALGOL 60 report they were called metalinguistic variables. Anything other than the meta symbols ::=, |, and class names in closed in <,> are symbols of the language being defined. The meta symbols ::= is to be interpreted as "is defined as". The | is used to separate alternative definitions and is interpreted as "or". The meta symbols <,> are delimiters enclosing a class name. BNF is described as a metalanguage for talking about ALGOL by Peter Naur and Saul Rosen.[6] In 1947 Saul Rosen became involved in the activities of the fledgling Association for Computing Machinery, first on the languages committee that became the IAL group and eventually led to ALGOL. He was the first managing editor of the Communications of the ACM. What we do know is that BNF was first used as a metalanguage to talk about the ALGOL language in the ALGOL 60 report. That is how it is explained in ALGOL programming course material developed by Peter Naur in 1962.[6] Early ALGOL manuals by IBM, Honeywell, Burroughs and Digital Equipment Corporation followed the ALGOL 60 report using it as a metalanguage. Saul Rosen in his book[13] describes BNF as a metalanguage for talking about ALGOL. An example of its use as a metalanguage would be in defining an arithmetic expression:
<expr> ::= <term>|<expr><addop><term>
The first symbol of an alternative may be the class being defined. As explained by Naur; The meaning of which is to specify a sequence beginning with a previous alternative.[6] For example, above <expr> is defined as a <term> followed by any number of <addop> <term>.
In some later metalanguages such Schorre's META II the BNF recursive repeat construct is replaced by a sequence operator and target language symbols defined using quoted strings. The < and > bracket removed. Mathematical grouping ( ) were added. The <expr> rule would appear in META II as:
- EXPR = TERM $('+' TERM .OUT('ADD') | '-' TERM .OUT('SUB'));
These changes made META II and its derivative programming languages that are able to define and extend their own metalanguage. In so doing the ability to use a natural language description, meta linguistic variable, language construct description was lost. Many spin-off metalanguages were inspired by BNF. See META II, TREE-META, and Metacompiler.
A BNF class describes a language construct formation, with formation defined as a pattern or the action of forming the pattern. The class name expr is described in a natural language as a <term> followed by a sequence <addop> <term>. A class is an abstraction we can talk about it independent of its formation. We can talk about term, independent of its definition, as being added or subtracted in expr. We can talk about a term being a specific data type and how an expr is to be evaluated having specific combinations of data types. Or even reordering an expression to group data types and evaluation results of mixed types. The natural language supplement provided specific details of the language class semantics to be used by a compiler implementation and a programmer writing an ALGOL program. Natural language description further supplemented the syntax as well. The integer rule is a good example of natural and metalanguage used to describe syntax:
<integer> ::= <digit>|<integer><digit>
There are no specifics on white space in the above. As far as the rule states we could have space between the digits. In the natural language we complement the BNF metalanguage by explaining that the digit sequence can have no white space between the digits. English is only one of the possible natural languages. Translations of the ALGOL reports were available in many natural languages.
The origin of BNF is not as important as its impact on programming language development. During the period immediately following the publication of the ALGOL 60 report BNF was the basis of many compiler-compiler systems. Some directly used BNF like "A Syntax Directed Compiler for ALGOL 60" developed by E. T. Irons and "A Compiler Building System" Developed by Brooker and Morris. Others changed it to a programming language. The Schorre Metacompilers made it a programming language with only a few changes.. <class name> became symbol identifiers dropping the enclosing <,> and using quoted strings for symbols of the target language. Arithmetic like grouping provided simplification that removed using classes were grouping was its only value. The META II arithmetic expression rule shows grouping use. Output expressions placed in a META II rule are used to output code and labels in an assembly language. Rules in META II are equivalent to a class definitions in BNF. The Unix utility yacc is based on BNF with code production similar to META II. Though yacc is most commonly used as a parser generator its roots are obviously BNF. BNF today is one of the oldest computer related languages still in use.
Introduction
A BNF specification is a set of derivation rules, written as
<symbol> ::= __expression__
where <symbol>[6] is a nonterminal, and the __expression__ consists of one or more sequences of symbols; more sequences are separated by the vertical bar, '|', indicating a choice, the whole being a possible substitution 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 <>.[6]
The '::=' means that the symbol on the left must be replaced with the expression on the right.
Example
As an example, consider this possible BNF for a U.S. postal address:
<postal-address> ::= <name-part> <street-address> <zip-part>
<name-part> ::= <personal-part> <last-name> <opt-suffix-part> <EOL>
| <personal-part> <name-part>
<personal-part> ::= <initial> "." | <first-name>
<street-address> ::= <house-num> <street-name> <opt-apt-num> <EOL>
<zip-part> ::= <town-name> "," <state-code> <ZIP-code> <EOL>
<opt-suffix-part> ::= "Sr." | "Jr." | <roman-numeral> | ""
<opt-apt-num> ::= <apt-num> | ""
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 suffix (Jr., Sr., or dynastic number) and end-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 a first name or an initial followed by a dot.
- A street address consists of a house number, followed by a street name, followed by an optional apartment specifier, followed by an end-of-line.
- A zip-part consists of a town-name, followed by a comma, followed by a state code, followed by a ZIP-code followed by an end-of-line.
- A opt-suffix-part consists of a suffix, such as "Sr.", "Jr." or a roman-numeral, or an empty string (i.e. nothing).
- A opt-apt-num consists of an apartment number or an empty string (i.e. nothing).
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:
<syntax> ::= <rule> | <rule> <syntax>
<rule> ::= <opt-whitespace> "<" <rule-name> ">" <opt-whitespace> "::=" <opt-whitespace> <expression> <line-end>
<opt-whitespace> ::= " " <opt-whitespace> | ""
<expression> ::= <list> | <list> <opt-whitespace> "|" <opt-whitespace> <expression>
<line-end> ::= <opt-whitespace> <EOL> | <line-end> <line-end>
<list> ::= <term> | <term> <opt-whitespace> <list>
<term> ::= <literal> | "<" <rule-name> ">"
<literal> ::= '"' <text> '"' | "'" <text> "'"
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 and/or line-feed, 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 pipe "|". These two lists consists of some terms (three terms and two terms, respectively). Each term in this particular rule is a rule-name.
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 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.
ABNF and RBNF 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:
- Optional items enclosed in square brackets:
[<item-x>]
. - Items existing 0 or more times are enclosed in curly brackets or suffixed with an asterisk,
*
, such as<word> ::= <letter> {<letter>}
or<word> ::= <letter> <letter>*
, respectively. - Items existing 1 or more times are suffixed with an addition (plus) symbol,
+
. - Terminals may appear in bold rather than italics, and non-terminals in plain text rather than angle brackets.
- Where items are grouped, they are enclosed in simple parentheses.
See also
- Extended Backus–Naur Form
- Augmented Backus–Naur Form
- Translational Backus-Naur Form
- Syntax diagram (railroad diagram)
- Definite clause grammar, a more expressive alternative to BNF used in Prolog
- Wirth syntax notation, an alternative to BNF from 1977
- Metalanguage
Software using BNF
- ANTLR, another parser generator written in Java
- BNF Converter (BNFC[14]), operating on a variant called Labelled Backus–Naur Form (LBNF). In this variant, each production for a given non-terminal is given a label, which can be used as a constructor of an algebraic data type representing that nonterminal. The converter is capable of producing types and parsers for abstract syntax in several languages, including Haskell and Java.
- Coco/R, compiler generator accepting an attributed grammar in EBNF
- DMS Software Reengineering Toolkit, program analysis and transformation system for arbitrary languages
- GOLD BNF parser
- GNU bison, GNU version of yacc
- RPA BNF parser.[15] Online (PHP) demo parsing: JavaScript, XML
- XACT X4MR System,[16] a rule-based expert system for programming language translation
- XPL Analyzer, a tool which accepts simplified BNF for a language, and which produces a parser for that language in XPL, and which may be integrated into the supplied SKELETON program, with which the language may be debugged[17] (SHARE contributed program, which was preceded by A Compiler Generator, ISBN 978-0-13-155077-3, which see)
- Yacc parser generator (used with Lex pre-processor)
- bnfparser2,[18] a universal syntax verification utility
- bnf2xml Markup input with XML tags using advanced BNF matching.
- JavaCC Java Compiler Compiler tm (JavaCC tm) - The Java Parser Generator
References
This article is based on material taken from 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.
- ↑ Grune, Dick (1999). Parsing Techniques: A Practical Guide. US: Springer.
- ↑ "Panini biography". School of Mathematics and Statistics, University of St Andrews, Scotland. Retrieved 2014-03-22.
- ↑ Ingerman, Peter Zilahy (March 1967). ""Pāṇini-Backus Form" Suggested". Communications of the ACM (Association for Computing Machinery) 10 (3): 137. doi:10.1145/363162.363165. Retrieved 24 September 2014. Ingerman suggests that the Backus Normal Form be renamed to the Pāṇini-Backus Form, to give due credit to Pāṇini as the earliest independent inventor.
- ↑ Chomsky, Noam (1956). "Three models for the description of language" (PDF). IRE Transactions on Information Theory 2 (2): 113–24. doi:10.1109/TIT.1956.1056813.
- ↑ Chomsky, Noam (1957). Syntactic Structures. The Hague: Mouton.
- 1 2 3 4 5 6 7 8 The meaning of syntactic formula may be further explained by saying that words enclosed in the brackets < >, like <ab>, denote classes whose members are sequences of basic symbols. Class designations of this kind are found in any description of a language. For describing ordinary natural languages designation like word, verb, noun, are used. , Peter Naur (1961)."A COURSE ON ALGOL PROGRAMMING". p. 5, Note 1. Retrieved 26 March 2015.
- 1 2 Backus, J.W. (1959). "The syntax and semantics of the proposed international algebraic language of the Zurich ACM-GAMM Conference" (PDF). Proceedings of the International Conference on Information Processing. UNESCO. pp. 125–132.
- ↑ Farrell, James A. (August 1995). "Compiler Basics: Extended Backus Naur Form". Archived from the original on 5 June 2011. Retrieved May 11, 2011.
- ↑ Fulton, III, Scott M. (20 March 2007). "John W. Backus (1924 - 2007)". BetaNews. Inc. Retrieved Jun 3, 2014.
- ↑ Knuth, Donald E. (1964). "Backus Normal Form vs. Backus Naur Form". Communications of the ACM 7 (12): 735–736. doi:10.1145/355588.365140.
- ↑ Ingerman, P. Z. (1967). ""Pāṇini Backus Form" suggested". Communications of the ACM 10 (3): 137. doi:10.1145/363162.363165.
- ↑ Revised ALGOL 60 report section. 1.1."ALGOL 60". Retrieved April 18, 2015.
- ↑ Saul Rosen (Jan 1967). Programming Systems and Languages. McGraw Hill Computer Science Series. New York/NY: McGraw Hill. ISBN 0070537089.
- ↑ "BNFC", Language technology, SE: Chalmers
- ↑ "Online demo", RPatk
- ↑ "Tools", Act world, archived from the original on 2006-01-02
- ↑ 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.
- ↑ "BNF parser²", Source forge (project)
External links
- Morrison, Kelly (July 2, 1993), "Backus Normal Form vs. Backus-Naur Form", comp.compilers (newsgroup), IECC posting quotes Alan J. Perlis and Peter Naur from "ALGOL Section" of Richard L. Wexelblat, editor, History of Programming languages (1981)
- Garshol, Lars Marius, BNF and EBNF: What are they and how do they work?, NO: Priv.
- RFC 4234 — Augmented BNF for Syntax Specifications: ABNF.
- RFC 5511 — Routing BNF: A Syntax Used in Various Protocol Specifications.
- ISO/IEC 14977:1996(E) Information technology – Syntactic metalanguage – Extended BNF, available from "Publicly available", Standards, ISO or from Kuhn, Marcus, Iso 14977 (PDF), UK: CAM (the latter is missing the cover page, but is otherwise much cleaner)
Language grammars
- Bernhard, Algol-60 BNF, DE: LRZ München, the original BNF.
- "BNF grammars for SQL-92, SQL-99 and SQL-2003", Savage, AU: Net, freely available BNF grammars for SQL.
- "BNF Web Club", DB research, CH: Unige, freely available BNF grammars for SQL, Ada, Java.
- "Free Programming Language Grammars for Compiler Construction", Source code, The free country, freely available BNF/EBNF grammars for C/C++, Pascal, COBOL, Ada 95, PL/I.
- "BNF files related to the STEP standard", Exp engine (SVN), Source forge. Includes parts 11, 14, and 21 of the ISO 10303 (STEP) standard.