RFC 2234
Augmented BNF for Syntax Specifications: ABNF
RFC 2234 β¨ Augmented BNF for Syntax Specifications: ABNF
okay bestie, this one is the RFC that teaches you how to write the rules for other RFCs π€π. It came out in November 1997 as part of the Network Working Group, and it's a Standards Track doc. The authors are D. Crocker, Ed. from the Internet Mail Consortium and P. Overell from Demon Internet Ltd. π The title block doesn't list any Obsoletes or Updates line, so it isn't replacing or patching anything else. The copyright belongs to The Internet Society (1997) πΈ
Status of this Memo π£
so this doc is an Internet standards track protocol for the Internet community, and it's basically asking everyone for discussion and suggestions for improvements π«Ά for the official standardization state and status of this protocol, check the current edition of the "Internet Official Protocol Standards" (STD 1) π. Distribution of this memo is unlimited π
Copyright Notice Β©οΈ
Copyright (C) The Internet Society (1997). All Rights Reserved. π
TABLE OF CONTENTS π
1. INTRODUCTION .................................................. 2
2. RULE DEFINITION ............................................... 2
2.1 RULE NAMING .................................................. 2
2.2 RULE FORM .................................................... 3
2.3 TERMINAL VALUES .............................................. 3
2.4 EXTERNAL ENCODINGS ........................................... 5
3. OPERATORS ..................................................... 5
3.1 CONCATENATION RULE1 RULE2 ............................. 5
3.2 ALTERNATIVES RULE1 / RULE2 ................................... 6
3.3 INCREMENTAL ALTERNATIVES RULE1 =/ RULE2 .................... 6
3.4 VALUE RANGE ALTERNATIVES %C##-## ........................... 7
3.5 SEQUENCE GROUP (RULE1 RULE2) ................................. 7
3.6 VARIABLE REPETITION *RULE .................................... 8
3.7 SPECIFIC REPETITION NRULE .................................... 8
3.8 OPTIONAL SEQUENCE [RULE] ..................................... 8
3.9 ; COMMENT .................................................... 8
3.10 OPERATOR PRECEDENCE ......................................... 9
4. ABNF DEFINITION OF ABNF ....................................... 9
5. SECURITY CONSIDERATIONS ....................................... 10
6. APPENDIX A - CORE ............................................. 11
6.1 CORE RULES ................................................... 11
6.2 COMMON ENCODING .............................................. 12
7. ACKNOWLEDGMENTS ............................................... 12
8. REFERENCES .................................................... 13
9. CONTACT ....................................................... 13
10. FULL COPYRIGHT STATEMENT ..................................... 14
1. INTRODUCTION β¨
okay so Internet technical specs need a way to describe what their message formats look like, and they're totally free to use whatever notation the authors think works π§. Over time, a modified version of Backus-Naur Form (BNF) called Augmented BNF (ABNF) became super popular across lots of Internet specs π₯. It's giving compact and simple, while still being pretty expressive π
Back in the early Arpanet days, every spec carried its own version of ABNF π. That includes the email specs RFC733 and then RFC822, which are the go-to citations people use when they need to define ABNF. This doc pulls that definition out into its own thing so you can reference just the part you need π¦. It also adds some tweaks and upgrades, because of course it does π
ABNF is different from standard BNF in a few key ways: naming rules, repetition, alternatives, order-independence, and value ranges πΈ. Appendix A (Core) gives you rule definitions and encodings for a core lexical analyzer that a bunch of Internet specs share π. It's there as a convenience, and it's separate from the meta language in the body of this doc, and separate from its formal status too π«Ά
2. RULE DEFINITION π
2.1 Rule Naming
A rule's name is just the name itself: a sequence of characters that starts with a letter and then keeps going with letters, digits, and hyphens (dashes) π
NOTE: Rule names are case-insensitive
so <rulename>, <Rulename>, <RULENAME>, and <rUlENamE> all point to the same rule π
Unlike original BNF, you don't need angle brackets ("<", ">") π But you can put them around a rule name whenever they help make it clear which part is the rule name. That mostly comes up in free-form prose, or when you're separating partial rules that get mashed together into one string with no white space between them, like in the repetition part coming up below β¨
2.2 Rule Form
A rule looks like this:
name = elements crlf
where name is the name of the rule, elements is one or more rule names or terminal specifications, and crlf is the end-of-line marker, which is a carriage return followed by a line feed π« The equal sign is what splits the name from the definition. The elements make up a sequence of one or more rule names and/or value definitions, put together with the operators this doc defines, like alternatives and repetition π
For readability, rule definitions line up on the left π When one rule takes up several lines, the continuation lines get indented. That left alignment and indentation are measured against the first lines of the ABNF rules, so they don't have to match the document's left margin π
2.3 Terminal Values
Rules boil down to a string of terminal values, which people sometimes just call characters π¦ In ABNF, a character is basically just a non-negative integer. In some contexts, a specific mapping (encoding) of values onto a character set (like ASCII) will be spelled out π§
Terminals are written with one or more numeric characters, and you say explicitly which base those numbers are in π These are the bases defined right now:
b = binary
d = decimal
x = hexadecimal
so, hot take: CR = %d13 and CR = %x0D are the exact same thing, just written in decimal and hex. They both point to the carriage return in [US-ASCII] π
If you want a bunch of these values in a row, you can squish them together with a period (".") between each one β¨ For example:
CRLF = %d13.10
ABNF also lets you write a literal text string straight out, wrapped in quotation-marks πΈ For example:
command = "command string"
Literal text strings get read as a run of printable characters π«Ά
NOTE: ABNF strings are case-insensitive and the character set for these strings is us-ascii.
Check this out π:
rulename = "abc"
and:
rulename = "aBc"
will both match "abc", "Abc", "aBc", "abC", "ABc", "aBC", "AbC", and "ABC" π (yes, all eight of them, I'm obsessed)
To specify a rule which IS case SENSITIVE, specify the characters individually.
For example, this:
rulename = %d97 %d98 %d99
or this:
rulename = %d97.98.99
will match only the string made up entirely of lowercased characters, abc ππ
to be continued in Part 2 πβ¨
2.4. External Encodings β¨
okay so the way ABNF handles characters is kinda chill π«Ά the same grammar can be written out in different ways depending on where it's going. a 7-bit US-ASCII setup, a plain binary octet setup, or 16-bit Unicode can all wear the same syntax in their own outfit π the encoding details are not ABNF's job tho. Appendix A (Core) does give you definitions for the 7-bit US-ASCII world, since that's been the Internet's go-to for ages πΈ
the whole point is keeping encoding separate from syntax, so the same syntax can totally run in different encoding environments π¦ iconic separation of concerns, no notes π
3. OPERATORS β¨
3.1. Concatenation Rule1 Rule2
a rule can make a simple, ordered string of values (basically a run of contiguous characters) just by listing rule names one after another π like this:
foo = %x61 ; a
bar = %x62 ; b
mumble = foo bar foo
so the rule <mumble> matches the lowercase string "aba" β¨
LINEAR WHITE SPACE: concatenation is the core of the ABNF parsing model π§ a string of contiguous characters gets parsed by the rules you defined. internet specs have a bit of history of letting linear white space (space and horizontal tab) just kinda float around between the big constructs, like around delimiters or atomic strings π NOTE: the ABNF spec here does not give you implicit linear white space. not happening, bestie π ββοΈ
so any grammar that wants white space around delimiters or string pieces has to spell it out explicitly π it's also super useful to put that white space in "core" rules and then reuse them across your higher-level rules π those core rules can be a lexical analyzer or just live in the main ruleset, whatever's giving you life π
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 with alphabetic characters is a special form for saying "these alternative characters." it's treated as a non-terminal that stands for the set of combinations of those characters, in the order given, but with any mix of upper and lower case π
3.3. Incremental Alternatives Rule1 =/ Rule2
sometimes it's easier to build up a list of alternatives in pieces π§© an initial rule can match one or more alternatives, and later definitions keep adding to that set. this is especially handy for specs that are kinda independent but come from the same parent ruleset, like parameter lists that keep growing πΈ ABNF lets you do this with:
oldrule =/ additional-alternatives
so this ruleset
ruleset = alt1 / alt2
ruleset =/ alt3
ruleset =/ alt4 / alt5
is exactly the same as writing
ruleset = alt1 / alt2 / alt3 / alt4 / alt5
slay the compact version π
3.4. Value Range Alternatives %c##-##
you can squish a range of alternative numeric values together by using a dash ("-") to show the range π so
DIGIT = %x30-39
is the same as
DIGIT = "0" / "1" / "2" / "3" / "4" / "5" / "6" /
"7" / "8" / "9"
one thing though: you can't mix concatenated numeric values and numeric value ranges in the same string π ββοΈ a numeric value can use the dotted notation for concatenation, or the dash notation for one value range, but not both π so if you want one printable character sitting between end-of-line sequences, it'd look like:
char-line = %x0D.0A %x20-7E %x0D.0A
3.5. Sequence Group (Rule1 Rule2)
elements inside parentheses get treated as one single element, and their contents are STRICTLY ORDERED π₯ so
elem (foo / bar) blat
matches (elem foo blat) or (elem bar blat) π
elem foo / bar blat
matches (elem foo) or (bar blat) π see how the bare slash is messing with the grouping? main character chaos π
NOTE: it is strongly advised to use grouping notation instead of relying on people reading "bare" alternations correctly, especially when the alternatives are made of multiple rule names or literals π«Ά
so instead of the form above, it's recommended to write:
(elem foo) / (bar blat)
that way casual readers won't misread it π
the sequence group notation is also used in free text to set off a sequence of elements from the surrounding prose β¨
3.6. Variable Repetition *Rule
the "*" operator in front of an element means repetition π the full form is:
<a>*<b>element
where <a> and <b> are optional decimal values, saying the element shows up at least <a> times and at most <b> times π¦
the defaults are 0 and infinity, so:
*<element>allows any number, zero included πΈ1*<element>requires at least one π3*3<element>allows exactly three π1*2<element>allows one or two π
3.7. Specific Repetition nRule
a rule shaped like this:
<n>element
is equivalent to
<n>*<n>element
which means exactly <N> occurrences of <element> π§ so 2DIGIT is a 2-digit number, and 3ALPHA is a string of three alphabetic characters π
3.8. Optional Sequence [RULE]
square brackets wrap an optional element sequence π
[foo bar]
is equivalent to
*1(foo bar)
3.9. ; Comment
a semi-colon starts a comment that runs to the end of the line π super useful for leaving notes right next to the specs π
3.10. Operator Precedence
the mechanisms we just covered have a pecking order, from the highest (binds tightest) at the top down to the lowest and loosest at the bottom π
Strings, Names formation
Comment
Value range
Repetition
Grouping, Optional
Concatenation
Alternative
mixing the alternative operator freely with concatenations can get really confusing π΅βπ« so again, use the grouping operator to make your concatenation groups explicit π«Ά
4. ABNF DEFINITION OF ABNF β¨
this syntax is built on the rules in Appendix A (Core) π
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 β¨
Security is truly believed to be irrelevant to this document. π (no notes, no cap, the spec said it, we respect it π)
6. APPENDIX A - CORE β¨
this appendix is a handy core for specific grammars π§ the definitions can be used as a core set of rules π
6.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
; space
VCHAR = %x21-7E
; visible (printing) characters
WSP = SP / HTAB
; white space
6.2. Common Encoding π
okay so externally, the data gets wrapped up as "network virtual ASCII" π which is basically 7-bit US-ASCII sitting inside an 8-bit field, with the high (8th) bit set to zero π¦ no extra bit flex needed. when you have a string of values, they go in "network byte order" π§, meaning the higher-valued bytes sit on the left and get sent over the network first. literally the most organized little line-up ever πβ¨
7. ACKNOWLEDGMENTS πΈ
the ABNF syntax was originally specified in RFC 733 π and Ken L. Harrenstien, of SRI International, was the queen who recoded the BNF into augmented BNF, making it smaller and way easier to understand π
this whole project started as a simple little glow-up π§ββοΈ the plan was to cull out the bit of RFC 822 that non-email spec writers kept citing over and over, which is the augmented BNF description. but instead of just copy-pasting that text into a new doc, the working group went full detective mode π and carefully looked at the flaws and the perks of the existing spec and its related specs from the last 15 years π and yeah, it turned into something way more ambitious than anyone expected π the end result isn't massively different from the original, but decisions like removing the list notation shocked everyone π
the current round of this spec was part of the DRUMS working group π«Ά with huge 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. the whole squad is iconic πβ¨
8. REFERENCES π
[US-ASCII] Coded Character Set--7-Bit American Standard Code for Information Interchange, ANSI X3.4-1986.
[RFC733] Crocker, D., Vittal, J., Pogran, K., and D. Henderson, "Standard for the Format of ARPA Network Text Message," RFC 733, November 1977.
[RFC822] Crocker, D., "Standard for the Format of ARPA Internet Text Messages", STD 11, RFC 822, August 1982.
9. CONTACT π
the people to ping if you want to reach out π¦ here's the whole contact card, no cap:
David H. Crocker Paul Overell
Internet Mail Consortium Demon Internet Ltd
675 Spruce Dr. Dorking Business Park
Sunnyvale, CA 94086 USA Dorking
Surrey, RH4 1HN
UK
Phone: +1 408 246 8253
Fax: +1 408 249 6205
EMail: dcrocker@imc.org paulo@turnpike.com
10. Full Copyright Statement π₯
copyright is held by The Internet Society (1997) β¨ All Rights Reserved. here's the legal stuff, kept exactly as it reads so nothing gets lost in translation π«Ά
Copyright (C) The Internet Society (1997). All Rights Reserved. This document and translations of it may be copied and furnished to others, and derivative works that comment on or otherwise explain it or assist in its implementation may be prepared, copied, published and distributed, in whole or in part, without restriction of any kind, provided that the above copyright notice and this paragraph are included on all such copies and derivative works. However, this document itself may not be modified in any way, such as by removing the copyright notice or references to the Internet Society or other Internet organizations, except as needed for the purpose of developing Internet standards in which case the procedures for copyrights defined in the Internet Standards process must be followed, or as required to translate it into languages other than English. The limited permissions granted above are perpetual and will not be revoked by the Internet Society or its successors or assigns. This document and the information contained herein is provided on an "AS IS" basis and THE INTERNET SOCIETY AND THE INTERNET ENGINEERING TASK FORCE DISCLAIMS 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.
π¦ where this page comes from
- ietf: rfc2234 1997-11
- π girlified by claude-haiku-5-5 on : a language model's rewrite of the original, under its terms