SLIDE 1 / 10
CSZone.co.uk
Click anywhere to advance · Arrow keys also work
AQA 7517 · Paper 1 · 4.4.2b

Syntax
Diagrams

Visual grammar notation · Railroad diagrams · Section 4.4

WHAT YOU'LL LEARN
Box = non-terminal · Oval = terminal · Arrows = valid paths · BNF equivalence
AQA SPEC LINK
4.4.2 — Syntax diagrams as an alternative to BNF
What Are They?

Syntax Diagrams (Railroad Diagrams)

Syntax diagrams are a visual/graphical alternative to BNF for representing context-free grammars. Follow the paths from left to right to determine valid strings.
RECTANGLE / BOX
identifier
Non-terminal — can be expanded further
OVAL / ROUNDED
if
Terminal — literal character/keyword
Sequence

Sequence in Syntax Diagrams

Items connected left-to-right in sequence must all appear in order. Corresponds to BNF concatenation.
IF STATEMENT (simplified):
IF
condition
THEN
statement
ENDIF
BNF: <if-stmt> ::= IF <condition> THEN <statement> ENDIF
Alternatives

Alternatives (OR) in Syntax Diagrams

When there are multiple valid options, the path branches into parallel routes, each taken as an alternative. Corresponds to BNF's |.
DIGIT (0 or 1 or 2 or … or 9):
→——◯ 0 ——→
   ◯ 1 ——→
   ◯ 2 ——→
   … etc up to 9
Repetition

Repetition in Syntax Diagrams

Repetition is shown with a loop — an arrow going backwards over a section indicates it can be repeated. Corresponds to BNF recursion or EBNF's {…}.
INTEGER (one or more digits):
→——□digit□——→
    ↑———————↓
   (loop back = repeat)
This allows: "3", "42", "157", etc.
BNF vs Syntax Diagram

BNF vs Syntax Diagram Comparison

BNF
Compact textual notation
Easy to process by programs
Less visual — harder for humans
SYNTAX DIAGRAM
Visual and intuitive
Easy to trace valid strings
Harder to process algorithmically
Both notations are equivalent in expressive power — any grammar expressible in one can be expressed in the other.
Complete Example

Identifier Syntax Diagram

An identifier starts with a letter, followed by zero or more letters or digits:
SYNTAX DIAGRAM — IDENTIFIER
→——□letter□——⊕——→
             ↑ ↓
      □letter□ or □digit□
          (loop back)
BNF equivalent: <id> ::= <letter> | <id><letter> | <id><digit>
Reading Diagrams

How to Read a Syntax Diagram

Enter from the left, exit at the right
At each junction, choose one of the available paths
When you reach a rectangle, look up that non-terminal's own diagram
When you reach an oval, output that terminal character literally
If you can reach the exit, the string is syntactically valid
AQA Exam Style

Practice Question

AQA 7517 — Paper 1 Style
(a) In a syntax diagram, what shape is used to represent a terminal? [1]
(b) In a syntax diagram, what shape is used to represent a non-terminal? [1]
(c) Convert the following BNF rule into a description of a syntax diagram:
<number> ::= <digit> | <number><digit> [2]
(d) Give ONE advantage of syntax diagrams over BNF notation. [1]
[5 marks]
1 mark
(a) An oval (or rounded rectangle/circle)
1 mark
(b) A rectangle (or square box)
2 marks
(c) A rectangle labelled <digit>, with an arrow looping back allowing it to be repeated (to allow sequences of digits)
1 mark
(d) More visual/intuitive for humans to understand; easier to trace valid strings through
Summary

Key Points to Remember

Syntax diagram = visual alternative to BNF; also called railroad diagram
Rectangle = non-terminal (expands to another rule)
Oval = terminal (literal character — appears in string as-is)
Loop back = repetition; branching paths = alternatives (OR)
Syntax diagrams and BNF are equivalent — same expressive power
🎉 Lesson complete — move to the quiz!