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 that 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 specifications, there is 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.
3.2. Alternatives: Rule1 / Rule2
Elements separated by a forward slash ("/") are alternatives. Therefore:
foo / bar
will accept <foo> or <bar>.
3.3. Incremental Alternatives: Rule1 =/ Rule2
It is sometimes convenient to specify a list of alternatives in fragments. That is, an initial rule may match one or more alternatives, with additional alternatives defined later. This is particularly useful for otherwise independent specifications that derive from the same parent rule set, such as often occurs with parameter lists. ABNF permits this incremental definition through the construct:
oldrule =/ additional-alternatives
3.4. Value Range Alternatives: %c##-##
A range of alternative numeric values can be specified compactly, using a dash ("-") to indicate the range of alternative values. Hence:
DIGIT = %x30-39
is equivalent to:
DIGIT = "0" / "1" / "2" / "3" / "4" / "5" / "6" / "7" / "8" / "9"
3.5. Sequence Group: (Rule1 Rule2)
Elements enclosed in parentheses are treated as a single element, whose contents are strictly ordered. Thus:
elem (foo / bar) blat
matches (elem foo blat) or (elem bar blat).
elem foo / bar blat
matches (elem foo) or (bar blat).
3.6. Variable Repetition: *Rule
The operator "*" preceding an element indicates repetition. The full form is:
<a>*<b>element
where <a> and <b> are optional decimal values, indicating at least <a> and at most <b> occurrences of the element.
Default values are 0 and infinity so that *<element> allows any number, including zero; 1*<element> requires at least one; 3*3<element> allows exactly 3; and 1*2<element> allows one or two.
3.7. Specific Repetition: nRule
A rule of the form:
<n>element
is equivalent to:
<n>*<n>element
That is, exactly <n> occurrences of <element>. Thus, 2DIGIT is a 2-digit number, and 3ALPHA is a string of three alphabetic characters.
3.8. Optional Sequence: [RULE]
Square brackets enclose an optional element sequence:
[foo bar]
is equivalent to:
*1(foo bar)
3.9. Comment: ; Comment
A semicolon starts a comment that continues to the end of line. This is a simple way of including useful notes in parallel with the specifications.
3.10. Operator Precedence
The various mechanisms described above have the following precedence, from highest (binding tightest) at the top, to lowest (loosest) at the bottom:
- Rule name, prose-val, Terminal value
- Comment
- Value range
- Repetition
- Grouping, optional
- Concatenation
- Alternative
Use of the alternative operator, freely mixed with concatenations, can be confusing. It is recommended that the grouping operator be used to make explicit concatenation groups.