The Syntax of the Wolfram Language

The Wolfram Language uses various syntactic rules to interpret input that you give, and to convert strings and boxes into expressions. The version of these rules that is used for (RefLink[StandardForm, paclet:ref/StandardForm]) and (RefLink[InputForm, paclet:ref/InputForm]) in effect defines the basic Wolfram Language. The rules used for other forms, such as (RefLink[TraditionalForm, paclet:ref/TraditionalForm]), follow the same overall principles, but differ in many details.

Types of tokens in the Wolfram Language.

When you give text as input to the Wolfram Language, the first thing that the Wolfram System does is to break the text into a sequence of tokens, with each token representing a separate syntactic unit.

Thus, for example, if you give the input xx+yy-zzzz, the Wolfram System will break this into the sequence of tokens xx, +, yy, - and zzzz. Here xx, yy, and zzzz are tokens that correspond to symbols, while + and - are operators.

Operators are ultimately what determine the structure of the expression formed from a particular piece of input. The Wolfram Language involves several general classes of operators, distinguished by the different positions in which they appear with respect to their operands.

Examples of classes of operators in the Wolfram Language.

Operators typically work by picking up operands from definite positions around them. But when a string contains more than one operator, the result can in general depend on which operator picks up its operands first.

Thus, for example, a*b+c could potentially be interpreted either as (a*b)+c or as a*(b+c) depending on whether * or + picks up its operands first.

To avoid such ambiguities, the Wolfram Language assigns a precedence to each operator that can appear. Operators with higher precedence are then taken to pick up their operands first.

Thus, for example, the multiplication operator * is assigned higher precedence than +, so that it picks up its operands first, and a*b+c is interpreted as (a*b)+c rather than a*(b+c).

The * operator has higher precedence than +, so in both cases (RefLink[Times, paclet:ref/Times]) is the innermost function:

{FullForm[a*b+c],FullForm[a+b*c]}

The // operator has rather low precedence:

a*b+c//f

The @ operator has high precedence:

f@a*b+c

Whatever the precedence of the operators you are using, you can always specify the structure of the expressions you want to form by explicitly inserting appropriate parentheses.

Inserting parentheses makes (RefLink[Plus, paclet:ref/Plus]) rather than (RefLink[Times, paclet:ref/Times]) the innermost function:

FullForm[a*(b+c)]

Outline of operators in order of decreasing precedence.

The table in "Operator Input Forms" gives the complete ordering by precedence of all operators in the Wolfram Language. Much of this ordering, as in the case of * and +, is determined directly by standard mathematical usage. But in general the ordering is simply set up to make it less likely for explicit parentheses to have to be inserted in typical pieces of input.

Operator precedences are such that this requires no parentheses:

(Subscript[∀, x])(Subscript[∃, y])x⊗y≻y∧m≠0\[Implies]n⧐̸m

(RefLink[FullForm, paclet:ref/FullForm]) shows the structure of the expression that was constructed:

FullForm[%]

Note that the first and second forms here are identical; the third requires explicit parentheses:

{x->#^2&,(x->#^2)&,x->(#^2&)}

Types of grouping for infix operators.

(RefLink[Plus, paclet:ref/Plus]) is a (RefLink[Flat, paclet:ref/Flat]) function, so no grouping is necessary here:

FullForm[a+b+c+d]

(RefLink[Power, paclet:ref/Power]) is not (RefLink[Flat, paclet:ref/Flat]), so the operands have to be grouped in pairs:

FullForm[a^b^c^d]

The syntax of the Wolfram Language is defined not only for characters that you can type on a typical keyboard, but also for all the various special characters that the Wolfram Language supports.

Letters such as γ, ℒ, and ℵ from any alphabet are treated just like ordinary English letters, and can for example appear in the names of symbols. The same is true of letter-like forms such as ∞, ℏ, and ∠.

But many other special characters are treated as operators. Thus, for example, ⊕ and ⊎ are infix operators, while ¬ is a prefix operator, and ⟨ and ⟩ are matchfix operators.

⊕ is an infix operator:

a ⊕ b ⊕ c//FullForm

× is an infix operator which means the same as *:

a × a × a × b × b × c

Some special characters form elements of fairly complicated compound operators. Thus, for example, ∫fdx contains the compound operator with elements ∫ and d.

The ∫ and d form parts of a compound operator:

∫ k[x] \[DifferentialD]x//FullForm

No parentheses are needed here: the "inner precedence" of ∫…d is lower than (RefLink[Times, paclet:ref/Times]):

∫ a[x] b[x] \[DifferentialD]x+c[x]

Parentheses are needed here, however:

∫ (a[x]+b[x]) \[DifferentialD]x+c[x]

Input to the Wolfram Language can be given not only in the form of one-dimensional strings, but also in the form of two-dimensional boxes. The syntax of the Wolfram Language covers not only one-dimensional constructs but also two-dimensional ones.

This superscript is interpreted as a power:

(x^(a+b))

(∂_x)f is a two-dimensional compound operator:

(∂_x)(x^n)

∑ is part of a more complicated two-dimensional compound operator:

(∑_(n=1)^∞)(1/(n^s))

The ∑ operator has higher precedence than +:

(∑_(n=1)^∞)(1/(n^s))+n