RFC 4234
Augmented BNF for Syntax Specifications: ABNF
RFC 4234: Augmented BNF for Syntax Specifications: ABNF ๐
Okay bestie, this one is the Augmented BNF (ABNF) spec ๐ง. It's RFC 4234, a Standards Track doc from the Network Working Group, dated October 2005. The authors are D. Crocker, Ed. (Brandenburg InternetWorking) and P. Overell (THUS plc.). It obsoletes RFC 2234, and the title block doesn't list any updates. ABNF is the notation Internet specs use to write down their syntax, so it's super important that we get the details right ๐
Status of This Memo ๐
This document specifies an Internet standards track protocol for the Internet community, and it requests discussion and suggestions for improvements ๐ธ For the standardization state and status of this protocol, check the current edition of the "Internet Official Protocol Standards" (STD 1) ๐ And distribution of this memo is unlimited, so share away ๐
Copyright Notice โญ
Copyright (C) The Internet Society (2005). ๐
Abstract โจ
Internet technical specs often need a formal syntax ๐ For years, a modified version of Backus-Naur Form (BNF), called Augmented BNF (ABNF), has been the popular pick across tons of Internet specs ๐ฅ This doc is all about ABNF. It balances compactness and simplicity with reasonable representational power, which is so iconic ๐ฆ The differences between standard BNF and ABNF involve naming rules, repetition, alternatives, order-independence, and value ranges. This spec also adds some extra rule definitions and an encoding for a core lexical analyzer, the kind that lots of Internet specs share ๐ซถ
Table of Contents ๐๏ธ
1. INTRODUCTION ....................................................2
2. RULE DEFINITION .................................................3
2.1. Rule Naming ................................................3
2.2. Rule Form ..................................................3
2.3. Terminal Values ............................................4
2.4. External Encodings .........................................5
3. OPERATORS .......................................................6
3.1. Concatenation: Rule1 Rule2 ................................6
3.2. Alternatives: Rule1 / Rule2 ...............................6
3.3. Incremental Alternatives: Rule1 =/ Rule2 ...................7
3.4. Value Range Alternatives: %c##-## .........................7
3.5. Sequence Group: (Rule1 Rule2) .............................8
3.6. Variable Repetition: *Rule ................................8
3.7. Specific Repetition: nRule ................................9
3.8. Optional Sequence: [RULE] .................................9
3.9. Comment: ; Comment ........................................9
3.10. Operator Precedence .......................................9
4. ABNF DEFINITION OF ABNF ........................................10
5. SECURITY CONSIDERATIONS ........................................11
6. References .....................................................11
6.1. Normative References ......................................11
6.2. Informative References ....................................11
Appendix A. ACKNOWLEDGEMENTS .....................................13
Appendix B. APPENDIX - CORE ABNF OF ABNF .........................13
B.1. Core Rules ...............................................13
B.2. Common Encoding ..........................................14
1. INTRODUCTION ๐
Internet technical specs often need a formal syntax, and they're free to use whatever notation their authors think is useful ๐ Over the years, a modified version of Backus-Naur Form (BNF), called Augmented BNF (ABNF), has been popular across many Internet specs. It balances compactness and simplicity, with reasonable representational power ๐ Back in the early days of the Arpanet, every spec had its own definition of ABNF. That included the email specs, [RFC733] and then [RFC822], which ended up being the usual citations for defining ABNF ๐ฎ This document pulls those definitions apart so you can reference just what you need. Predictably, it also adds some modifications and enhancements โจ
The differences between standard BNF and ABNF involve naming rules, repetition, alternatives, order-independence, and value ranges ๐ธ Appendix B supplies rule definitions and an encoding for a core lexical analyzer of the type common to several Internet specs. It's provided as a convenience. It's otherwise separate from the meta language defined in the body of this document, and separate from its formal status ๐ง
Changes since [RFC2234]: ๐
In Section 3.7, the phrase: "That is, exactly <N> occurrences of <element>." was corrected to: "That is, exactly <n> occurrences of <element>."
Some continuation comment lines needed to be corrected to begin with comment character (";").
2. RULE DEFINITION ๐ฆ
2.1. Rule Naming ๐ท๏ธ
The name of a rule is simply the name itself. That means a sequence of characters, beginning with an alphabetic character, followed by a combination of alphabetics, digits, and hyphens (dashes) ๐
NOTE: Rule names are case-insensitive ๐
The names <rulename>, <Rulename>, <RULENAME>, and <rUlENamE> all refer to the same rule. It's giving chaotic but it's also correct ๐
Unlike original BNF, angle brackets ("<", ">") are not required. ๐ However, angle brackets may be used around a rule name whenever their presence helps tell a rule name apart. This is typically limited to rule name references in free-form prose, or to tell apart partial rules that combine into a string not separated by white space, like the repetition discussion further down ๐
2.2. Rule Form ๐
A rule is defined by the following sequence:
name = elements crlf
Here <name> is the name of the rule, <elements> is one or more rule names or terminal specifications, and <crlf> is the end-of-line indicator (carriage return followed by line feed) ๐ The equal sign separates the name from the definition of the rule. The elements form a sequence of one or more rule names and/or value definitions, combined according to the various operators defined in this document, such as alternative and repetition ๐
For visual ease, rule definitions are left aligned ๐ซถ When a rule needs multiple lines, the continuation lines are indented. The left alignment and indentation are relative to the first lines of the ABNF rules, and they don't need to match the left margin of the document โจ
2.3. Terminal Values ๐ข
Rules resolve into a string of terminal values, sometimes called characters ๐ In ABNF, a character is merely a non-negative integer. In certain contexts, a specific mapping (encoding) of values into a character set (such as ASCII) will be specified ๐
Terminals are specified by one or more numeric characters, with the base interpretation of those characters indicated explicitly. The following bases are currently defined:
b = binary
d = decimal
x = hexadecimal
Hence:
CR = %d13
CR = %x0D
respectively specify the decimal and hexadecimal representation of [US-ASCII] for carriage return. Literally the same char, just written two ways ๐
A concatenated string of such values is specified compactly, using a period (".") to indicate a separation of characters within that value. Hence:
CRLF = %d13.10
ABNF permits the specification of literal text strings directly, enclosed in quotation marks. Hence:
command = "command string"
Literal text strings are interpreted as a concatenated set of printable characters ๐
NOTE: ABNF strings are case-insensitive, and the character set for these strings is us-ascii. ๐
Hence:
rulename = "abc"
and:
rulename = "aBc"
will match "abc", "Abc", "aBc", "abC", "ABc", "aBC", "AbC", and "ABC". Wild, right? ๐ญ
To specify a rule that IS case SENSITIVE, specify the characters individually. ๐
For example:
rulename = %d97 %d98 %d99
or
rulename = %d97.98.99
will match only the string that comprises only the lowercased characters, abc ๐ง
2.4. External Encodings ๐
External representations of terminal value characters will vary according to constraints in the storage or transmission environment ๐ So the same ABNF-based grammar may have multiple external encodings. There could be one for a 7-bit US-ASCII environment, another for a binary octet environment, and still a different one when 16-bit Unicode is used. Encoding details are beyond the scope of ABNF, although Appendix A (Core) provides definitions for a 7-bit US-ASCII environment, which has been common across much of the Internet ๐ธ
By separating external encoding from the syntax, the intent is that alternate encoding environments can be used for the same syntax ๐ฆ
3. OPERATORS โ๏ธ
3.1. Concatenation: Rule1 Rule2 ๐
A rule can define a simple, ordered string of values (i.e., a concatenation of contiguous characters) by listing a sequence of rule names. For example:
foo = %x61 ; a
bar = %x62 ; b
mumble = foo bar foo
So the rule <mumble> matches the lowercase string "aba" ๐ซถ
LINEAR WHITE SPACE: Concatenation is at the core of the ABNF parsing model ๐ A string of contiguous characters (values) is parsed according to the rules defined in ABNF. For Internet specs, there's some history of permitting linear white space (space and horizontal tab) to be freely and implicitly interspersed around major constructs, such as delimiting special characters or atomic strings ๐
NOTE: This specification for ABNF does not provide for implicit specification of linear white space. ๐ โโ๏ธ
So any grammar that wants to permit linear white space around delimiters or string segments has to specify it explicitly ๐ It's often useful to provide for that white space in "core" rules, which are then used variously among higher-level rules. The "core" rules might be formed into a lexical analyzer, or they could simply be part of the main ruleset ๐
3.2. Alternatives: Rule1 / Rule2 ๐
Elements separated by a forward slash ("/") are alternatives. So:
foo / bar
will accept <foo> or <bar> ๐
NOTE: A quoted string containing alphabetic characters is a special form for specifying alternative characters. It's interpreted as a non-terminal representing the set of combinatorial strings with the contained characters, in the specified order but with any mixture of upper and lower case. ๐ง
3.3. Incremental Alternatives: Rule1 =/ Rule2 โ
Sometimes it's convenient to specify a list of alternatives in fragments ๐งฉ That means an initial rule may match one or more alternatives, with later rule definitions adding to the set of alternatives. This is especially handy for otherwise independent specs that derive from the same parent rule set, which often happens with parameter lists ๐ฆ ABNF permits this incremental definition through the construct:
oldrule =/ additional-alternatives
So the rule set:
ruleset = alt1 / alt2
ruleset =/ alt3
ruleset =/ alt4 / alt5
is the same as specifying:
ruleset = alt1 / alt2 / alt3 / alt4 / alt5
3.4. Value Range Alternatives: %c##-## ๐
okay so instead of writing out every single option one by one, you can use a dash ("-") to say "anything in this range is fair game" ๐ฆ like:
DIGIT = %x30-39
is the same as:
DIGIT = "0" / "1" / "2" / "3" / "4" / "5" / "6" /
"7" / "8" / "9"
bestie, the one thing you can't do is mix concatenated values and value ranges in the same string ๐ โโ๏ธ you pick one. a numeric value can use dots for concatenation, or a dash for one value range. so if you want one printable character sitting between end of line sequences, you'd write:
char-line = %x0D.0A %x20-7E %x0D.0A
that's giving precision ๐ โจ
3.5. Sequence Group: (Rule1 Rule2) ๐
anything wrapped in parentheses gets treated as a single element ๐ง and the stuff inside is strictly ordered, no shuffling allowed. so:
elem (foo / bar) blat
matches (elem foo blat) or (elem bar blat), but:
elem foo / bar blat
matches (elem foo) or (bar blat). see how the second one is totally different?? ๐ญ that's why parentheses are so important.
NOTE: It is strongly advised that grouping notation be used, rather than relying on the proper reading of "bare" alternations, when alternatives consist of multiple rule names or literals.
so the form you want is:
(elem foo) / (bar blat)
it stops casual readers from misreading it, which is honestly the whole point ๐ซถ the sequence group notation also shows up in free text to pull an element sequence out of the prose ๐ธ
3.6. Variable Repetition: *Rule ๐
the * in front of an element means "repeat this." the full form looks like:
<a>*<b>element
where <a> and <b> are optional decimal values, meaning at least <a> and at most <b> occurrences of the element. the defaults are 0 and infinity, so:
*<element>allows any number, including zero ๐1*<element>requires at least one ๐3*3<element>allows exactly 3 ๐ฏ1*2<element>allows one or two ๐ฆ
3.7. Specific Repetition: nRule ๐ง
a rule like:
<n>element
is equivalent to:
<n>*<n>element
so it's exactly <n> occurrences of <element> ๐
2DIGIT is a 2-digit number, and 3ALPHA is a string of three alphabetic characters. iconic ๐
3.8. Optional Sequence: [RULE] ๐
square brackets wrap an optional element sequence:
[foo bar]
and that's equivalent to:
*1(foo bar)
so it's basically "zero or one" ๐ซง
3.9. Comment: ; Comment ๐ฌ
a semi-colon starts a comment that runs to the end of the line. it's the easiest way to drop useful notes right alongside the spec, so future you (and your bestie) doesn't get lost ๐
3.10. Operator Precedence ๐
these mechanisms bind in the order below, from tightest (highest) at the top to loosest (lowest) at the bottom:
Strings, Names formation
Comment
Value range
Repetition
Grouping, Optional
Concatenation
Alternative
the alternative operator mixed freely with concatenations can get really confusing, ngl ๐ so again, it's recommended that you use the grouping operator to make your concatenation groups explicit. future-you will be so grateful ๐ซถ
4. ABNF DEFINITION OF ABNF โจ
okay this part is where ABNF defines itself ๐คฏ main character energy. two things to know before you read it:
- This syntax requires a formatting of rules that is relatively strict. so the version of a ruleset included in a specification might need preprocessing before an ABNF parser can interpret it ๐งน
- This syntax uses the rules provided in Appendix B (Core).
here's the full grammar:
rulelist = 1*( rule / (*c-wsp c-nl) )
rule = rulename defined-as elements c-nl
; continues if next line starts
; with white space
rulename = ALPHA *(ALPHA / DIGIT / "-")
defined-as = *c-wsp ("=" / "=/") *c-wsp
; basic rules definition and
; incremental alternatives
elements = alternation *c-wsp
c-wsp = WSP / (c-nl WSP)
c-nl = comment / CRLF
; comment or newline
comment = ";" *(WSP / VCHAR) CRLF
alternation = concatenation
*(*c-wsp "/" *c-wsp concatenation)
concatenation = repetition *(1*c-wsp repetition)
repetition = [repeat] element
repeat = 1*DIGIT / (*DIGIT "*" *DIGIT)
element = rulename / group / option /
char-val / num-val / prose-val
group = "(" *c-wsp alternation *c-wsp ")"
option = "[" *c-wsp alternation *c-wsp "]"
char-val = DQUOTE *(%x20-21 / %x23-7E) DQUOTE
; quoted string of SP and VCHAR
; without DQUOTE
num-val = "%" (bin-val / dec-val / hex-val)
bin-val = "b" 1*BIT
[ 1*("." 1*BIT) / ("-" 1*BIT) ]
; series of concatenated bit values
; or single ONEOF range
dec-val = "d" 1*DIGIT
[ 1*("." 1*DIGIT) / ("-" 1*DIGIT) ]
hex-val = "x" 1*HEXDIG
[ 1*("." 1*HEXDIG) / ("-" 1*HEXDIG) ]
prose-val = "<" *(%x20-3D / %x3F-7E) ">"
; bracketed string of SP and VCHAR
; without angles
; prose description, to be used as
; last resort
5. SECURITY CONSIDERATIONS ๐
okay, the document says it plainly: security is truly believed to be irrelevant to this document ๐ it's just a grammar, bestie, so no security worries here. ๐
6. References ๐
6.1. Normative References
- [US-ASCII] American National Standards Institute, "Coded Character Set -- 7-bit American Standard Code for Information Interchange", ANSI X3.4, 1986.
6.2. Informative References
- [RFC2234] Crocker, D. and P. Overell, "Augmented BNF for Syntax Specifications: ABNF", RFC 2234, November 1997.
- [RFC733] Crocker, D., Vittal, J., Pogran, K., and D. Henderson, "Standard for the format of ARPA network text messages", RFC 733, November 1977.
- [RFC822] Crocker, D., "Standard for the format of ARPA Internet text messages", STD 11, RFC 822, August 1982.
Appendix A. ACKNOWLEDGEMENTS ๐
the syntax for ABNF was first specified in RFC 733 ๐ฐ๏ธ then Ken L. Harrenstien, of SRI International, re-coded the BNF into an augmented BNF that's smaller and easier to read. slay ๐
this more recent project started as a simple effort to pull out the bit of RFC 822 that non-email spec writers kept citing, namely the description of augmented BNF. but instead of just copy-pasting it into a new doc, the working group carefully looked at the flaws and the benefits of the old spec and related specs from the last 15 years, and decided to level it up. it got way more ambitious than planned ๐ the result isn't massively different from the original, though decisions like removing the list notation came as a surprise ๐
this "separated" version was part of the DRUMS working group, with major contributions from Jerome Abela, Harald Alvestrand, Robert Elz, Roger Fajman, Aviva Garrett, Tom Harsch, Dan Kohn, Bill McQuillan, Keith Moore, Chris Newman, Pete Resnick, and Henning Schulzrinne ๐ซถ
and a special thanks to Julian Reschke for converting the Draft Standard version to XML source form ๐
Appendix B. APPENDIX - CORE ABNF OF ABNF ๐ง
this appendix is a handy core for specific grammars. the definitions can be used as a core set of rules ๐
B.1. Core Rules
certain basic rules are in uppercase, like SP, HTAB, CRLF, DIGIT, ALPHA, etc. ๐
ALPHA = %x41-5A / %x61-7A ; A-Z / a-z
BIT = "0" / "1"
CHAR = %x01-7F
; any 7-bit US-ASCII character,
; excluding NUL
CR = %x0D
; carriage return
CRLF = CR LF
; Internet standard newline
CTL = %x00-1F / %x7F
; controls
DIGIT = %x30-39
; 0-9
DQUOTE = %x22
; " (Double Quote)
HEXDIG = DIGIT / "A" / "B" / "C" / "D" / "E" / "F"
HTAB = %x09
; horizontal tab
LF = %x0A
; linefeed
LWSP = *(WSP / CRLF WSP)
; linear white space (past newline)
OCTET = %x00-FF
; 8 bits of data
SP = %x20
VCHAR = %x21-7E
; visible (printing) characters
WSP = SP / HTAB
; white space
B.2. Common Encoding ๐ธ
externally, data are represented as "network virtual ASCII" (basically 7-bit US-ASCII in an 8-bit field), with the high (8th) bit set to zero. a string of values is in "network byte order", where the higher-valued bytes sit on the left and get sent over the network first โจ
Authors' Addresses ๐๏ธ
Dave Crocker (editor)
Brandenburg InternetWorking
675 Spruce Dr.
Sunnyvale, CA 94086
US
Phone: +1.408.246.8253
EMail: dcrocker@bbiw.net
Paul Overell
THUS plc.
1/2 Berkeley Square
99 Berkeley Street
Glasgow
G3 7HR
UK
EMail: paul.overell@thus.net
Full Copyright Statement โ๏ธ
Copyright (C) The Internet Society (2005).
this document is subject to the rights, licenses and restrictions contained in BCP 78, and except as set forth therein, the authors retain all their rights.
the document and the information in it are provided on an "AS IS" basis, and the disclaimer below is quoted exactly as written ๐
THE CONTRIBUTOR, THE ORGANIZATION HE/SHE REPRESENTS OR IS SPONSORED BY (IF ANY), THE INTERNET SOCIETY AND THE INTERNET ENGINEERING TASK FORCE DISCLAIM ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
Intellectual Property โจ
okay bestie, let's talk about the legal stuff, but make it cute ๐ The IETF takes no position on whether any Intellectual Property Rights (or other rights) that someone claims over the technology in this document are valid, or how far they reach. It also doesn't say whether you can get a license for them, and it isn't promising it went hunting for any of those rights either ๐ซถ If you want the details on how rights work for RFC documents, check out BCP 78 and BCP 79 ๐
If you're looking for IPR disclosures that were sent to the IETF Secretariat, or assurances that licenses will be available, or the results of anyone trying to get a general license or permission for implementers or users of this spec, you can find it all in the IETF online IPR repository at http://www.ietf.org/ipr ๐
And the IETF genuinely wants to hear from you! ๐ฆ If you know about any copyrights, patents, patent applications, or other proprietary rights that might cover technology you'd need to implement this standard, please tell the IETF. Send it to ietf-ipr@ietf.org and they'll take it from there ๐
Acknowledgement ๐ธ
Shoutout to the Internet Society, who currently fund the RFC Editor function ๐ Literally the reason these documents get published! ๐
๐ฆ where this page comes from
- ietf: rfc4234 2005-10
- ๐ girlified by claude-haiku-5-5 on : a language model's rewrite of the original, under its terms