Question

3.8. Optional Sequence:
[RULE]

   Square brackets enclose an optional element sequence:

         [foo bar]

   is equivalent to

         *1(foo bar).

The above section from RFC5234 seems not correct to me.

I think this is because the optional sequence rule [foo bar] is not only equivalent to 1*1(foo bar), but also equivalent to 1*1(bar foo). And the above example matches with default value 0, that is 0*1(foo bar).

However, [] usually means something else. So on the other hand, I think [foo bar] should mean either (foo) or (bar).

Can anyone clear this confusion for me?

Was it helpful?

Solution

The RFC defines the syntax and semantics of ABNF grammars and the quoted text defines the semantics of optional sequence syntax. It is correct by definition. Parentheses in ABNF form sequence groups, (foo bar) means foo immediately followed by bar. The number syntax in front indicates repetition, where the asterisk separates minimum number of occurences from maximum number of occurences. The minimum defaults to zero. So

*1(foo bar)

is the same as

0*1(foo bar)

meaning a sequence of foo immediately followed by bar that appears at least zero and at most one time, i.e., the sequence is optional. Since optional parts are quite frequent in formal grammars, there is a special shorthand syntax for them, namely

[foo bar]

which also means a sequence of foo immediately followed by bar that appears at least zero and at most one time. What syntactic constructs usually mean does not matter here, the specification is not reflecting on the world, it defines its own conventions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top