AQA 7517, section 4.4.3.1
Backus-Naur Form (BNF) is a notation for defining the syntax of a language precisely. It is used to describe programming languages, so that compilers know which programs are valid, and to describe data formats.
A language defined this way is called a context-free language.
Production rules
BNF is made of production rules. Each rule says what a non-terminal can be replaced by.
<digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
<digit>is a non-terminal: a name for something that is defined by further rules. It is written in angle brackets.::=means "is defined as".|means "or".0,1, ... are terminals: actual symbols that appear in the language and are not replaced.
Rules can refer to other rules, and can contain a sequence of items:
<sign> ::= + | -
<number> ::= <sign><digit> | <digit>
This says a <number> is either a sign followed by a digit, or just a digit. So 7, +7 and -3 are valid, but ++7 and 73 are not.
Recursion: repeating things
BNF has no *, so repetition is done with recursion: a rule that refers to itself.
<digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
<integer> ::= <digit> | <digit><integer>
An <integer> is a digit, or a digit followed by another integer. So the rule can be applied again and again, and 4, 42 and 2026 are all valid integers.
Checking that a string is valid
Show that 42 is an <integer>:
42matches<digit><integer>, with4as the<digit>and2as the<integer>.2is a<digit>, so it is a valid<integer>.- So
42is a valid<integer>.
A string such as 4a is not valid, because a is not a <digit>.
A larger example: arithmetic
<expression> ::= <term> | <expression> + <term>
<term> ::= <digit> | ( <expression> )
Using this, 3, 3+4, 3+4+5 and (3+4)+5 are valid expressions. 3+ is not, because a + must be followed by a <term>.
Syntax diagrams
A syntax diagram shows the same information as a picture. You start at the left and follow the arrows through the boxes, which are non-terminals (or terminals in circles).
An <integer> is one or more digits:
The loop back shows that a digit can be repeated. A path that goes straight through takes the digit once. A path that goes round the loop takes it again.
You can use either a diagram or BNF to check whether a string follows the rules. Follow the path that matches each character in turn. If you can reach the end, the string is valid.
Why BNF can do more than regular expressions
Regular expressions (and FSMs) can't describe every language, because they can't count or nest without limit.
BNF can, because rules can be recursive. For example, take the language that an FSM can't accept:
<s> ::= 01 | 0<s>1
This generates 01, then 0011, then 000111, and so on. Each time the rule 0<s>1 is used, one 0 is added at the front and one 1 at the end, so the counts always match.
The same idea lets BNF describe matching brackets and nested structures, such as (a + (b * c)), that regular expressions can't. Programming languages are full of nesting (brackets, blocks inside blocks), so they need BNF, not just regular expressions.
| Regular expressions | BNF | |
|---|---|---|
| Repetition | * and + |
Recursion |
| Can match nested or balanced structures | No | Yes |
| Language type | Regular | Context-free |
| Typical use | Pattern matching in text | Defining language syntax |
Every regular language can also be written in BNF, but BNF can describe more.
Worked example
Task: write BNF for a simple variable name: a letter, followed by any number of letters or digits. Then check that x2 is valid and 2x is not.
<letter> ::= a | b | c | x | y | z
<digit> ::= 0 | 1 | 2 | 3
<char> ::= <letter> | <digit>
<rest> ::= <char> | <char><rest>
<variable> ::= <letter> | <letter><rest>
(Only a few letters and digits are listed, to keep it short.)
x2:xis a<letter>, and the rest2is a<char>(a<digit>), so it matches<letter><rest>. Valid.2x: the first character2must be a<letter>, but it is a digit. Not valid.
Exam tip: when you're formulating production rules, use recursion for anything that can repeat. When explaining why BNF is more powerful than regular expressions, mention recursion and give a nested or matched structure, such as brackets, as the example.
Check yourself
What do ::= and | mean in BNF?
::= means "is defined as", and | means "or".
How does BNF show that something can be repeated?
By a recursive rule, where the rule refers to itself, such as <integer> ::= <digit> | <digit><integer>.
Why can BNF describe some languages that regular expressions cannot?
BNF rules can be recursive, so it can represent nested or balanced structures (such as matching brackets, or equal numbers of 0s and 1s) that need unlimited counting, which regular expressions and FSMs can't do.
Is 12 a valid <integer> using the rule above?
Yes. 1 is a <digit> and 2 is a valid <integer> (a single digit), so 12 matches <digit><integer>.