BNF notation · Terminals vs non-terminals · Production rules · Derivation · Recursion
AQA SPEC LINK
4.4.2 — Backus-Naur Form and syntax diagrams
Why BNF?
Why Do We Need BNF?
Programming language syntax must be defined precisely — natural language is too ambiguous. BNF is a formal notation for defining the grammar (syntax rules) of languages.
Used to define syntax of programming languages (Python, Java, C#…)
Compilers use BNF grammar to check if programs are syntactically valid
BNF defines context-free grammars (Type 2 in Chomsky hierarchy)
BNF Symbols
BNF Notation
::= means "is defined as"
<digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
<angle brackets> = non-terminals (can be expanded)
<letter>, <expression>, <statement> — placeholders for further rules
Bare text = terminals (literal characters — cannot be expanded)
0, 1, a, b, +, -, if, while — actual characters in the language
Compiler design — BNF defines the grammar the parser checks against
Language specification — Python, Java, SQL all have formal BNF-like grammars
Error detection — parser reports "syntax error" when derivation fails
EBNF (Extended BNF) adds repetition {...} and optional [...] for convenience
AQA Exam Style
Practice Question
AQA 7517 — Paper 1 Style
Consider this BNF grammar: <letter> ::= a | b | c <digit> ::= 0 | 1 | 2 <identifier> ::= <letter> | <identifier> <letter> | <identifier> <digit> (a) Give TWO strings that can be derived from <identifier>. [2] (b) Explain what the | symbol means in BNF. [1] (c) Write a derivation for the string "ab2". [3]
[6 marks]
2 marks
(a) "a", "abc", "a1", "b2c" — any valid identifiers using a/b/c and 0/1/2
1 mark
(b) OR — it means the item can be replaced by any of the alternatives separated by |