The Syntax of the Mathematica Language

Mathematica 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 StandardForm and InputForm in effect defines the basic Mathematica language. The rules used for other forms, such as TraditionalForm, follow the same overall principles, but differ in many details.

a, xyz, symbols
"some text", "+" strings
123.456, 3*^45numbers
+, ->, operators
(*comment*)input to be ignored

Types of tokens in the Mathematica language.

When you give text as input to Mathematica, the first thing that Mathematica 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 , Mathematica will break this into the sequence of tokens , , , and . Here , , and 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 Mathematica language involves several general classes of operators, distinguished by the different positions in which they appear with respect to their operands.

prefix!xNot[x]
postfixx!Factorial[x]
infixx+y+zPlus[x,y,z]
matchfix{x,y,z}List[x,y,z]
compoundx/:y=zTagSet[x,y,z]
overfixOverHat[x]

Examples of classes of operators in the Mathematica 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, could potentially be interpreted either as or as depending on whether or picks up its operands first.

To avoid such ambiguities, Mathematica 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 is interpreted as rather than .

The operator has higher precedence than , so in both cases Times is the innermost function.
In[1]:=
Click for copyable input
Out[1]=
The operator has rather low precedence.
In[2]:=
Click for copyable input
Out[2]=
The operator has high precedence.
In[3]:=
Click for copyable input
Out[3]=

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 Plus rather than Times the innermost function.
In[4]:=
Click for copyable input
Out[4]//FullForm=
Extensions of symbol namesx_,#2,e::s, etc.
Function application variantse[e],e@@e, etc.
Power-related operatorse,e^e, etc.
Multiplication-related operators
e,e/e,ee,ee, etc.
Addition-related operatorsee,e+e,ee, etc.
Relational operatorse==e,ee,ee,ee,ee, etc.
Arrow and vector operatorsee,ee,ee,ee, etc.
Logic operatorsee,e&&e,ee,ee, etc.
Pattern and rule operatorse..,e|e,e->e,e/.e, etc.
Pure function operatore&
Assignment operatorse=e,e:=e, etc.
Compound expressione;e

Outline of operators in order of decreasing precedence.

The table in "Operator Input Forms" gives the complete ordering by precedence of all operators in Mathematica. 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.
In[5]:=
Click for copyable input
Out[5]=
FullForm shows the structure of the expression that was constructed.
In[6]:=
Click for copyable input
Out[6]//FullForm=
Note that the first and second forms here are identical; the third requires explicit parentheses.
In[7]:=
Click for copyable input
Out[7]=
flatx+y+zx+y+z
left groupingx/y/z(x/y)/z
right groupingx^y^zx^(y^z)

Types of grouping for infix operators.

Plus is a Flat function, so no grouping is necessary here.
In[8]:=
Click for copyable input
Out[8]//FullForm=
Power is not Flat, so the operands have to be grouped in pairs.
In[9]:=
Click for copyable input
Out[9]//FullForm=

The syntax of the Mathematica language is defined not only for characters that you can type on a typical keyboard, but also for all the various special characters that Mathematica 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.
In[10]:=
Click for copyable input
Out[10]//FullForm=
is an infix operator which means the same as .
In[11]:=
Click for copyable input
Out[11]=

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

The and form parts of a compound operator.
In[12]:=
Click for copyable input
Out[12]//FullForm=
No parentheses are needed here: the "inner precedence" of is lower than Times.
In[13]:=
Click for copyable input
Out[13]=
Parentheses are needed here, however.
In[14]:=
Click for copyable input
Out[14]=

Input to Mathematica 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 Mathematica language covers not only one-dimensional constructs but also two-dimensional ones.

This superscript is interpreted as a power.
In[15]:=
Click for copyable input
Out[15]=
is a two-dimensional compound operator.
In[16]:=
Click for copyable input
Out[16]=
is part of a more complicated two-dimensional compound operator.
In[17]:=
Click for copyable input
Out[17]=
The operator has higher precedence than .
In[18]:=
Click for copyable input
Out[18]=
New to Mathematica? Find your learning path »
Have a question? Ask support »