RFC 4234

Augmented BNF for Syntax Specifications: ABNF

๐Ÿ“… published October 2005, from ietf

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

  1. 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 ๐Ÿงน
  2. 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

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