Subsections

Expressions

If an operator has multiple operands, the order of evaluation is unspecified except when stated otherwise. The precedence and associativity of the operators are listed in Table 2.


Table 2: Operator precedence and associativity.
Precedence Operators Associativity
1 ( ) (function call), [ ] (indexing) left
2 not, size of, (unary) -, (unary) + right
3 *, / left
4 (binary) +, (binary) - left
5 $<$, $<=$, $>$, $>=$ non
6 =, $<>$ left
7 and left
8 or left


Grouping

expression
$\rightarrow$
( expression$_1$ )

Yields expression$_1$. expression$_1$ must be of any type. expression is of the same type, and may be used as lvalue if expression$_1$ may be used as lvalue.

Constants

expression
$\rightarrow$
integer_constant

expression
$\rightarrow$
character_constant

expression
$\rightarrow$
real_constant

expression
$\rightarrow$
true $\vert$ false

expression
$\rightarrow$
string_constant

expression
$\rightarrow$
null

Any constant is an expression, as defined in Sect. 2.

Identifiers

expression
$\rightarrow$
identifier

An identifier can be used as expression. identifier must be visible in the current scope. The type of expression depends on the declaration of identifier. If it is declared as variable, the type is given by its declaration, and expression can be used as lvalue. If it is declared as subprogram, the type is of the implicit subprogram type; the only way to use it is in a subprogram invocation; it cannot be used as lvalue then.

The new operator

expression
$\rightarrow$
new array [ expression$_1$ ] of type

The new operator yields a reference to newly allocated heap space for an array. expression$_1$ must be of type int. expression$_1$ indicates the size (number of elements) of the array; a runtime error occurs if this value is negative. All elements in the array are initialized according to Table 1. The type of expression is array of type; it may not be used as lvalue.

The index operator

expression
$\rightarrow$
expression$_1$ [ expression$_2$ ]

Indexes expression$_1$ with index value expression$_2$. The type of expression$_1$ must be an array of any type, expression$_2$ must be of type int. A runtime error occurs if expression$_1$ is a null reference, or if not 0 $\leq$ expression$_2$ $<$ size of expression$_1$. The type of expression is the base type of expression$_1$ and may be used as lvalue.

Subprogram invocation

expression
$\rightarrow$
expression$_1$ ( [ argument_list ] )

argument_list
$\rightarrow$
expression [ , argument_list$_1$ ]

Invokes the subprogram expression$_1$. expression$_1$ must be a procedure or function. If expression$_1$ is a procedure, expression has no value, and may not be used as subexpression. If expression$_1$ is a function, expression has the value of the result of invoking expression$_1$; its type becomes the return type of the invoked function; it may not be used as lvalue.

The actual argument list must match the formal parameter list, as defined below. The number of actual arguments must be the same as the number of formal parameters. If the formal parameter corresponding to an actual argument is not a var parameter, the type of the actual argument must be assignment compatible with the type of the formal parameter; call by value semantics is used for argument passing. If the formal parameter corresponding to an actual argument is a var parameter, the type of the actual argument must be exactly the same as the type of the formal parameter; the actual argument must be an lvalue; and call by reference semantics is used for argument passing.

The not operator

expression
$\rightarrow$
not expression$_1$

Yields the logical negation of expression$_1$. expression$_1$ must be of type bool. expression is of type bool, and may not be used as lvalue.

The size of operator

expression
$\rightarrow$
size of expression$_1$

Yields the number of elements in expression$_1$. expression$_1$ must be an array of any type. A runtime error occurs if expression$_1$ is a null reference. expression is of type int, and may not be used as lvalue.

The unary minus operator

expression
$\rightarrow$
$-$ expression$_1$

Yields the result of subtracting expression$_1$ from 0 or 0.0. expression$_1$ must be of type int or real. expression is of the same type, and may not be used as lvalue.

The unary plus operator

expression
$\rightarrow$
+ expression$_1$

Yields expression$_1$. expression$_1$ must be of type int or real. expression is of the same type, and may not be used as lvalue.

The multiplication operator

expression
$\rightarrow$
expression$_1$ * expression$_2$

Yields the product of expression$_1$ and expression$_2$. expression$_1$ and expression$_2$ must be both of type int or both of type real. expression is of the same type, and may not be used as lvalue.

The division operator

expression
$\rightarrow$
expression$_1$ / expression$_2$

Yields the quotient of expression$_1$ and expression$_2$. expression$_1$ and expression$_2$ must be both of type int or both of type real. expression is of the same type, and may not be used as lvalue.

The binary minus operator

expression
$\rightarrow$
expression$_1$ $-$ expression$_2$

Yields the difference between expression$_1$ and expression$_2$. expression$_1$ and expression$_2$ must be both of type int or both of type real. expression is of the same type, and may not be used as lvalue.

The binary plus operator

expression
$\rightarrow$
expression$_1$ + expression$_2$

Yields the sum of expression$_1$ and expression$_2$. expression$_1$ and expression$_2$ must be both of type int or both of type real. expression is of the same type, and may not be used as lvalue.

The smaller than operator

expression
$\rightarrow$
expression$_1$ $<$ expression$_2$

Yields true if expression$_1$ is smaller than expression$_2$. $<$ is non-associative, thus e$_1$ $<$ e$_2$ $<$ e$_3$ results in an error. expression$_1$ and expression$_2$ must be both of type char, both of type int, or both of type real. expression is of type bool, and may not be used as lvalue.

The smaller or equal operator

expression
$\rightarrow$
expression$_1$ $<=$ expression$_2$

Yields true if expression$_1$ is smaller than or equal to expression$_2$. $<=$ is non-associative, thus e$_1$ $<=$ e$_2$ $<=$ e$_3$ results in an error. expression$_1$ and expression$_2$ must be both of type char, both of type int, or both of type real. expression is of type bool, and may not be used as lvalue.

The greater than operator

expression
$\rightarrow$
expression$_1$ $>$ expression$_2$

Yields true if expression$_1$ is greater than expression$_2$. $>$ is non-associative, thus e$_1$ $>$ e$_2$ $>$ e$_3$ results in an error. expression$_1$ and expression$_2$ must be both of type char, both of type int, or both of type real. expression is of type bool, and may not be used as lvalue.

The greater or equal operator

expression
$\rightarrow$
expression$_1$ $>=$ expression$_2$

Yields true if expression$_1$ is greater than or equal to expression$_2$. $>=$ is non-associative, thus e$_1$ $>=$ e$_2$ $>=$ e$_3$ results in an error. expression$_1$ and expression$_2$ must be both of type char, both of type int, or both of type real. expression is of type bool, and may not be used as lvalue.

The equality operator

expression
$\rightarrow$
expression$_1$ $=$ expression$_2$

Yields true if expression$_1$ is equal to expression$_2$. expression$_1$ and expression$_2$ must be both of type bool, both of type char, both of type int, both of type real; or one of the operands is an array of any type and the other is either an array of compatible base type or null. Two arrays are equal if the references are equal. expression is of type bool, and may not be used as lvalue.

The unequal operator

expression
$\rightarrow$
expression$_1$ $<>$ expression$_2$

Is equivalent with not ( expression$_1$ $=$ expression$_2$ ).

The logical and operator

expression
$\rightarrow$
expression$_1$ and expression$_2$

Yields false if expression$_1$ evaluates to false, or if expression$_2$ evaluates to false. If expression$_1$ evaluates to false, expression$_2$ is not evaluated. expression$_1$ and expression$_2$ must be both of type bool. expression is of type bool, and may not be used as lvalue.

The logical or operator

expression
$\rightarrow$
expression$_1$ or expression$_2$

Yields true if expression$_1$ evaluates to true, or if expression$_2$ evaluates to true. If expression$_1$ evaluates to true, expression$_2$ is not evaluated. expression$_1$ and expression$_2$ must be both of type bool. expression is of type bool, and may not be used as lvalue.

K.G. Langendoen 2006-10-03