Revision Notes
OxfordAQA A-Level Computer Science (9645): Backus-Naur Form (BNF) and syntax diagrams – Revision Notes
Recall notes for OxfordAQA 9645 sub-topic 3.13.4: BNF notation, recursion, checking syntax, syntax diagrams, palindromes and equal counts, with a self-test.
- Subject
- Computer Science
- Level
- A LEVEL
- Topic
- Topic 13 – Theory of computation
- Author
- Marlbridge Academic Team
- Updated
- Reviewed by
- Harris Khan (what this means)
Aligned to OxfordAQA A Level Computer Science (9645), 2024-onwards. Official specification .
Syllabus page (what it covers and how it is assessed): OxfordAQA A Level Computer Science.
Syllabus points this page covers
9645 (A Level)
- 3.13.4 Backus-Naur Form (BNF) and syntax diagrams
This page covers 1 of the 5 subtopics in topic 13, Theory of computation.
Found an error? Report a correction.
Need help with this topic? Request a free trial class for A Level Computer Science (9645).
These notes condense sub-topic 3.13.4 Backus-Naur Form (BNF) and syntax diagrams of OxfordAQA International AS and A-level Computer Science (9645), Version 1.1 (International A-level content in topic 3.13, examined in Unit 4). For derivations and worked grammars, see the BNF study guide, then practise with the BNF practice questions.
What the specification requires
- Check language syntax by referring to BNF or syntax diagrams.
- Formulate simple production rules.
- Know that BNF can represent some languages that regular expressions cannot, such as palindromes and languages where strings must contain equal numbers of certain characters.
Key terms
| Term | Meaning |
|---|---|
| BNF | Backus-Naur Form, a notation that defines a language using production rules |
| Production rule | <name> ::= alternatives, saying how a non-terminal may be replaced |
| Non-terminal | A name in angle brackets that stands for a set of strings |
| Terminal | A symbol that appears in the final string |
::= |
“is defined as” |
| |
“or” between alternatives |
| Recursion | A rule that uses its own name; used for repetition |
| Syntax diagram | A diagram of paths through boxes that shows the same rules |
| Derivation | A sequence of substitutions from the start non-terminal to a string of terminals |
Reading a rule
<identifier> ::= <letter> | <letter><rest>
<rest> ::= <letter> | <digit> | <letter><rest> | <digit><rest>
- The left side names the non-terminal being defined.
- Alternatives are separated by
|. - Items side by side must appear one after another.
- The alternatives that do not mention the rule’s own name are the base case.
- The alternatives that do mention it are the recursive case.
Checking strings
Time of day rules:
<time> ::= <hour>:<minute>
<hour> ::= 0<digit> | 1<digit> | 2<low>
<low> ::= 0 | 1 | 2 | 3
<minute> ::= <tens><digit>
<tens> ::= 0 | 1 | 2 | 3 | 4 | 5
| String | Valid? | Reason |
|---|---|---|
09:45 |
Yes | all parts fit |
24:00 |
No | after 2 only 0 to 3 is allowed |
7:30 |
No | the hour needs two digits |
12:60 |
No | minute tens must be 0 to 5 |
Method: split the string into the parts the top rule names, then test each part against its own rule.
Expressions with brackets
<expr> ::= <term> | <expr>+<term>
<term> ::= <digit> | (<expr>)
| String | Valid? | Reason |
|---|---|---|
2+3+4 |
Yes | <expr>+<term> applied twice |
2+(3+4) |
Yes | the bracket is a <term> |
((5)) |
Yes | nested brackets around a digit |
2+ |
No | + must be followed by a <term> |
(2+3 |
No | the bracket is never closed |
23 |
No | two digits are not a single <term> |
Brackets can nest to any depth, which regular expressions cannot match.
Writing rules: a checklist
- Name each part that can be built from smaller parts.
- Give each name a rule with
::=. - Repetition: recursion with a base case and a recursive case.
- Optional item: two alternatives, one with and one without.
- Test on valid and invalid strings.
Example: signed number with optional fraction.
<real> ::= <integer> | <integer>.<fraction>
<integer> ::= <unsigned> | +<unsigned> | -<unsigned>
<unsigned> ::= <digit> | <digit><unsigned>
<fraction> ::= <unsigned>
4.25 valid; 23. invalid; .5 invalid; --2 invalid.
Syntax diagrams
| BNF feature | In a syntax diagram |
|---|---|
| Terminal | rounded box |
| Non-terminal | rectangular box |
| alternative |
separate route from the entry |
| Recursive rule | a loop back to an earlier point |
| Items one after another | boxes in sequence along a route |
A string is valid if some route from entry to exit uses its symbols in order.
BNF versus regular expressions
<pal> ::= 0 | 1 | 00 | 11 | 0<pal>0 | 1<pal>1
<same> ::= 01 | 0<same>1
| Language | Examples | Regular expression? | BNF? |
|---|---|---|---|
| Palindromes over 0 and 1 | 11011, 0110 |
No | Yes |
| Equal 0s then 1s | 01, 0011, 000111 |
No | Yes |
| Strings ending in 01 | 1101 |
Yes | Yes |
11011 is valid: outer 1s around 101, whose outer 1s surround 0. 0101 is not valid for <same>.
Common faults in grammars
| Fault | Symptom | Repair |
|---|---|---|
| Recursive rule with no base case | No string can ever be completed | Add a non-recursive alternative |
| Missing alternative | A valid string is rejected | Add the alternative and re-test |
| Too many alternatives | An invalid string is accepted | Remove or tighten the alternative |
| Terminal written with angle brackets | Treated as a non-terminal with no rule | Write plain characters for terminals |
Exam traps
- Missing base case. A recursive rule without a way to stop can never produce a string.
- No
*or+in BNF. Use recursion. - Angle brackets. Non-terminals have them; terminals do not.
- Partial matches. The whole string must fit the rule.
- Syntax only. BNF does not describe meaning.
- Reasons. When asked why a regular expression cannot do it, mention unlimited counting or nesting.
Quick self-test
- What does
::=mean? - What does
|mean in BNF? - What is a non-terminal?
- How is repetition expressed in BNF?
- Using the time rules, is
19:07valid? - Using the time rules, is
20:75valid? - Using the expression rules, is
(2)+3valid? - Give the base case of
<unsigned> ::= <digit> | <digit><unsigned>. - Name one language BNF can describe but a regular expression cannot.
- Is
001valid for<same>? - Name the two kinds of box in a syntax diagram.
- What does a syntax diagram loop represent?
Answers
- “Is defined as”.
- “Or” between alternatives.
- A name in angle brackets that stands for a set of strings, defined by a rule.
- By recursion: a rule that uses its own name.
- Yes: hour 1 then 9, minute 0 then 7.
- No: the minute tens digit 7 is not between 0 and 5.
- Yes:
(2)is a<term>, so<term>+<term>is an<expr>. <digit>- Palindromes, or strings with equal numbers of two characters.
- No: it has two 0s and one 1.
- Terminal (rounded) and non-terminal (rectangular).
- Recursion or repetition.
Official syllabus
OxfordAQA International AS and A-level Computer Science (9645) specification, Version 1.1, for International AS exams May/June 2025 onwards and International A-level exams May/June 2026 onwards: sub-topic 3.13.4 Backus-Naur Form (BNF) and syntax diagrams.
Related resources
-
Study guides
OxfordAQA A-Level Computer Science (9645): Classification of algorithms
OxfordAQA 9645 sub-topic 3.13.5 in depth: comparing algorithms, Big O and best and worst cases, tractable and intractable problems, heuristics and the Halting problem.
Computer Science · OxfordAQA · A Level
Read guide -
Practice questions
OxfordAQA A-Level Computer Science (9645): Classification of algorithms – Practice Questions
Ten original OxfordAQA 9645 questions on sub-topic 3.13.5 Classification of algorithms: Big O, best and worst cases, tractability, heuristics and the Halting problem.
Computer Science · OxfordAQA · A Level
Try questions -
Revision notes
OxfordAQA A-Level Computer Science (9645): Classification of algorithms – Revision Notes
Recall notes for OxfordAQA 9645 sub-topic 3.13.5: Big O classes, best and worst cases, tractable and intractable problems, heuristics, the Halting problem.
Computer Science · OxfordAQA · A Level
Revise topic
Get free revision emails (optional)
Occasional emails with practice questions, worked explanations and links to free resources for the qualification and subjects you choose. No spam, and you can unsubscribe from any email. The free tools on this site never need an email.