Revision Notes
AQA A-Level Computer Science (7517): Context-free languages – Revision Notes
Condensed AQA 7517 section 4.4.3 recall: Backus-Naur Form, syntax diagrams, recursion, checking and writing rules, and why BNF goes beyond regular expressions.
- Subject
- Computer Science
- Level
- A LEVELS
- Topic
- Theory of computation
- Author
- Marlbridge Academic Team
- Updated
- Reviewed by
- Harris Khan (what this means)
Aligned to AQA A Level Computer Science (7517), 2015-onwards. Official specification .
Syllabus page (what it covers and how it is assessed): AQA A Level Computer Science.
Syllabus points this page covers
7517
- 4.4.3 Context-free languages
This page covers 1 of the 5 subtopics in topic 4, Theory of computation.
Found an error? Report a correction.
Need help with this topic? Request a free trial class for A Level Computer Science (7517).
Condensed recall for 4.4.3 Context-free languages, AQA A-Level Computer Science (7517): check syntax using BNF or syntax diagrams, formulate simple production rules, and explain why BNF can represent some languages that regular expressions cannot. Fuller explanations are in the context-free languages study guide; exam-style questions are in the context-free languages practice questions.
BNF notation
| Item | Meaning |
|---|---|
<name> |
A non-terminal: a category that is defined by a rule |
Plain symbol, for example 7, +, if |
A terminal: it appears literally in the string |
::= |
“is defined as” |
| |
“or”: separates alternatives |
| Symbols side by side | “followed by” |
A rule is a production rule. A string is in the language if it can be derived from the starting non-terminal by repeatedly replacing non-terminals until only terminals are left.
Recursion
A rule that uses its own name is recursive, and this is how BNF shows repetition:
<number> ::= <digit> | <digit><number>
- First alternative: the base case that ends the recursion.
- Second alternative: the recursive case.
- A recursive rule without a base case can never finish.
Checking a string
- Start with the starting non-terminal.
- Choose the alternative that fits the next symbols.
- Expand non-terminals until you reach terminals.
- If the whole string is produced, it is valid. If no alternative fits, name the rule that fails.
Worked check.
<identifier> ::= <letter> | <letter><rest>
<rest> ::= <char> | <char><rest>
<char> ::= <letter> | <digit>
<letter> ::= a | b | c | d
<digit> ::= 0 | 1 | 2
ab1is valid: letter a, then rest b1 (char b, then rest 1).1abis invalid: an identifier must start with a letter.a4is invalid: 4 is not a digit in this language.
Writing rules
- Look at the shape of a valid string.
- Name each part and give it a rule.
- Use
|for choices. - Use recursion for unlimited repeats, with a base case.
- Test with one valid and one invalid string.
Example. One or more a’s followed by a single b:
<s> ::= <as> b
<as> ::= a | a <as>
Valid: ab, aaab. Invalid: b, abb.
Syntax diagrams
A syntax diagram is a picture of the same rules.
| Element | Meaning |
|---|---|
| Circle or rounded box | A terminal |
| Rectangle | A non-terminal (refers to another diagram) |
| Branches | Alternatives (like |) |
| Arrow looping back | Repetition (like recursion) |
Follow arrows from entry to exit to build a valid string. A rule such as <number> ::= <digit> | <digit><number> becomes one <digit> rectangle with a loop back.
Why BNF is more powerful than regular expressions
- A regular expression (or FSM) can repeat but has only a finite memory, so it cannot match one part of a string with another by counting.
- BNF rules can be recursive with the recursion in the middle of the rule, which builds nested or matched structures.
{0ⁿ1ⁿ | n ≥ 1}is not regular, but in BNF it is:
<s> ::= 0 1 | 0 <s> 1
- Other examples are balanced brackets and arithmetic with nested brackets.
- Programming languages use nesting everywhere (brackets, nested
IF, nested calls), so BNF can describe their syntax and regular expressions alone cannot. - Every regular language can be described in BNF, but not the reverse.
Derivation example
Using the rules <number> ::= <digit> | <digit><number> and <digit> ::= 0 | 1 | ... | 9, the number 407 is derived as:
<number>
<digit><number>
4 <number>
4 <digit><number>
4 0 <number>
4 0 <digit>
4 0 7
Where syntax rules are used
- Compilers and interpreters check that source code follows the rules of the language before translating it. A statement that cannot be derived from the rules causes a syntax error.
- Language designers use BNF to publish an unambiguous definition of a language.
- Data formats and commands can also be defined this way, for example the allowed form of a date or an input line.
A string that follows the rules is syntactically valid. This is not the same as being meaningful or correct: 2 * (3 + 4) is valid syntax whatever the numbers mean in a program.
Exam traps
- No base case in a recursive rule.
- Angle brackets missing on non-terminals or added to terminals.
- Using
*or+from regular expressions in BNF. - Accepting a string after checking only its start.
- Weak explanation of why BNF is more powerful: you need recursion or nesting, not “shorter”.
- Unlabelled boxes in a syntax diagram.
Key terms
| Term | Definition |
|---|---|
| Syntax | The rules about how symbols may be combined into valid strings |
| Backus-Naur Form | A notation that defines syntax using production rules |
| Production rule | A rule saying how a non-terminal may be replaced |
| Terminal | A symbol that appears literally in valid strings |
| Non-terminal | A named category defined by a production rule |
| Recursion | A rule that refers to itself |
| Syntax diagram | A graphical form of BNF |
| Context-free language | A language that can be defined by BNF rules |
Self-test
- What do the symbols
::=and|mean in BNF? - What is the difference between a terminal and a non-terminal?
- What is the base case in
<number> ::= <digit> | <digit><number>? - Why does a recursive rule need a base case?
- With
<s> ::= a b | a <s> b, isaabbvalid? - With the same rule, is
aabvalid? - Write a rule for a signed integer: an optional + or - sign, then digits (use
<digit>). - How is repetition shown in a syntax diagram?
- Why can a regular expression not describe balanced brackets?
- Why is BNF used to define programming language syntax?
Answers
::=means “is defined as”;|means “or”.- A terminal appears literally in the string; a non-terminal is a name for a category that is defined by a rule.
<digit>, a single digit.- Without it the rule can never finish producing a string.
- Yes:
a <s> bwith<s>asa b. - No: the numbers of a’s and b’s are not equal.
<int> ::= <digits> | + <digits> | - <digits>and<digits> ::= <digit> | <digit><digits>.- By an arrow that loops back to an earlier point.
- It would need to remember how many brackets are open, which needs unbounded memory.
- It is precise, and its recursion can describe nested constructs.
More resources on this topic
Related resources
-
Study guides
AQA A-Level Computer Science (7517): Regular languages
Deeper guide to AQA 7517 section 4.4.2 Regular languages: finite state machines with and without output, set notation, regular expressions and the definition of a regular language.
Computer Science · AQA · A Level
Read guide -
Practice questions
AQA A-Level Computer Science (7517): Regular languages – Practice Questions
Original AQA 7517 section 4.4.2 questions with answers: finite state machines, Mealy machines, sets, regular expressions and regular languages.
Computer Science · AQA · A Level
Try questions -
Revision notes
AQA A-Level Computer Science (7517): Regular languages – Revision Notes
Condensed AQA 7517 section 4.4.2 recall: finite state machines, Mealy machines, set notation, regular expressions and regular languages, with a self-test.
Computer Science · AQA · 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.