.. index:: ! abstract syntax
WebAssembly is a programming language that has multiple concrete representations (its :ref:`binary format <binary>` and the :ref:`text format <text>`). Both map to a common structure. For conciseness, this structure is described in the form of an abstract syntax. All parts of this specification are defined in terms of this abstract syntax.
.. index:: ! grammar notation, notation single: abstract syntax; grammar pair: abstract syntax; notation
The following conventions are adopted in defining grammar rules for abstract syntax.
- Terminal symbols (atoms) are written in sans-serif font: \K{i32}, \K{end}.
- Nonterminal symbols are written in italic font: \X{valtype}, \X{instr}.
- A^n is a sequence of n\geq 0 iterations of A.
- A^\ast is a possibly empty sequence of iterations of A. (This is a shorthand for A^n used where n is not relevant.)
- A^+ is a non-empty sequence of iterations of A. (This is a shorthand for A^n where n \geq 1.)
- A^? is an optional occurrence of A. (This is a shorthand for A^n where n \leq 1.)
- Productions are written \X{sym} ::= A_1 ~|~ \dots ~|~ A_n.
- Large productions may be split into multiple definitions, indicated by ending the first one with explicit ellipses, \X{sym} ::= A_1 ~|~ \dots, and starting continuations with ellipses, \X{sym} ::= \dots ~|~ A_2.
- Some productions are augmented with side conditions in parentheses, "(\iff \X{condition})", that provide a shorthand for a combinatorial expansion of the production into many separate cases.
- If the same meta variable or non-terminal symbol appears multiple times in a production, then all those occurrences must have the same instantiation. (This is a shorthand for a side condition requiring multiple different variables to be equal.)
When dealing with syntactic constructs the following notation is also used:
- \epsilon denotes the empty sequence.
- |s| denotes the length of a sequence s.
- s[i] denotes the i-th element of a sequence s, starting from 0.
- s[i \slice n] denotes the sub-sequence s[i]~\dots~s[i+n-1] of a sequence s.
- s \with [i] = A denotes the same sequence as s, except that the i-th element is replaced with A.
- s \with [i \slice n] = A^n denotes the same sequence as s, except that the sub-sequence s[i \slice n] is replaced with A^n.
- \concat(s^\ast) denotes the flat sequence formed by concatenating all sequences s_i in s^\ast.
Moreover, the following conventions are employed:
- The notation x^n, where x is a non-terminal symbol, is treated as a meta variable ranging over respective sequences of x (similarly for x^\ast, x^+, x^?).
- When given a sequence x^n, then the occurrences of x in a sequence written (A_1~x~A_2)^n are assumed to be in point-wise correspondence with x^n (similarly for x^\ast, x^+, x^?). This implicitly expresses a form of mapping syntactic constructions over a sequence.
Productions of the following form are interpreted as records that map a fixed set of fields \K{field}_i to "values" A_i, respectively:
\X{r} ~::=~ \{ \K{field}_1~A_1, \K{field}_2~A_2, \dots \}
The following notation is adopted for manipulating such records:
r.\K{field} denotes the contents of the \K{field} component of r.
r \with \K{field} = A denotes the same record as r, except that the contents of the \K{field} component is replaced with A.
r_1 \compose r_2 denotes the composition of two records with the same fields of sequences by appending each sequence point-wise:
\{ \K{field}_1\,A_1^\ast, \K{field}_2\,A_2^\ast, \dots \} \compose \{ \K{field}_1\,B_1^\ast, \K{field}_2\,B_2^\ast, \dots \} = \{ \K{field}_1\,A_1^\ast~B_1^\ast, \K{field}_2\,A_2^\ast~B_2^\ast, \dots \}\bigcompose r^\ast denotes the composition of a sequence of records, respectively; if the sequence is empty, then all fields of the resulting record are empty.
The update notation for sequences and records generalizes recursively to nested components accessed by "paths" \X{pth} ::= ([\dots] \;| \;.\K{field})^+:
- s \with [i]\,\X{pth} = A is short for s \with [i] = (s[i] \with \X{pth} = A).
- r \with \K{field}\,\X{pth} = A is short for r \with \K{field} = (r.\K{field} \with \X{pth} = A).
where r \with~.\K{field} = A is shortened to r \with \K{field} = A.
.. index:: ! vector pair: abstract syntax; vector
Vectors are bounded sequences of the form A^n (or A^\ast), where the A can either be values or complex constructions. A vector can have at most 2^{32}-1 elements.
\begin{array}{lllll}
\production{vector} & \vec(A) &::=&
A^n
& (\iff n < 2^{32})\\
\end{array}