Backus-Naur form

The Backus-Naur form (BNF) (also known as Backus normal form) is a metasyntax used to express context-free grammars: that is, a formal way to describe formal languages. BNF is widely used as a notation for the grammars of computer programming languages, command sets and communication protocols. They can also be used as a notation for representing parts of natural language grammars. For example see the grammar for meter in Venpa poetry. Most textbooks for programming language theory and/or semantics document the programming language in BNF. Some variants, for example ABNF, have their own documentation.

BNF was originally named after John Backus and later (at the suggestion of Donald Knuth) also after Peter Naur. They were two pioneers in computer science, especially in the art of compiler design. The Backus-Naur Form or BNF grammars have significant similarities to Pāṇini's grammar rules, and is sometimes also referred to as Panini-Backus Form. BNF was created as part of creating the rules for Algol 60.

Contents

Introduction

A BNF specification is a set of derivation rules, written as

<symbol> ::= <expression with symbols>

where <symbol> is a nonterminal, and the expression consists of sequences of symbols and/or sequences 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.

Example

As an example, consider this BNF for a US postal address:

<postal-address> ::= <name-part> <street-address> <zip-part>

<personal-part> ::= <first-name> | <initial> "."

<name-part> ::= <personal-part> <last-name> [<jr-part>] <EOL> | <personal-part> <name-part>

<street-address> ::= [<apt>] <house-num> <street-name> <EOL>

<zip-part> ::= <town-name> "," <state-code> <ZIP-code> <EOL>

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 personal-part consists of either a first name or an initial followed by a dot.
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) 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 street address consists of an optional apartment specifier, followed by a street number, followed by a street name.
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.

Note that many things (such as the format of a personal-part, apartment specifier, or ZIP-code) are left unspecified here. If necessary, they may be described using additional BNF rules, or left as abstraction if irrelevant for the purpose at hand.

Further examples

Interestingly enough, BNF's syntax may be represented in BNF as follows:

<syntax> ::= <rule> [<syntax>]
<rule> ::= <whitespace> "<" <rule-name> ">" <whitespace> "::=" <expression> <whitespace> <line-end>
<expression> ::= <whitespace> <or-expression>
<or-expression> ::= <whitespace> <list-expression> [ "|" <or-expression> ]
<list-expression> ::= <whitespace>  ("<" <rule-name> ">" | <QUOTE> <text> <QUOTE> | "(" <expression> ")" | "[" <expression> "]") [<list-expression>]
<whitespace> ::= [" " <whitespace>]
<line-end> ::= [<whitespace>] <EOL> [<line-end>]

This assumes that no whitespace is necessary for proper interpretation of the rule. <QUOTE> is presumed to be the " character, and <EOL> to be a carriage-return/line-feed. <rule-name> and <text> are to be substituted with a declared rule's name/label or literal text, respectively.

Variants

There are many variants and extensions of BNF, possibly containing some or all of the regexp wild cards such as "*" or "+". The Extended 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's PL/I definition but is now universally recognised. ABNF is another extension commonly used to describe 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.

See also

References

This article was originally based on material from the Free On-line Dictionary of Computing, which is licensed under the GFDL.

External links

fr:Forme de Backus-Naur it:BNF nl:Backus-Naur-formalisme ja:バッカス・ナウア記法 pl:Notacja BNF tr:Backus-Naur form zh:巴科斯范式

Navigation

  • Art and Cultures
    • Art (https://academickids.com/encyclopedia/index.php/Art)
    • Architecture (https://academickids.com/encyclopedia/index.php/Architecture)
    • Cultures (https://www.academickids.com/encyclopedia/index.php/Cultures)
    • Music (https://www.academickids.com/encyclopedia/index.php/Music)
    • Musical Instruments (http://academickids.com/encyclopedia/index.php/List_of_musical_instruments)
  • Biographies (http://www.academickids.com/encyclopedia/index.php/Biographies)
  • Clipart (http://www.academickids.com/encyclopedia/index.php/Clipart)
  • Geography (http://www.academickids.com/encyclopedia/index.php/Geography)
    • Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
    • Maps (http://www.academickids.com/encyclopedia/index.php/Maps)
    • Flags (http://www.academickids.com/encyclopedia/index.php/Flags)
    • Continents (http://www.academickids.com/encyclopedia/index.php/Continents)
  • History (http://www.academickids.com/encyclopedia/index.php/History)
    • Ancient Civilizations (http://www.academickids.com/encyclopedia/index.php/Ancient_Civilizations)
    • Industrial Revolution (http://www.academickids.com/encyclopedia/index.php/Industrial_Revolution)
    • Middle Ages (http://www.academickids.com/encyclopedia/index.php/Middle_Ages)
    • Prehistory (http://www.academickids.com/encyclopedia/index.php/Prehistory)
    • Renaissance (http://www.academickids.com/encyclopedia/index.php/Renaissance)
    • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
    • United States (http://www.academickids.com/encyclopedia/index.php/United_States)
    • Wars (http://www.academickids.com/encyclopedia/index.php/Wars)
    • World History (http://www.academickids.com/encyclopedia/index.php/History_of_the_world)
  • Human Body (http://www.academickids.com/encyclopedia/index.php/Human_Body)
  • Mathematics (http://www.academickids.com/encyclopedia/index.php/Mathematics)
  • Reference (http://www.academickids.com/encyclopedia/index.php/Reference)
  • Science (http://www.academickids.com/encyclopedia/index.php/Science)
    • Animals (http://www.academickids.com/encyclopedia/index.php/Animals)
    • Aviation (http://www.academickids.com/encyclopedia/index.php/Aviation)
    • Dinosaurs (http://www.academickids.com/encyclopedia/index.php/Dinosaurs)
    • Earth (http://www.academickids.com/encyclopedia/index.php/Earth)
    • Inventions (http://www.academickids.com/encyclopedia/index.php/Inventions)
    • Physical Science (http://www.academickids.com/encyclopedia/index.php/Physical_Science)
    • Plants (http://www.academickids.com/encyclopedia/index.php/Plants)
    • Scientists (http://www.academickids.com/encyclopedia/index.php/Scientists)
  • Social Studies (http://www.academickids.com/encyclopedia/index.php/Social_Studies)
    • Anthropology (http://www.academickids.com/encyclopedia/index.php/Anthropology)
    • Economics (http://www.academickids.com/encyclopedia/index.php/Economics)
    • Government (http://www.academickids.com/encyclopedia/index.php/Government)
    • Religion (http://www.academickids.com/encyclopedia/index.php/Religion)
    • Holidays (http://www.academickids.com/encyclopedia/index.php/Holidays)
  • Space and Astronomy
    • Solar System (http://www.academickids.com/encyclopedia/index.php/Solar_System)
    • Planets (http://www.academickids.com/encyclopedia/index.php/Planets)
  • Sports (http://www.academickids.com/encyclopedia/index.php/Sports)
  • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
  • Weather (http://www.academickids.com/encyclopedia/index.php/Weather)
  • US States (http://www.academickids.com/encyclopedia/index.php/US_States)

Information

  • Home Page (http://academickids.com/encyclopedia/index.php)
  • Contact Us (http://www.academickids.com/encyclopedia/index.php/Contactus)

  • Clip Art (http://classroomclipart.com)
Toolbox
Personal tools