Summary of the Grammar
The official Swift book ends with a complete formal grammar — the precise rules that define what counts as valid Swift. This page is a short orientation: what that grammar is, how to read the notation it uses, and where to find the full, authoritative version online.
What the formal grammar is
Throughout the reference chapters, each construct is accompanied by a small set of grammar rules. Collected together, they form a complete description of Swift's syntax — a contract that the compiler's parser implements. The grammar tells you, unambiguously, how tokens combine into expressions, statements, declarations, types, and patterns.
You rarely need to read it cover to cover. Its value is as a precise tie-breaker: when prose is ambiguous about exactly what syntax is allowed, the grammar settles it.
Reading the BNF-style notation
Swift's grammar is written in a variant of BNF (Backus–Naur Form). A few conventions are worth knowing:
| Notation | Meaning |
|---|---|
| italic name | A syntactic category (a rule that is defined elsewhere) |
::= or “defined as” | Separates a rule's name from its definition |
| monospace text | A literal token that appears verbatim in source |
| subscript opt | The preceding element is optional |
vertical bar | | Alternatives — choose one |
| a category listed on multiple lines | Each line is an alternative production |
For example, a rule of the form “guard-statement →
guard condition-list else
code-block” reads as: a guard statement is the keyword
guard, followed by a condition list, the keyword else,
and a code block. An element marked opt may be left out.
Note: the notation describes syntax only — what arrangements of tokens parse. Rules about meaning (type checking, overload resolution, concurrency isolation) live in the prose, not the grammar.
Where to find the complete grammar
This blog deliberately does not reproduce the entire grammar — it is long, it evolves with each Swift release, and the canonical version should always be read from the source. The authoritative reference is the “Summary of the Grammar” chapter of The Swift Programming Language, maintained by Apple and the Swift open-source project:
- docs.swift.org/swift-book — the full book, including the complete formal grammar and every reference chapter summarized on the preceding pages.
Tip: when you hit a syntax question these reference pages don't answer, jump straight to the relevant chapter on docs.swift.org and find its grammar rules — they're the same rules the Swift compiler enforces.
Summary
The formal grammar is Swift's precise syntactic specification, written in
BNF-style notation with italic categories, literal tokens, optional
(opt) elements, and | alternatives. Use it as the final
word on what parses, and read the always-current version in
The Swift Programming Language on docs.swift.org. That closes out this
Language Reference.