Precedence and Associativity

Like most programming languages, WMLScript assigns a level of precedence to each of the operators. In addition, each binary operator has an associativity . These exist to disambiguate expressions without the need for lots of extra parentheses.

The precedence determines which parts of an expression are evaluated first when there are different operators involved. For example, the * operator has a higher precedence than the + operator, so:

3 + 4 * 5

is actually equivalent to:

3 + (4 * 5)

not:

(3 + 4) * 5

and the answer is 23, not 35.

Associativity determines which parts of an expression should be evaluated first when there are several operators of the same precedence together. (All operators of the same precedence always have the same associativity.) Associativity is specified as either left or right. Left-associative operators evaluate their left side first, and right-associative operators evaluate their right side first.

For example, + and - are both left-associative with the same precedence, so:

3 - 4 + 5

is equivalent to:

(3 - 4) + 5

rather than:

3 - (4 + 5)

and the answer is 4, not -6.

Similarly, the assignment operators are all right-associative with the same precedence, so:

a = b = c

is equivalent to:

a = (b = c)

rather than:

(a = b) = c

which would not be legal, since:

a = b

isn’t a variable, so can’t appear on the left side of the = operator.

Appendix C summarizes all the WMLScript operators, together with their precedence and associativity.

Get Learning WML, and WMLScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.