Backus-Naur Form
(Content adapted from Critchlow & Eck)
Context-free grammars are used to describe some aspects of the syntax of programming languages. However, a notation that is often used for grammars in the context of programming languages is somewhat different from the notation introduced in the preceding section. The notation that is used is called Backus-Naur Form or BNF. It is named after computer scientists John Backus and Peter Naur, who developed the notation. Actually, several variations of BNF exist. I will discuss one of them here. BNF can be used to describe the syntax of natural languages, as well as programming languages, and some of the examples in this section will deal with the syntax of English.
Like context-free grammars, BNF grammars make use of production rules, non-terminals, and terminals. The non-terminals are usually given meaningful, multi-character names. Here, I will follow a common practice of enclosing non-terminals in angle brackets, so that they can be easily distinguished. For example, and could be non-terminals in a BNF grammar for English, while and might be used in a BNF grammar for a programming language. Note that a BNF non-terminal usually represents a meaningful syntactic category, that is, a certain type of building block in the syntax of the language that is being described, such as an adverb, a prepositional phrase, or a variable declaration statement. The terminals of a BNF grammar are the things that actually appear in the language that is being described. In the case of natural language, the terminals are individual words.
In BNF production rules, I will use the symbol "::=" in place of the "" that is used in context-free grammars. BNF production rules are more powerful than the production rules in context-free grammars. That is, one BNF rule might be equivalent to several context-free grammar rules. As for context-free grammars, the left-hand side of a BNF production rule is a single non-terminal symbol. The right hand side can include terminals and non-terminals, and can also use the following notations, which should remind you of notations used in regular expressions:
- A vertical bar, , indicates a choice of alternatives. For example,
indicates that the non-terminal can be replaced by any one of the terminal symbols 0, 1, \dots, 9.
- Items enclosed in brackets are optional. For example,
says that can be replaced either by "" or by "". (The symbols "=" and ";" are terminal symbols in this rule.)
- Items enclosed between "" and "" can be repeated zero or more times. (This has the same effect as a ""in a regular expression.) For example,
says that an consists of a followed optionally by any number of additional 's. That is, the non-terminal can be replaced by or by or by , and so on.
- Parentheses can be used as usual, for grouping.
All these notations can be expressed in a context-free grammar by introducing additional production rules. For example, the BNF rule "" is equivalent to the two rules, "" and "". A rule that contains an optional item can also be replaced by two rules. For example,
can be replaced by the two rules
In context-free grammars, repetition can be expressed by using a recursive rule such as "", in which the same non-terminal symbol appears both on the left-hand side and on the right-hand side of the rule. BNF-style notation using "" and "" can be eliminated by replacing it with a new non-terminal symbol and adding a recursive rule to allow that symbol to repeat zero or more times. For example, the production rule
can be replaced by three rules using a new non-terminal symbol