Augmented Backus–Naur form

In computer science, augmented Backus–Naur form (ABNF) is a metalanguage based on Backus–Naur form (BNF), but consisting of its own syntax and derivation rules. The motive principle for ABNF is to describe a formal system of a language to be used as a bidirectional communications protocol. It is defined by Internet Standard 68 ("STD 68", type case sic), which is, and it often serves as the definition language for IETF communication protocols.

supersedes. updates it, adding a syntax for specifying case-sensitive string literals.

Overview


An ABNF specification is a set of derivation rules, written as

rule = definition ; comment CR LF

where rule is a case-insensitive nonterminal, the definition consists of sequences of symbols that define the rule, a comment for documentation, and ending with a carriage return and line feed.

Rule names are case-insensitive:,  ,  , and   all refer to the same rule. Rule names consist of a letter followed by letters, numbers, and hyphens.

Angle brackets are not required around rule names (as they are in BNF). However, they may be used to delimit a rule name when used in prose to discern a rule name.

Terminal values
Terminals are specified by one or more numeric characters.

Numeric characters may be specified as the percent sign, followed by the base (  = binary,   = decimal, and   = hexadecimal), followed by the value, or concatenation of values (indicated by  ). For example, a carriage return is specified by  in decimal or   in hexadecimal. A carriage return followed by a line feed may be specified with concatenation as.

Literal text is specified through the use of a string enclosed in quotation marks. These strings are case-insensitive, and the character set used is (US-)ASCII. Therefore, the string  will match “abc”, “Abc”, “aBc”, “abC”, “ABc”, “AbC”, “aBC”, and “ABC”. RFC 7405 added a syntax for case-sensitive strings:  will only match "aBc". Prior to that, a case-sensitive string could only be specified by listing the individual characters: to match “aBc”, the definition would be. A string can also be explicitly specified as case-insensitive with a  prefix.

White space
White space is used to separate elements of a definition; for space to be recognized as a delimiter, it must be explicitly included. The explicit reference for a single whitespace character is  (linear white space), and   is for zero or more whitespace characters with newlines permitted. The  definition in RFC5234 is controversial because at least one whitespace character is needed to form a delimiter between two fields.

Definitions are left-aligned. When multiple lines are required (for readability), continuation lines are indented by whitespace.

Comment
A semicolon starts a comment that continues to the end of the line.

Concatenation
A rule may be defined by listing a sequence of rule names.

To match the string “aba”, the following rules could be used:
 * fu = %x61	; a
 * bar = %x62	; b
 * mumble = fu bar fu

Alternative
A rule may be defined by a list of alternative rules separated by a solidus.

To accept the rule fu or the rule bar, the following rule could be constructed:
 * fubar = fu / bar

Incremental alternatives
Additional alternatives may be added to a rule through the use of  between the rule name and the definition.

The rule is therefore equivalent to
 * ruleset = alt1 / alt2
 * ruleset =/ alt3
 * ruleset =/ alt4 / alt5
 * ruleset = alt1 / alt2 / alt3 / alt4 / alt5

Value range
A range of numeric values may be specified through the use of a hyphen.

The rule is equivalent to
 * OCTAL = %x30-37
 * OCTAL = "0" / "1" / "2" / "3" / "4" / "5" / "6" / "7"

Sequence group
Elements may be placed in parentheses to group rules in a definition.

To match "a b d" or "a c d", the following rule could be constructed:
 * group = a (b / c) d

To match “a b” or “c d”, the following rules could be constructed:
 * group = a b / c d
 * group = (a b) / (c d)

Variable repetition
To indicate repetition of an element, the form  is used. The optional  gives the minimal number of elements to be included (with the default of 0). The optional  gives the maximal number of elements to be included (with the default of infinity).

Use  for zero or more elements,   for zero or one element,   for one or more elements, and   for two or three elements, cf. regular expressions,  ,   and.

Specific repetition
To indicate an explicit number of elements, the form  is used and is equivalent to.

Use  to get two numeric digits, and   to get three numeric digits. ( is defined below under "Core rules". Also see zip-code in the example below.)

Optional sequence
To indicate an optional element, the following constructions are equivalent:
 * [fubar snafu]
 * *1(fubar snafu)
 * 0*1(fubar snafu)

Operator precedence
The following operators have the given precedence from tightest binding to loosest binding:
 * 1) Strings, names formation
 * 2) Comment
 * 3) Value range
 * 4) Repetition
 * 5) Grouping, optional
 * 6) Concatenation
 * 7) Alternative

Use of the alternative operator with concatenation may be confusing, and it is recommended that grouping be used to make explicit concatenation groups.

Core rules


The core rules are defined in the ABNF standard.

Note that in the core rules diagram the CHAR2 charset is inlined in char-val and CHAR3 is inlined in prose-val in the RFC spec. They are named here for clarity in the main syntax diagram.

Example
The (U.S.) postal address example given in the augmented Backus–Naur form (ABNF) page may be specified as follows:

Pitfalls
RFC 5234 adds a warning in conjunction to the definition of LWSP as follows: "Use of this linear-white-space rule permits lines containing only white space that are no longer legal in mail headers and have caused interoperability problems in other contexts. Do not use when defining mail headers and use with caution in other contexts."