Tangled web



Sunday, December 31, 2006

A List Apart: Articles: Learn to Read BNF
 
A List Apart: Articles: How to Read W3C Specs

BNF stands for Backus Naur Form, or Backus Normal Form. It’s a compact way to represent the grammar of computer languages, and it’s been around, well, forever. Different specifications use different flavors of BNF, but they all translate long English descriptions into symbolic form. Take this example of what constitutes a sandwich:

A sandwich consists of a lower slice of bread, mustard or mayonnaise; optional lettuce, an optional slice of tomato; two to four slices of either bologna, salami, or ham (in any combination); one or more slices of cheese, and a top slice of bread.

This translates to:

sandwich ::=
lower_slice
[ mustard | mayonnaise ]
lettuce? tomato?
[ bologna | salami | ham ] {2,4}
cheese+
top_slice

The constituent parts of a definition are listed in order, separated by blanks. Items are grouped with square brackets, and choices within a group are separated by a vertical bar.

If an item is followed by a question mark, that means “one or none;” if followed by a plus sign, that means “one or more;” if followed by an asterisk, that means “zero or more;” and if followed by numbers inside braces, it gives the lower and upper limits for how many times an item can occur.

Parentheses, or more square brackets, are used to group items in more complex definitions. Sometimes a generic item (like a “color”) is enclosed in < and >, or fixed items will be enclosed in quote marks."