# Precedence and Associativity

In the Relevance language, the operator precedence is fairly standard, e.g. multiplication has a higher precedence than addition, so 3+5*2 = 3+(5*2), not (3+5)*2:

```
Q: 3+5*2
A: 13
```

Parentheses, as expected, trump the other operators:

```
Q: (3+5)*2
A: 16
```

If two operators with the same precedence act on the same object, then a choice is made to associate first with either the left or right object. Addition and subtraction are left-associative, thus, 1+2-3+4 is processed as (((1+2)-3)+4).

Casting is also left-associative, so that ‘3 as string as integer’ is interpreted as (3 as string) as integer:

```
Q: 3 as string as integer
A: 3
I: singular integer
```

The following is a list of the language elements, from highest to lowest precedence, including associativity where appropriate:

Description | Grammatic Value | Associativity |
---|---|---|

parentheses | ( ) | |

casting operator | as | left |

unary operator | exists, not exists, not, - | |

products | *, /, mod, & | left |

addition | +, - | left |

relations | =, !=, <, <=, >, >= | |

AND | and | left |

OR | or | left |

Tuple | , | |

plural | ; | left |

Note there is no associativity listed for a relation, because multiple relation operators cannot appear in the same sub-expression. For example:

```
Q: 1 is 1 is 1
A: This expression could not be parsed.
```

Also, the tuple operator (comma) is right associative, but is not listed that way in the table because parentheses can change the association. For example, the first expression below is a triple, but the second is a pair:

```
Q: (1), (2), (3)
A: 1, 2, 3
Q: (1), (2,(3))
A: 1, ( 2, 3 )
```