Search
Simple operator precidence chart (and associativity)?
Mar 13, 2012
Nick Sabalausky
Mar 13, 2012
Timon Gehr
Mar 14, 2012
Nick Sabalausky
Mar 14, 2012
Timon Gehr
Mar 14, 2012
Nick Sabalausky
Mar 14, 2012
Jos van Uden
Mar 14, 2012
Timon Gehr
Mar 14, 2012
Stewart Gordon
Mar 14, 2012
Jonathan M Davis
```I'm reading through D's grammar spec, and maybe it's just not enough sleep or some such, but my brain is turning to mud:

Is there a simple operator precidence chart somewhere? (Also, for associativity: Assign and OpAssign are right-associative and everything else is left-associative, correct?)

```
```On 03/13/2012 11:29 PM, Nick Sabalausky wrote:
> I'm reading through D's grammar spec, and maybe it's just not enough sleep
> or some such, but my brain is turning to mud:
>
> Is there a simple operator precidence chart somewhere?

I don't think there is, but I think I can create one:

!                 // 1 template instantiation
=>                // 2 goesto, binds weaker to the right
. ++ -- ( [       // 3 postfix operators
^^                // 4 power (right-associative)
& ++ -- * - + ! ~ // 5 prefix operators
* / %             // 6 multiplicative operators
+ - ~             // 7 additive operators
>> << >>>         // 8 shift operators
== != > < >= <= \ // 9 relational operators
!> !< !>= !<= <>\
!<> <>= !<>= in \
!in is !is
&                 // 10 bitwise AND (ambiguous with 9)
^                 // 11 bitwise XOR (ambiguous with 9)
|                 // 12 bitwise OR  (ambiguous with 9)
&&                // 13 logical AND
||                // 14 logical OR
?:                // 15 conditional operator
/= &= |= -= += \  // 16 assignment operators (right-associative)
<<= >>= >>>= = \
*= %= ^= ^^= ~=
=>                // 17 goesto, binds stronger to the left
,                 // 18 comma operator
..                // range (not actually an operator)

> (Also, for
> associativity: Assign and OpAssign are right-associative and everything else
> is left-associative, correct?)
>
>

Power is right-associative too.
```
```On Tuesday, March 13, 2012 18:29:03 Nick Sabalausky wrote:
> I'm reading through D's grammar spec, and maybe it's just not enough sleep or some such, but my brain is turning to mud:
>
> Is there a simple operator precidence chart somewhere? (Also, for associativity: Assign and OpAssign are right-associative and everything else is left-associative, correct?)

For all of the operators which come from C/C++, the operator precendence chart from C/C++ should work:

http://en.cppreference.com/w/cpp/language/operator_precedence

It's how the D-specific ones fit in that gets harder to figure out. IIRC TDPL has an operator precedence table though, so you should be able to look there.

- Jonathan M Davis
```
```"Timon Gehr" <timon.gehr@gmx.ch> wrote in message news:jjokc7\$t3j\$1@digitalmars.com...
> On 03/13/2012 11:29 PM, Nick Sabalausky wrote:
>> I'm reading through D's grammar spec, and maybe it's just not enough
>> sleep
>> or some such, but my brain is turning to mud:
>>
>> Is there a simple operator precidence chart somewhere?
>
>
> I don't think there is, but I think I can create one:
>

Thanks!

> == != > < >= <= \ // 9 relational operators
> !> !< !>= !<= <>\
> !<> <>= !<>= in \
> !in is !is
> &                 // 10 bitwise AND (ambiguous with 9)
> ^                 // 11 bitwise XOR (ambiguous with 9)
> |                 // 12 bitwise OR  (ambiguous with 9)

Ah, see, that's what I was tripping up on. I thought there seemed to be something slightly odd going on with those (and then from there I started second-guessing everything). It seemed almost like these had both ordered priorities *and* the same priority.

So how exactly does this part work then?

>
>> (Also, for
>> associativity: Assign and OpAssign are right-associative and everything
>> else
>> is left-associative, correct?)
>>
>>
>
> Power is right-associative too.

Oh that's right. I keep forgetting we have that now.

```
```On 03/14/2012 01:20 AM, Nick Sabalausky wrote:
> "Timon Gehr"<timon.gehr@gmx.ch>  wrote in message

>> >> << >>>         // 8 shift operators
>> == !=>  <  >=<= \ // 9 relational operators
>> !>  !<  !>= !<=<>\
>> !<>  <>= !<>= in \
>> !in is !is
>> &                 // 10 bitwise AND (ambiguous with 9)
>> ^                 // 11 bitwise XOR (ambiguous with 9)
>> |                 // 12 bitwise OR  (ambiguous with 9)
>
> Ah, see, that's what I was tripping up on. I thought there seemed to be
> something slightly odd going on with those (and then from there I started
> second-guessing everything). It seemed almost like these had both ordered
> priorities *and* the same priority.
>
> So how exactly does this part work then?
>

It is a special case. If a bitwise operator appears next to a relational operator, then the expression has to be parenthesised for disambiguation. You can think of it as a partial order:

...
|
shift operators
^ ^
/   \
/  bitwise AND
/       \
/      bitwise XOR
/            \
relational operators      bitwise OR
^           ^
\         /
\       /
\     /
\   /
logical AND
^
|
logical OR
^
|
...

The precedence of bitwise operators cannot be compared with the precedence of relational operators, and D disallows programs that cannot be parsed unambiguously.
```
```"Timon Gehr" <timon.gehr@gmx.ch> wrote in message news:jjpmov\$305u\$1@digitalmars.com...
> On 03/14/2012 01:20 AM, Nick Sabalausky wrote:
>> "Timon Gehr"<timon.gehr@gmx.ch>  wrote in message
>
>>> >> << >>>         // 8 shift operators
>>> == !=>  <  >=<= \ // 9 relational operators
>>> !>  !<  !>= !<=<>\
>>> !<>  <>= !<>= in \
>>> !in is !is
>>> &                 // 10 bitwise AND (ambiguous with 9)
>>> ^                 // 11 bitwise XOR (ambiguous with 9)
>>> |                 // 12 bitwise OR  (ambiguous with 9)
>>
>> Ah, see, that's what I was tripping up on. I thought there seemed to be something slightly odd going on with those (and then from there I started second-guessing everything). It seemed almost like these had both ordered priorities *and* the same priority.
>>
>> So how exactly does this part work then?
>>
>
> It is a special case. If a bitwise operator appears next to a relational operator, then the expression has to be parenthesised for disambiguation. You can think of it as a partial order:
>
>                     ...
>                      |
>               shift operators
>                     ^ ^
>                    /   \
>                   /  bitwise AND
>                  /       \
>                 /      bitwise XOR
>                /            \
> relational operators      bitwise OR
>                 ^           ^
>                  \         /
>                   \       /
>                    \     /
>                     \   /
>                  logical AND
>                       ^
>                       |
>                  logical OR
>                       ^
>                       |
>                      ...
>
> The precedence of bitwise operators cannot be compared with the precedence of relational operators, and D disallows programs that cannot be parsed unambiguously.

Ok, I see. Thanks.

```
```On 14-3-2012 0:14, Timon Gehr wrote:
> I don't think there is, but I think I can create one:
>
> ! // 1 template instantiation
> => // 2 goesto, binds weaker to the right
> . ++ -- ( [ // 3 postfix operators
> ^^ // 4 power (right-associative)
> & ++ -- * - + ! ~ // 5 prefix operators
> * / % // 6 multiplicative operators
> + - ~ // 7 additive operators
>  >> << >>> // 8 shift operators
> == != > < >= <= \ // 9 relational operators
> !> !< !>= !<= <>\
> !<> <>= !<>= in \
> !in is !is
> & // 10 bitwise AND (ambiguous with 9)
> ^ // 11 bitwise XOR (ambiguous with 9)
> | // 12 bitwise OR (ambiguous with 9)
> && // 13 logical AND
> || // 14 logical OR
> ?: // 15 conditional operator
> /= &= |= -= += \ // 16 assignment operators (right-associative)
> <<= >>= >>>= = \
> *= %= ^= ^^= ~=
> => // 17 goesto, binds stronger to the left
> , // 18 comma operator
> .. // range (not actually an operator)

The TDPL has a list of expressions in decreasing precedence
on pages 61-63. Some of these operators, like <> or ^^= are
not listed there.

Is there a difference between != and <> ?

Why is the "goesto" operator listed twice? Is that accidental?

Jos

```
```On 03/14/2012 01:23 PM, Jos van Uden wrote:
> On 14-3-2012 0:14, Timon Gehr wrote:
>> I don't think there is, but I think I can create one:
>>
>> ! // 1 template instantiation
>> => // 2 goesto, binds weaker to the right
>> . ++ -- ( [ // 3 postfix operators
>> ^^ // 4 power (right-associative)
>> & ++ -- * - + ! ~ // 5 prefix operators
>> * / % // 6 multiplicative operators
>> + - ~ // 7 additive operators
>> >> << >>> // 8 shift operators
>> == != > < >= <= \ // 9 relational operators
>> !> !< !>= !<= <>\
>> !<> <>= !<>= in \
>> !in is !is
>> & // 10 bitwise AND (ambiguous with 9)
>> ^ // 11 bitwise XOR (ambiguous with 9)
>> | // 12 bitwise OR (ambiguous with 9)
>> && // 13 logical AND
>> || // 14 logical OR
>> ?: // 15 conditional operator
>> /= &= |= -= += \ // 16 assignment operators (right-associative)
>> <<= >>= >>>= = \
>> *= %= ^= ^^= ~=
>> => // 17 goesto, binds stronger to the left
>> , // 18 comma operator
>> .. // range (not actually an operator)
>
> The TDPL has a list of expressions in decreasing precedence
> on pages 61-63. Some of these operators, like <> or ^^= are
> not listed there.

Some guys want to deprecate <>, but I think it is actually quite nice. ^^ and ^^= were added after TDPL was released afaik.

>
> Is there a difference between != and <> ?

Yes, != gives true when at least one of the arguments is NAN, while <> gives false if at least one of the arguments is NAN.

!= means not equal.
<> means ordered, but not equal.

There is also <>=, which just means ordered, etc.

>
> Why is the "goesto" operator listed twice? Is that accidental?
>

That is intentional. It binds stronger to the left than to the right, for example:

x+x=>x+x

will be parsed as:

x+(x=>(x+x))

(It could be argued that => is not a proper 'operator', but an operator precedence parser can be slightly more efficient if it is treated as one.)

```
```On 13/03/2012 23:14, Timon Gehr wrote:
<snip>
>> (Also, for
>> associativity: Assign and OpAssign are right-associative and everything else
>> is left-associative, correct?)
>>
>>
>
> Power is right-associative too.

You forgot the conditional operator.

And the relational operators are non-associative (a == b == c or similar is illegal).

Stewart.
```