Skip to content

Commit

Permalink
Explain associativity
Browse files Browse the repository at this point in the history
  • Loading branch information
henrikt-ma committed Feb 16, 2022
1 parent b9b64f0 commit 655f7e5
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions chapters/operatorsandexpressions.tex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ \section{Operator Precedence and Associativity}\label{operator-precedence-and-as

Operator precedence determines the order of evaluation of operators in an expression.
An operator with higher precedence is evaluated before an operator with lower precedence in the same expression.
For example, `\lstinline!*!' having higher precedence than `\lstinline!+!' means that \lstinline!1 + 2 * 3! becomes evaluated as \lstinline!1 + (2 * 3)!.

Operator associativity are used to determine the order of evaluation when operators have equal precedence.
Left associativity means that evaluation proceeds from left to right.
For example, left associativity of binary `\lstinline!-!' means that \lstinline!1 - 2 - 3! becomes evaluated as \lstinline!(1 - 2) - 3!.
Right associativity means evaluation from right to left, but there are no operators with this associativity in Modelica.
An operator may also be non-associative, meaning that evaluation order cannot be determined based on associativity.
For example, non-associativity of `\lstinline!<!' means that \lstinline!1 < 2 < 3! is an invalid expression.
Note that the operators don't need to be identical for associativity to matter; also \lstinline!1 == 2 < 3! is invalid, and \lstinline!1 - 2 + 3! becomes evaluated as \lstinline!(1 - 2) + 3!.

The following table presents the precedence and associativity of all the expression operators, consistent with and complementing information that can be derived from the Modelica grammar in \cref{modelica-concrete-syntax}.
\begin{table}[H]
Expand Down

0 comments on commit 655f7e5

Please sign in to comment.