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 as of December 2010[update] is RFC 5234, and it often serves as the definition language for IETF communication protocols.
- 1 Introduction
- 2 Terminal values
- 3 Operators
- 4 Example
- 5 Pitfalls
- 6 References
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:
<rUlENamE> 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.
Terminals are specified by one or more numeric characters.
Numeric characters may be specified as the percent sign “
%”, followed by the base (b = binary, d = decimal, and x = hexadecimal), followed by the value, or concatenation of values (indicated by “
.”). For example a carriage return is specified by
%d13 in decimal or
%x0D 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 “abc” will match “abc”, “Abc”, “aBc”, “abC”, “ABc”, “AbC”, “aBC”, and “ABC”. For a case-sensitive match the explicit characters must be defined: to match “aBc” the definition will be
%d97 %d66 %d99.
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 is LWS (linear white space).
A semicolon (“
;”) starts a comment that continues to the end of the line.
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
Rule1 / Rule2
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
Rule1 =/ Rule2
Additional alternatives may be added to a rule through the use of “
=/” between the rule name and the definition.
ruleset = alt1 / alt2 / alt3 / alt4 / alt5
is equivalent to
ruleset = alt1 / alt2
ruleset =/ alt3
ruleset =/ alt4 / alt5
A range of numeric values may be specified through the use of a hyphen (“
OCTAL = "0" / "1" / "2" / "3" / "4" / "5" / "6" / "7"
is equivalent to
OCTAL = %x30-37
Elements may be placed in parentheses to group rules in a definition.
To match “elem fubar snafu” or “elem tarfu snafu” the following rule could be constructed:
group = elem (fubar / tarfu) snafu
To match “elem fubar” or “tarfu snafu” the following rules could be constructed:
group = elem fubar / tarfu snafu
group = (elem fubar) / (tarfu snafu)
To indicate repetition of an element the form
<a>*<b>element is used. The optional
<a> gives the minimum number of elements to be included with the default of 0. The optional
<b> gives the maximum number of elements to be included with the default of infinity.
*element for zero or more elements,
*1element for zero or one element,
1*element for one or more elements, and
2*3element for two or three elements, cf. regular expressions
To indicate an explicit number of elements the form
<a>element is used and is equivalent to
2DIGIT to get two numeric digits and
3DIGIT to get three numeric digits. (DIGIT is defined below under 'Core rules'. Also see zip-code in the example below.)
To indicate an optional element the following constructions are equivalent:
The following operators have the given precedence from tightest binding to loosest binding:
- Strings, Names formation
- Value range
- Grouping, Optional
Use of the alternative operator with concatenation may be confusing and it is recommended that grouping be used to make explicit concatenation groups.
The core rules are defined in the ABNF standard.
|ALPHA||%x41-5A / %x61-7A||Upper- and lower-case ASCII letters (A–Z, a–z)|
|DIGIT||%x30-39||Decimal digits (0–9)|
|HEXDIG||DIGIT / "A" / "B" / "C" / "D" / "E" / "F"||Hexadecimal digits (0–9, A–F)|
|WSP||SP / HTAB||space and horizontal tab|
|LWSP||*(WSP / CRLF WSP)||linear white space (past newline)|
|VCHAR||%x21-7E||visible (printing) characters|
|CHAR||%x01-7F||any ASCII character, excluding NUL|
|OCTET||%x00-FF||8 bits of data|
|CTL||%x00-1F / %x7F||controls|
|CRLF||CR LF||Internet standard newline|
|BIT||"0" / "1"||binary digit|
The postal address example given in the Backus–Naur Form (BNF) page may be specified as follows:
postal-address = name-part street zip-part name-part = *(personal-part SP) last-name [SP suffix] CRLF name-part =/ personal-part CRLF personal-part = first-name / (initial ".") first-name = *ALPHA initial = ALPHA last-name = *ALPHA suffix = ("Jr." / "Sr." / 1*("I" / "V" / "X")) street = [apt SP] house-num SP street-name CRLF apt = 1*4DIGIT house-num = 1*8(DIGIT / ALPHA) street-name = 1*VCHAR zip-part = town-name "," SP state 1*2SP zip-code CRLF town-name = 1*(ALPHA / SP) state = 2ALPHA zip-code = 5DIGIT ["-" 4DIGIT]
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.
- "Official Internet Protocol Standards". RFC Editor. 2010-02-21. Archived from the original on 9 February 2010. Retrieved 2010-02-21.
- Crocker, D.; Overell, P. (January 2008). "Augmented BNF for Syntax Specifications: ABNF" (plain text). RFC Editor. p. 16. Retrieved 2010-02-21.
- "RFC Index". RFC Editor. 2010-02-19. Archived from the original on 9 February 2010. Retrieved 2010-02-21.