Backus-Naur Form (BNF)

The standard technique for defining the syntax of a computer language is to use Backus-Naur Form (BNF). The variant used in this tutorial is as follows:

  1. Words inside double quotes ("word") represent literal words themselves (these are called terminals). The phrase double_quote is used to represent the double-quote character itself. For example: "if".
  2. Words outside double quotes (possibly with underscores) represent syntactic categories (i.e. nonterminals). For example: if_statement.
  3. Syntactic categories are defined using the form:
    syntactic_category ::= definition
  4. Square brackets ([]) surround optional items.
  5. Curly brackets ({}) surround items that can repeat zero or more times.
  6. A vertical line (|) separates alternatives.

This BNF variant is intentionally very similar to the form used in the Ada 94 Reference Manual. The main difference is that terminals (such as keywords) are represented inside quotes instead of being bold, since not all WWW viewers can accurately represent bold characters.

As an example, here's the Ada syntax of the if_statement:

if_statement ::=
  "if" condition "then"
     sequence_of_statements
  {"elsif" condition "then"
     sequence_of_statements}
  ["else" 
     sequence_of_statements]
  "end if;"

And here are some examples of constructs that are legal if_statements:

Example 1: Zero "elsif" repetitions and no optional "else" phrase:

 if A=B then
    A := B + 1;
 end if;

Example 2: Zero "elsif" repetitions and has the optional "else" phrase:

 if A=B then
    A := B + 1;
 else
    A := B;
 end if;

Example 3: Two "elsif" repetitions, no the optional "else" phrase:

 if A=B then
    A := B + 1;
 elsif A=C then
    A := C + 1;
 elsif A=D then
    A := D + 1;
 end if;

Members of the Database Research Group at the University of Geneva have produced a very nice set of documents describing the syntax rules of Ada 95 illustrated by syntactic diagrams, with cross-references.

Another source for Ada syntax is a very nice set of Ada reference cards by Paul Pukite.

You may return to the Lovelace home page.

David A. Wheeler (dwheeler@dwheeler.com)