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:

NotationMeaning
italic nameA syntactic category (a rule that is defined elsewhere)
::= or “defined as”Separates a rule's name from its definition
monospace textA literal token that appears verbatim in source
subscript optThe preceding element is optional
vertical bar |Alternatives — choose one
a category listed on multiple linesEach line is an alternative production

For example, a rule of the form “guard-statementguard 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:

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.