digitalmars.D.learn - Simple operator precidence chart (and associativity)?
- Nick Sabalausky (5/5) Mar 13 2012 I'm reading through D's grammar spec, and maybe it's just not enough sle...
- Timon Gehr (26/33) Mar 13 2012 I don't think there is, but I think I can create one:
- Nick Sabalausky (9/30) Mar 13 2012 Thanks!
- Timon Gehr (29/43) Mar 14 2012 It is a special case. If a bitwise operator appears next to a relational...
- Nick Sabalausky (3/49) Mar 14 2012 Ok, I see. Thanks.
- Jos van Uden (7/32) Mar 14 2012 The TDPL has a list of expressions in decreasing precedence
- Timon Gehr (15/47) Mar 14 2012 Some guys want to deprecate <>, but I think it is actually quite nice.
- Stewart Gordon (5/11) Mar 14 2012 You forgot the conditional operator.
- Jonathan M Davis (7/13) Mar 13 2012 For all of the operators which come from C/C++, the operator precendence...
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?)
Mar 13 2012
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== != > < >= <= \ // 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)<< >>> // 8 shift operators(Also, for associativity: Assign and OpAssign are right-associative and everything else is left-associative, correct?)Power is right-associative too.
Mar 13 2012
"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:Thanks!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:== != > < >= <= \ // 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?Oh that's right. I keep forgetting we have that now.(Also, for associativity: Assign and OpAssign are right-associative and everything else is left-associative, correct?)Power is right-associative too.
Mar 13 2012
On 03/14/2012 01:20 AM, Nick Sabalausky wrote:"Timon Gehr"<timon.gehr gmx.ch> wrote in messageIt 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.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?== !=> < >=<= \ // 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)<< >>> // 8 shift operators
Mar 14 2012
"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:Ok, I see. Thanks."Timon Gehr"<timon.gehr gmx.ch> wrote in messageIt 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.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?== !=> < >=<= \ // 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)<< >>> // 8 shift operators
Mar 14 2012
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
Mar 14 2012
On 03/14/2012 01:23 PM, Jos van Uden wrote:On 14-3-2012 0:14, Timon Gehr wrote:Some guys want to deprecate <>, but I think it is actually quite nice. ^^ and ^^= were added after TDPL was released afaik.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 operatorsThe TDPL has a list of expressions in decreasing precedence on pages 61-63. Some of these operators, like <> or ^^= are not listed there.== != > < >= <= \ // 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)<< >>> // 8 shift operatorsIs 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.)
Mar 14 2012
On 13/03/2012 23:14, Timon Gehr wrote: <snip>You forgot the conditional operator. And the relational operators are non-associative (a == b == c or similar is illegal). Stewart.(Also, for associativity: Assign and OpAssign are right-associative and everything else is left-associative, correct?)Power is right-associative too.
Mar 14 2012
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
Mar 13 2012