📄 Paper 1 · 4.4 Theory of Computation
4.4.3 BNF & Syntax Diagrams
AQA 7517 · A-Level Computer Science · ~20 min read

Backus-Naur Form (BNF)

Backus-Naur Form (BNF) is a formal notation for describing the syntax of programming languages. It is a type of context-free grammar (CFG) and uses rules called productions.

BNF notation

SymbolMeaningExample
<name>Non-terminal (to be replaced)<digit>
::="Is defined as"<digit> ::= 0 | 1 | 2 | ...
|Alternation (OR)0 | 1 | 2
TerminalLiteral symbol (not in angle brackets)0, 1, +, =

Example BNF grammar for integers

<integer> ::= <sign><digits> | <digits>
<sign>    ::= + | -
<digits>  ::= <digit> | <digit><digits>
<digit>   ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

How BNF works — derivation

Start with the top-level non-terminal and apply rules until only terminals remain. This is called a derivation.

// Derive "-37":
<integer> → <sign><digits>
           → - <digits>
           → - <digit><digits>
           → - 3 <digits>
           → - 3 <digit>
           → - 3 7

Extended BNF (EBNF)

EBNF extends BNF with additional notation to make grammars more concise:

EBNF notationMeaning
[...]Optional (zero or one)
{...}Repetition (zero or more)
(...)Grouping
// EBNF for integer (equivalent to above):
<integer> ::= [+ | -] <digit> {<digit>}

Recursion in BNF

BNF naturally handles recursion — a rule that refers to itself. This is used to express repetition:

// Recursive definition of a list of digits:
<digits> ::= <digit> | <digit><digits>

// This generates: 0, 1, 00, 01, 10, 123, 4567, ...

// Recursive definition for balanced brackets:
<expr> ::= (<expr>) | ε

Syntax Diagrams (Railroad Diagrams)

A syntax diagram (also called a railroad diagram) is a visual alternative to BNF. It shows valid syntax as paths through a diagram.

Shapes used

ShapeRepresents
RectangleNon-terminal (rule)
Oval/rounded boxTerminal (literal)
ArrowPath/direction of reading
Split/loopAlternation or repetition

Reading syntax diagrams

  • Follow arrows from left to right
  • Where paths split, choose one branch (alternation)
  • Where a path loops back, the element can repeat
  • A valid string follows any valid path from start to end

BNF and Context-Free Grammars

BNF describes context-free languages — more powerful than regular languages. CFLs can express nested/recursive structures (like balanced brackets, arithmetic expressions) that regular expressions cannot.

FormalismLanguage classExample
Regular expressions / FSMsRegular languagesIdentifiers, keywords
BNF / CFGContext-free languagesArithmetic expressions, balanced brackets
Turing machinesRecursively enumerableAll computable problems
Exam tip: AQA asks you to read and write BNF, trace derivations, and interpret syntax diagrams. Key points: non-terminals in angle brackets <>; terminals are literals; ::= means "is defined as"; | is alternation; recursion allows repetition. Syntax diagrams are visual equivalents — follow the arrows. Know that BNF describes context-free languages, which are more powerful than regular languages.
▶
Click through the slides at your own pace. Use arrow keys or click to advance.
Click slide or press arrow keys to navigate

Worksheet — 4.4.3 BNF & Syntax Diagrams

8 questions · instantly marked · AQA 7517 standard

Q1What does BNF stand for and what is it used for?[2]
✅ Mark scheme
Mark scheme
Backus-Naur Form [1]; used to formally describe (specify) the syntax of programming languages / context-free grammars [1].
Q2In BNF notation, what do: (a) angle brackets <>, (b) ::=, and (c) | represent?[3]
✅ Mark scheme
Mark scheme
(a) Angle brackets: enclose non-terminals (symbols that can be further replaced/expanded) [1]; (b) ::=: "is defined as" (production rule) [1]; (c) |: alternation / "or" (choice between alternatives) [1].
Q3Given: <digit> ::= 0|1|2|3|4|5|6|7|8|9 and <number> ::= <digit> | <digit><number>, show a derivation for the number 42.[3]
✅ Mark scheme
Mark scheme
<number> → <digit><number> [1] → 4<number> [1] → 4<digit> → 42 [1]. Accept any valid sequence of derivation steps leading to "42".
Q4Write BNF rules to define a <letter> (lowercase a–z) and a <word> (one or more letters) using recursion.[3]
✅ Mark scheme
Mark scheme
<letter> ::= a|b|c|...|z (full or abbreviated list) [1]; <word> ::= <letter> | <letter><word> [2] — first alternative handles single letter [1], recursive alternative handles multiple letters [1].
Q5What is the difference between a terminal and a non-terminal in BNF?[2]
✅ Mark scheme
Mark scheme
A terminal is a literal symbol that appears in the final string and cannot be expanded further [1]; a non-terminal (in angle brackets) is a placeholder that must be replaced by further productions until only terminals remain [1].
Q6In EBNF, what do the notations [...] and {...} mean?[2]
✅ Mark scheme
Mark scheme
[...] = optional (zero or one occurrence) [1]; {...} = repetition (zero or more occurrences) [1].
Q7What shapes are used in syntax (railroad) diagrams and what do they represent?[2]
✅ Mark scheme
Mark scheme
Rectangles represent non-terminals (references to other rules) [1]; ovals/rounded boxes represent terminals (literal symbols) [1]. Arrows show the direction/path through the diagram.
Q8Explain why BNF is more powerful than regular expressions, giving an example of something BNF can describe that a regex cannot.[3]
✅ Mark scheme
Mark scheme
BNF describes context-free languages, which are more powerful than regular languages [1]; BNF can express nested/recursive structures through recursive rules [1]; e.g. balanced parentheses or nested arithmetic expressions like ((1+2)*3) cannot be described by a regular expression but can be expressed using BNF [1].
Topic Quiz
Question 1 of 15
You scored
out of 15
Card 1 of 9
Click to reveal definition
🎉
All cards reviewed!
TermDefinition
🎯

Mini Test — BNF & Syntax Diagrams

10 questions · 10 minutes

← 4.4.2b Sets & Regular Expressions
29 of 70 · AQA 7517
4.4.4a Big-O Notation →