RFC 5234 - Augmented BNF for Syntax Specifications: ABNF
Published: January 2008
Status: Internet Standard (STD 68)
Authors: D. Crocker (Brandenburg InternetWorking), P. Overell (THUS plc.)
Obsoletes: RFC 4234
Abstract
Internet technical specifications often need to define a formal syntax. Over the years, a modified version of Backus-Naur Form (BNF), called Augmented BNF (ABNF), has been popular among many Internet specifications. The current specification documents ABNF. It balances compactness and simplicity with reasonable representational power. The differences between standard BNF and ABNF involve naming rules, repetition, alternatives, order-independence, and value ranges. This specification also supplies additional rule definitions and encoding for a core lexical analyzer of the type common to several Internet specifications.
Status of This Memo
This document specifies an Internet standards track protocol for the Internet community, and requests discussion and suggestions for improvements. Please refer to the current edition of the "Internet Official Protocol Standards" (STD 1) for the standardization state and status of this protocol. Distribution of this memo is unlimited.
Table of Contents
- 1. Introduction
- 2. Rule Definition
- 2.1. Rule Naming
- 2.2. Rule Form
- 2.3. Terminal Values
- 2.4. External Encodings
- 3. Operators
- 3.1. Concatenation
- 3.2. Alternatives
- 3.3. Incremental Alternatives
- 3.4. Value Range Alternatives
- 3.5. Sequence Group
- 3.6. Variable Repetition
- 3.7. Specific Repetition
- 3.8. Optional Sequence
- 3.9. Comment
- 3.10. Operator Precedence
- 4. ABNF Definition of ABNF
- 5. Security Considerations
- 6. References
- 6.1. Normative References
- 6.2. Informative References
Appendices
- Appendix A. Acknowledgements
- Appendix B. Core ABNF of ABNF
- B.1. Core Rules
- B.2. Common Encoding
ABNF Core Concepts
What is ABNF?
ABNF (Augmented BNF) is a metalanguage used to define syntax, widely used in Internet protocol specifications. It is an extension of the classic Backus-Naur Form (BNF).
Basic Syntax
Rule Definition:
rulename = elements
Example:
postal-address = name-part street zip-part
name-part = *(personal-part SP) last-name [SP suffix] CRLF
Core Operators
| Operator | Syntax | Description | Example |
|---|---|---|---|
| Concatenation | Rule1 Rule2 | Sequential concatenation | "http" "://" |
| Alternatives | Rule1 / Rule2 | Choose one | "yes" / "no" |
| Incremental Alt | Rule1 =/ Rule2 | Add alternative | digit =/ "A" |
| Value Range | %c##-## | Character range | %x30-39 (0-9) |
| Optional | [Rule] | Zero or one | ["s"] |
| Repetition | *Rule | Zero or more | *DIGIT |
| Specific Rep | nRule | Exactly n times | 3DIGIT |
| Range Rep | n*mRule | n to m times | 1*3DIGIT |
| Grouping | (Rule1 Rule2) | Group rules | ("http" / "https") |
| Comment | ; comment | Comment | ; This is a comment |
Core Rules
ABNF defines a set of core rules commonly used in various specifications:
ALPHA = %x41-5A / %x61-7A ; A-Z / a-z
DIGIT = %x30-39 ; 0-9
HEXDIG = DIGIT / "A" / "B" / "C" / "D" / "E" / "F"
DQUOTE = %x22 ; " (double quote)
SP = %x20 ; space
HTAB = %x09 ; horizontal tab
WSP = SP / HTAB ; whitespace
LWSP = *(WSP / CRLF WSP) ; linear whitespace
VCHAR = %x21-7E ; visible (printing) characters
CHAR = %x01-7F ; any 7-bit US-ASCII character
OCTET = %x00-FF ; 8-bit data
CTL = %x00-1F / %x7F ; control characters
CR = %x0D ; carriage return
LF = %x0A ; line feed
CRLF = CR LF ; carriage return line feed
BIT = "0" / "1"
Practical Examples
Example 1: Simple Rule
greeting = "Hello" SP name
name = 1*ALPHA
Matches: Hello John, Hello Alice
Example 2: Email Address (Simplified)
email = local-part "@" domain
local-part = 1*(ALPHA / DIGIT / "." / "-" / "_")
domain = subdomain *("." subdomain)
subdomain = 1*(ALPHA / DIGIT / "-")
Matches: [email protected], [email protected]
Example 3: HTTP Version
HTTP-version = "HTTP/" 1*DIGIT "." 1*DIGIT
Matches: HTTP/1.1, HTTP/2.0
Example 4: URI Scheme
scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
Matches: http, https, ftp, mailto
Repetition Operators Explained
| Notation | Meaning | Example | Matches |
|---|---|---|---|
*Rule | 0 or more | *DIGIT | "", "1", "123" |
1*Rule | 1 or more | 1*DIGIT | "1", "123" |
2*4Rule | 2 to 4 times | 2*4DIGIT | "12", "123", "1234" |
3Rule | Exactly 3 times | 3DIGIT | "123" |
[Rule] | 0 or 1 time | ["s"] | "", "s" |
Operator Precedence
From highest to lowest:
- Rule names, groups -
rulename,(group) - Repetition -
*,n*m,n - Value range, strings -
%c##-##,"string" - Concatenation -
Rule1 Rule2 - Alternatives -
Rule1 / Rule2
Example:
foo = "a" *("b" / "c")
Matches: a, ab, ac, abc, abb, acc, abbc, ...
Terminal Value Representation
Strings
command = "GET" / "POST"
Numeric Values (Hexadecimal)
CR = %x0D ; carriage return
LF = %x0A ; line feed
Numeric Ranges
DIGIT = %x30-39 ; 0-9
ALPHA = %x41-5A / %x61-7A ; A-Z / a-z
Sequence Values
CRLF = %x0D.0A ; carriage return line feed sequence
ABNF vs BNF Comparison
| Feature | BNF | ABNF |
|---|---|---|
| Rule definition | ::= | = |
| Alternatives | ` | ` |
| Grouping | <> | () |
| Optional | - | [] |
| Repetition | - | *, n*m |
| Terminal values | Quotes | Quotes + numeric |
| Comments | Varies | ; |
Practical Applications
ABNF is widely used to define:
- HTTP (RFC 7230, 7231, etc.)
- URI (RFC 3986)
- SMTP (RFC 5321)
- SIP (RFC 3261)
- JSON (RFC 7159)
- Nearly all IETF protocols
Related Resources
- Official Text: ````https://www.rfc-editor.org/rfc/rfc5234.txt\````
- Official Page: ````https://datatracker.ietf.org/doc/html/rfc5234\````
- Standard: STD 68
- Obsoletes: RFC 4234
- Related RFCs:
- RFC 2234 (Previous version of ABNF)
- RFC 7405 (Case-Sensitive String Support in ABNF)
Important Note: ABNF is a fundamental tool for Internet protocol specifications. Understanding ABNF is crucial for reading and implementing RFC specifications!