Simple operator precidence chart (and associativity)?

Timon Gehr timon.gehr at gmx.ch
Wed Mar 14 02:01:19 PDT 2012


On 03/14/2012 01:20 AM, Nick Sabalausky wrote:
> "Timon Gehr"<timon.gehr at 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.


More information about the Digitalmars-d-learn mailing list