# Basic Expressions

Consider the expression `a+b`, which parses as:

```        			 ┌───┐
│ + │
└───┘
⠌   ⠡
⠌     ⠡
┌───┐     ┌───┐
│ a │     │ b │
└───┘     └───┘
```

# Basic Expressions

How does `a+b*c` parse?

```                  ┌───┐                       ┌───┐
│ * │                       │ + │
└───┘                       └───┘
⠌   ⠡                       ⠌   ⠡
⠌     ⠡                     ⠌     ⠡
┌───┐     ┌───┐             ┌───┐     ┌───┐
│ + │     │ c │             │ a │     │ * │
└───┘     └───┘             └───┘     └───┘
⠌   ⠡                                 ⠌   ⠡
⠌     ⠡                               ⠌     ⠡
┌───┐     ┌───┐                       ┌───┐     ┌───┐
│ a │     │ b │                       │ b │     │ c │
└───┘     └───┘                       └───┘     └───┘
```

The right-hand side, of course. `*` has higher precedence than `+`.

# Basic Expressions

Let’s use parentheses. How does `(a+b)*c` parse?

```                  ┌───┐                       ┌───┐
│ * │                       │ + │
└───┘                       └───┘
⠌   ⠡                       ⠌   ⠡
⠌     ⠡                     ⠌     ⠡
┌───┐     ┌───┐             ┌───┐     ┌───┐
│ + │     │ c │             │ a │     │ * │
└───┘     └───┘             └───┘     └───┘
⠌   ⠡                                 ⠌   ⠡
⠌     ⠡                               ⠌     ⠡
┌───┐     ┌───┐                       ┌───┐     ┌───┐
│ a │     │ b │                       │ b │     │ c │
└───┘     └───┘                       └───┘     └───┘
```

The left-hand side, of course. Parentheses mean “do this first”.

# Really

```              ┌───┐
│ * │
└───┘
⠌   ⠡
⠌     ⠡
┌───┐     ┌───┐
│ + │     │ c │
└───┘     └───┘
⠌   ⠡
⠌     ⠡
┌───┐     ┌───┐
│ a │     │ b │
└───┘     └───┘
```
• Parentheses do not mean “do this first”.
• Parentheses mean “group these together”.
• Parentheses determine the shape of the parse tree, not the order of evaluation.
• Of course, the shape of the tree determines the order of evaluation, to some extent.
• In this tree, the `+` node feeds to the `*` node, so `+` must execute before `*`.
• However, we can evaluate `c` before the addition, or after.

# Really

```              ┌───┐
│ * │
└───┘
⠌   ⠡
⠌     ⠡
┌───┐     ┌───┐
│ + │     │ c │
└───┘     └───┘
⠌   ⠡
⠌     ⠡
┌───┐     ┌───┐
│ a │     │ b │
└───┘     └───┘
```

Possible orders of evaluation:

1. a, b, c, +, *
2. a, b, +, c, *
3. a, c, b, +, *
4. b, a, c, +, *
5. b, a, +, c, *
6. b, c, a, +, *
7. c, a, b, +, *
8. c, b, a, +, *

# Who Cares?

• So what? It doesn’t matter in which order `a`, `b`, and `c` are fetched.
• If they’re simple variables, then, no, it doesn’t matter.
• If they’re expressions with side effects, then it does matter:
• increment/decrement
• assignment
• function calls
• could change global variables
• could generate output

# Some Operators Are Different

• Some operators have order of evaluation specified:
• `&& ||`
• `,` (in an expression, not as an argument separator)
• `?:`
• Most do not:
• `+ - * / %`
• `<< >> & | ^`
• `< > <= >= == !=`
• `= += -= *= /= %= <<= >>= &= |= ^=`

A user-defined operation (e.g., `operator+`) does not have order determined, because it’s a function called with two arguments. The order of evaluation of function arguments is unspecified.

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2018-04-24T16:55

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA