BNF Notation
Alternatives |, repetition *, optional ?
BNF (Backus-Naur Form, Definition 7.2.8, notes p. 138) is the practical
shorthand everyone actually uses to write context-free grammars. It was invented
to specify the syntax of ALGOL 60 and is now universal: every programming
language reference uses BNF.
BNF adds five convenience constructors to plain production rules:
1. Alternatives — choose one of the
. This is shorthand for rules sharing a head.
2. Repetition — zero or more 's. means one or more.
3. Optional — zero or one .
4. Grouping — usually for repetition.
5. Character sets — all characters with .
6. Complements — all characters except those.
All of these can be eliminated by introducing auxiliary nonterminals and
extra rules. So BNF is purely a notational convenience — it adds zero expressive
power. For example, expands to:
-
-
-
And expands to:
-
-
-
BNF is the lingua franca of compiler writers and language designers.