modeller
modeller

Reputation: 3850

Does precedence & associativity group operators or operands?

By reading the book "C++ Primer" and wikipedia, I notice both mentioned that "precedence and associativity defines the grouping of OPERATORS". However, it appears to me the examples they given were showing grouping of OPERANDS. Here I quote:

from "Defined terms @ C++ Primer 5th edition":

associativity: Determines how OPERATORS with the same precedence are grouped.

from Operator_associativity @ Wikipedia:

Consider the expression a ~ b ~ c. If the operator ~ has left associativity, this expression would be interpreted as (a ~ b) ~ c. If the operator has right associativity, the expression would be interpreted as a ~ (b ~ c).

But from what I see, the above explanation grouped two OPERANDS (not operators): a and b into (a ~ b), or b and c into (b ~ c). Because I see they parenthesized two operands, but not two operators.

Given that operator and operands are different concepts, does the precedence and associativity rule group operator or operands ?

Thanks in advance.

Upvotes: 2

Views: 276

Answers (3)

David Hammen
David Hammen

Reputation: 33126

Precedence and associativity address how a language interprets parenthesis-free expressions involving three or more operands. I'll use the symbols # and @ to denote two operators. Consider the expressions

  • a @ b # c,
  • x # y @ z,
  • d @ e @ f, and
  • u # v # w.

Note that the first two expressions involve different operands. The C++ precedence rules determines whether a@b#c is interpreted as meaning (a@b)#c or a@(b#c), and whether x#y@z is interpreted as meaning x#(y@z) or (x#y)@z.

The latter two expressions involve the same operand. Precedence has no bearing here. It's associativity that determines whether d@e@f is interpreted as meaning (d@e)@f or d@(e@f), and whether u#v#w is interpreted as meaning (u#v)#w or u#(v#w).


C++ has a large number of operators. There's an easy way to deal with the plethora of precedences: Use parentheses. My rule is "Everyone knows a*b+c means (a*b)+c. Nobody but a language lawyer knows whether a?b:c=d means (a?b:c)=d or a?b:(c=d). Use parentheses when in doubt."

Note: Apparently even Microsoft and wikipedia don't know the correct answer to "What does a?b:c=d mean?", at least as of June 16, 2014. The precedence tables at wikipedia and Microsoft have the ternary operator separate from the lower precedence assignment operators, which is incorrect. That would mean a?b:c=d needs to be interpreted as (a?b:c)=d, which always assigns the value of d to b or c, depending on whether a is true of false. That is incorrect. The correct interpretation is a?b:(c=d). The precedence tables at cppreference.com and cplusplus.com correctly group the ternary operator with the assignment operators.

There's an even better solution to puzzling out what the standard says: Just use parentheses.

Upvotes: 3

haccks
haccks

Reputation: 106022

The associativity rules of a language specify which operator is evaluated first when two operators with the same precedence are adjacent in an expression.

The precedence rules of a language specify which operator is evaluated first when two operators with different precedence are adjacent in an expression.

Wiki says:

[..] the associativity (or fixity) of an operator is a property that determines how operators of the same precedence are grouped in the absence of parentheses. If an operand is both preceded and followed by operators (for example, "^ 4 ^"), and those operators have equal precedence, then the operand may be used as input to two different operations (i.e. the two operations indicated by the two operators). The choice of which operations to apply the operand to, is determined by the "associativity" of the operators.

This means that operators are grouped among the operands and operands are grouped among the operators.

Upvotes: 0

Shoe
Shoe

Reputation: 76240

Given that operator and operands are different concepts, does the precedence and associativity rule group operator or operands ?

Both. It groups operands and the corresponding operator(s). In:

(a ~ b) ~ c

you are grouping a and b, which are operands, by the ~ operator. Precedence and associativity are properties only of the operand, though.

Upvotes: 2

Related Questions