ArticleslgStudy

science

Operator associativity

Operator associativity is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Operator associativity rather than just read about it. In short: In programming language theory, the associativity 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, ^ 3 ^), 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).

Key takeaways

  • Operator associativity belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Operator associativity to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Operator associativity from memory before moving on to harder problems.

Reference excerpt

In programming language theory, the associativity 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, ^ 3 ^), 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. Operators may be associative (meaning the operations can be grouped arbitrarily), left-associative (meaning the operations are grouped from the left), right-associative (meaning the operations are grouped from the right) or non-associative (meaning operations cannot be chained, often because the output type is incompatible with the input types). The associativity and precedence of an operator is a part of the definition of the programming language; different programming languages may have different associativity and precedence for the same type of operator. 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). If the operator is non-associative, the expression might be a syntax error, or it might have some special meaning. Some mathematical operators have inherent associativity. For example, subtraction and division, as used in conventional math notation, are inherently left-associative. Addition and multiplication, by contrast, are both left and right associative. (e.g. (a * b) * c = a * (b * c)). Many programming language manuals provide a table of operator precedence and associativity; see, for example, the table for C and C++. The concept of notational associativity described here is related to, but different from, the mathematical associativity. An operation that is mathematically associative, by definition requires no notational associativity. (For example, addition has the associative property, therefore it does not have to be either left associative or right associative.) An operation that is not mathematically associative, however, must be notationally left-, right-, or non-associative. (For example, subtraction does not have the associative property, therefore it must have notational associativity.)

Examples Associativity is only needed when the operators in an expression have the same precedence. Usually + and - have the same precedence. Consider the expression 7 - 4 + 2. The result could be either (7 - 4) + 2 = 5 or 7 - (4 + 2) = 1. The former result corresponds to the case when + and - are left-associative, the latter to when + and - are right-associative. In order to reflect normal usage, addition, subtraction, multiplication, and division operators are usually left-associative, while for an exponentiation operator (if present) there is no general agreement. Any assignment operators are typically right-associative. To prevent cases where operands would be associated with two operators, or no operator at all, operators with the same precedence must have the same associativity.

A detailed example Consider the expression 5^4^3^2, in which ^ is taken to be a right-associative exponentiation operator. A parser reading the tokens from left to right would apply the associativity rule to a branch, because of the right-associativity of ^, in the following way:

Term 5 is read. Nonterminal ^ is read. Node: "5^". Term 4 is read. Node: "5^4". Nonterminal ^ is read, triggering the right-associativity rule. Associativity decides node: "5^(4^". Term 3 is read. Node: "5^(4^3". Nonterminal ^ is read, triggering the re-application of the right-associativity rule. Node "5^(4^(3^". Term 2 is read. Node "5^(4^(3^2". No tokens to read. Apply associativity to produce parse tree "5^(4^(3^2))". This can then be evaluated depth-first, starting at the top node (the first ^):

The evaluator walks down the tree, from the first, over the second, to the third ^ expression. It evaluates as: 32 = 9. The result replaces the expression branch as the second operand of the second ^. Evaluation continues one level up the parse tree as: 49 = 262,144. Again, the result replaces the expression branch as the second operand of the first ^. Again, the evaluator steps up the tree to the root expression and evaluates as: 5262144 ≈ 6.2060699×10183230. The last remaining branch collapses and the result becomes the overall result, therefore completing overall evaluation. A left-associative evaluation would have resulted in the parse tree ((5^4)^3)^2 and the completely different result (6253)2 = 244,140,6252 ≈ 5.9604645×1016.

Right-associativity of assignment operators In many imperative programming languages, the assignment operator is defined to be right-associative, and assignment is defined to be an expression (which evaluates to a value), not just a statement. This allows chained assignment by using the value of one assignment expression as the right operand of the next assignment expression. In C, the assignment a = b is an expression that evaluates to the same value as the expression b converted to the type of a, with the side effect of storing the R-value of b into the L-value of a. Therefore the expression a = (b = c) can be interpreted as b = c; a = b;. The alternative expression (a = b) = c raises an error because a = b is not an L-value expression, i.e. it has an R-value but not an L-value where to store the R-value of c. The right-associativity of the = operator allows expressions such as a = b = c to be interpreted as a = (b = c). In C++, the assignment a = b is an expression that evaluates to the same value as the expression a, with the side effect of storing the R-value of b into the L-value of a. Therefore the expression a = (b = c) can still be interpreted as b = c; a = b;. And the alternative expression (a = b) = c can be interpreted as a = b; a = c; instead of raising an error. The right-associativity of the = operator allows expressions such as a = b = c to be interpreted as a = (b = c).

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Operator associativity

Start with the simplest possible case. Write down what Operator associativity claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Operator associativity before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Operator associativity ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Operator associativity

In research
Operator associativity appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Operator associativity in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Operator associativity is common in secondary-school and first-year university syllabi. It links to neighbouring topics Operators (programming), Parsing, Programming language topics, so understanding it makes those chapters shorter.
In everyday life
Look for Operator associativity outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Operator associativity in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Operator associativity means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Operator associativity out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Operator associativity in simple terms?

In programming language theory, the associativity 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, ^ 3 ^), and those operators have equal precedence, th…

Why does Operator associativity matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Operator associativity?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Operator associativity.

Tags

  • Operators (programming)
  • Parsing
  • Programming language topics

Keep exploring