/ COMPUTER SCIENCE, COMPILERS

# Generalizing PEMDAS: What is an operator?

In programming languages, an operator is a symbol used to represent a specific operation such as subtraction of integers or dereferencing a pointer. The symbols +, *, and ! are commonly used as operators in many programming languages, for example. In this article we define some basic programming language terminology that allows us to categorize operators according to their different properties.

## Computation with Expressions

Virtually all programming languages have expressions, like 2 + 4, which consist of operators (the + operator in 2 + 4) and their operands (or arguments; the 2 and the 4), that is, the things operators operate on. This language is barrowed from mathematics where, for example, $17/2 + \sin(\pi/2)$ is an expression that evaluates to the value $19/2$. Most programming lanugages also have other language constructs like statements and declarations that are not expressions. In some languages, like Wolfram Language, everything is an expression. In these languages, an entire program itself is an expression, albeit possibly a very complex one, and executing a program is called evaluation.

## Properties of Operators

### Arity

The arity of an operator is just a fancy word for how many operands (arguments) the operator takes. Here are some common examples:

Arity Operator Example Expression
“Nullary”
(takes no arguments)
Constants 7
Unary Factorial: ! $3! = 3 \cdot 2 \cdot 1 = 6$
Binary Addition: + $2 + 4 = 6$
Ternary The conditional operator
found in many languages:
a ? b : c
// if a>b then x else y
result = a > b ? x : y;
(takes a variable number
of arguments)
Lists: {x, y, ...} { } (empty list);
{2, 3, 5, 7, 11}
$n$-ary
(takes $n$ arguments)
A function of $n$ arguments:
$f(a_1, a_2, \ldots, a_n)$
DeleteCases[expr, pattern,
 levelspec, n]

You will occassionally see authors abuse the language above by using n-ary to mean variadic. The Wikipedia article on arity has a long list of obscure names for different arities.

### Affix

The affix of an operator refers to the operator’s position within an expression with respect to its operands. Again, some common examples:

Affix Operator Example Expression
Prefix: precedes its operand(s) Unary minus: - $-7$
Postfix: follows its operand(s) Factorial: ! $3!$
Infix: between its operands Addition: + $2 + 4$
Matchfix (Circumfix): surrounds its operand(s) Floor function: ⌊ ⌋ $\lfloor{2.78}\rfloor = 2$

## Conclusion

The arity, affix, associativity, and precedence of an operator are essential to know in order to determine which parts of an expression a given operator is acting on and therefore how the expression should be evaluated. A parser for a programming language is responsible for correctly interpreting its input according to these operator properties. In fact, some parsing strategies rely entirely on operator properties to interpret the meaning of expressions in programs.

#### Robert Jacobson

I am an assistant professor of mathematics at Roger Williams University. I write about mathematics, education, mathematics education, computer science, and whatever interests me.