RFC 2234

Augmented BNF for Syntax Specifications: ABNF

πŸ“… published November 1997, from ietf

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 (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

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