Topic: Why did ?: and = exchange priorities?


Author: Jonathan.Coxhead@sv.sc.philips.com (Jonathan R. Coxhead)
Date: 1999/11/30
Raw View
   bs@research.att.com (Bjarne Stroustrup) writes:

 | I provide the table as an aid to memory, but it is not authoratative.
 | It couldn't be. In particular, as I state immediately below that table,
 | the rules for ? and = cannot be expressed in terms of precedence.

   I remember a news article from a while ago that pointed out that there
actually *is* a precedence rule that describes this phenomenon:

      '=' and '?:' have equal precedence, and associate to the right

   This give the following interpretations (where 'x' is any expression):

      Comparing '='  with '=',  'a =   b =   c' is 'a =   (b =   c)'.
      Comparing '='  with '?:', 'a =   b ?x: c' is 'a =   (b ?x: c)'.
      Comparing '?:' with '=',  'a ?x: b =   c' is 'a ?x: (b =   c)'.
      Comparing '?:' with '?:', 'a ?x: b ?x: c' is 'a ?x: (b ?x: c)'.

   This is for C++ only: in C, '?:' has higher precedence, so the two rules that
compare an operator to itself are the same:

      Comparing '='  with '=',  'a =   b =   c' is 'a =   (b =   c)'.
      Comparing '?:' with '?:', 'a ?x: b ?x: c' is 'a ?x: (b ?x: c)'.

   The others are:

      Comparing '='  with '?:', 'a =   b ?x: c' is 'a = (b ?x: c)'.
      Comparing '?:' with '=',  'a ?x: b =   c' is '(a ?x: b) = c'.

and the first is the same as C++, but the last is a constraint violation in C,
because '?:' cannot yield an lvalue.

   Maybe something for the next edition of the book? :-)

        /|
 o o o (_|/
        /|
       (_/




[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: bs@research.att.com (Bjarne Stroustrup)
Date: 1999/11/25
Raw View

 > Ronny <ronald_fNOroSPAM@myremarq.com.invalid> write:
 >

 > In article <199911212100.QAA05752@crete.cs.brown.edu>, er@cs.brown.edu
 > (Manos Renieris) wrote:
 > > Sometime between Stroustrup 2nd and the standard, the ternary
 > > operator
 > > ? : and the assignment operators exchanged priorities. Why did this
 > > happen (and when exactly)?
 >
 > How did you come to the conclusion that this has changed? I looked at
 > the ARM 2nd ed and did not find any precedence table at all (if you
 > know where it is, please tell me the page number), but there are
 > examples that suggest ?: having a higher precedence than assignment.
 > In Stroustrup's "C++ Programming Language 3rd ed." on page  121, which
 > covers the standard, ?: has higher priority as well. I checked the
 > standard but again did not find a precedence table.

That's because C++'s grammar isn't an operator precedence grammar.
The C++ grammar is defined by the grammar rules. You can find them
in Appexdix A of the standard of my 3rd edition.

I provide the table as an aid to memory, but it is not authoratative.
It couldn't be. In particular, as I state immediately below that table,
the rules for ? and = cannot be expressed in terms of precedence.


 > Interestingly, http://devcentral.iftech.com/learning/tutorials/c-cpp/c/
 > says that in C, assignment is higher than ?:, which would make
 >
 >  x=p?y:z
 >
 > parse as
 >
 >  (x=p)?y:z
 >
 > so if there was a change in this topic, it would be on the transition
 > from C to C++, not from Classic C++ to ISO C++. I don't have a C
 > standard at hand, but my guess is that even in C, precendence was the
 > same and the above mentioned page got it wrong.

Yes,
 x=p?y:x;

means

 x = (p?y:x);

in all vintages of C and C++.

There was a clarification of the grammar related to ?: in the transition
of ARM C++ to ISO C++. I do not know if C9x introduced a similar resolution.

 - Bjarne
Bjarne Stroustrup - http://www.research.att.com/~bs




[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: 1999/11/27
Raw View
David R Tribble wrote:
>
> In any case, mixing the '?:' and the '=' operators should be done
> using parentheses appropriately.  Otherwise your maintenance
> programmers will shoot you.

I'll be long gone by then :-)

Seriously, I do use ?: somewhat more often than the typically
entry-level programmers I supervise. Being familiar with it, I use
somewhat fewer parentheses in its vicinity than they would. However, I'm
not advocating leaving out every unnecessary pair of parentheses; I was
just explaining what happens if you do.

"appropriately" covers a lot of ground. What do you consider
appropriate? The most extreme policy I could imagine would be to put
parenthesis around any operand that contains an operator, plus another
set around the conditional-expression itself. That would be going a
little overboard by my standards.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: er@cs.brown.edu (Manos Renieris)
Date: 1999/11/23
Raw View
On 22 Nov 1999 17:48:33 GMT,
Ronny <ronald_fNOroSPAM@myremarq.com.invalid> wrote:

>
>In article <199911212100.QAA05752@crete.cs.brown.edu>, er@cs.brown.edu
>(Manos Renieris) wrote:
>> Sometime between Stroustrup 2nd and the standard, the ternary
>> operator
>> ? : and the assignment operators exchanged priorities. Why did this
>> happen (and when exactly)?
>
>How did you come to the conclusion that this has changed? I looked at
>the ARM 2nd ed and did not find any precedence table at all (if you
>know where it is, please tell me the page number), but there are
>examples that suggest ?: having a higher precedence than assignment.

K&R 1, page 49.
K&R 2, page 53.
Stroustrup 2nd, page 90.
Stroustrup 3rd, page 121.

The standard agrees with Stroustrup 3rd. There is mention of
a grammar change in the on line version of the final chapter of
Stroustrup 2nd, (www.research.att.com/~bs/2nd.html), on pages
642-643.

-- Manos


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: 1999/11/23
Raw View
David B Anderson wrote:
....
>   ?: was higher then = in K&R, first edition.
> And is so in the C std. And in the C++ std.

The current standards for C and C++ are not described in terms of
operator precedence. Instead, the equivalent information is encoded in
the grammar productions. The net effect isn't quite the same; grammar
rules can express more complicated relationships than can be described
simply by operator precedence. As an example, try to describe the
following rules in terms of precedence. You'll have some trouble.

The two languages have somewhat different rules for the ?: operator. In
C99, the relevant productions are:


(6.5.15) _conditional-expression_:
 _logical-OR-expression_
 _logical-OR-expression_ ? _expression_ : _conditional-expression_

(6.5.16) _assignment-expression_:
 _conditional-expression_
 _unary-expression_ _assignment-operator_ _assignment-expression_

(6.5.17) _expression_:
 _assignment-expression_

Thus, C would parse

 a=b?c=d:e=f

as

 a=((b?(c=d):e)=f)

This is because a=b doesn't qualify as a unary-expression, and e=f
doesn't qualify as a conditional-expression, but c=d does qualify as an
expression.

Note that the second assignment would be a constraint violation, because
a conditional expression in C cannot yield an lvalue, and the left
operand of an assignment operator must be an lvalue.

In C++, on the other hand, the relevant production are:

(5.16) _conditional-expression_:
 _logical-or-expression_
 _logical-or-expression_ ? _expression_ : _assignment-expression_

(5.17) _assignment-expression_:
 _logical_or_expression_ _assignment-operator_ _assignment-expression_

The productions for _logical-or-expression_, and _expression_, are
essentially the same for this purpose as for C. This means that C++
parses that same expression as:

 a=(b?(c=d):(e=f))

Furthermore, in C++, unlike C, a conditional expression can yield an
lvalue, so

 (a?b:c)=d

need not be a constraint violation, if b and c are both lvalues.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: 1999/11/24
Raw View
"James Kuyper Jr." wrote:
....
> Thus, C would parse
>
>         a=b?c=d:e=f
>
> as
>
>         a=((b?(c=d):e)=f)
>
> This is because a=b doesn't qualify as a unary-expression, and e=f
> doesn't qualify as a conditional-expression, but c=d does qualify as an
> expression.

I wrote that just before going to bed, and realized when I woke up that
I'd goofed up. My example got too complicated. "a?b:c=d" is simply a
syntax error in C. "a?b:c" doesn't qualify as the unary-expression
required for the first operand of the "=" operator, and "c=d" doesn't
qualify as the conditional-expression required for the third operand of
the "?:" operator, so neither way of grouping the operators works. The
best example using legal syntax would be

 a=b?c=d:e

which would be parsed as:

 a=(b?(c=d):e)

This mistake only applies to my C example. The expression "a?b:c=d" is
legal C++, even though "a?b:c" doesn't qualify as the
logical-or-expression required for the first operand of the "="
operator. That's because "c=d" does qualify as the assignment-expression
required for the third operand of the "?:" operator.






Author: David R Tribble <david@tribble.com>
Date: 1999/11/24
Raw View
In any case, mixing the '?:' and the '=' operators should be done
using parentheses appropriately.  Otherwise your maintenance
programmers will shoot you.

-- David R. Tribble, david@tribble.com, http://david.tribble.com --

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: er@cs.brown.edu (Manos Renieris)
Date: 1999/11/21
Raw View
Sometime between Stroustrup 2nd and the standard, the ternary operator
? : and the assignment operators exchanged priorities. Why did this
happen (and when exactly)?


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Ronny <ronald_fNOroSPAM@myremarq.com.invalid>
Date: 1999/11/22
Raw View
In article <199911212100.QAA05752@crete.cs.brown.edu>, er@cs.brown.edu
(Manos Renieris) wrote:
> Sometime between Stroustrup 2nd and the standard, the ternary
> operator
> ? : and the assignment operators exchanged priorities. Why did this
> happen (and when exactly)?

How did you come to the conclusion that this has changed? I looked at
the ARM 2nd ed and did not find any precedence table at all (if you
know where it is, please tell me the page number), but there are
examples that suggest ?: having a higher precedence than assignment.
In Stroustrup's "C++ Programming Language 3rd ed." on page  121, which
covers the standard, ?: has higher priority as well. I checked the
standard but again did not find a precedence table.

Interestingly, http://devcentral.iftech.com/learning/tutorials/c-cpp/c/
says that in C, assignment is higher than ?:, which would make

 x=p?y:z

parse as

 (x=p)?y:z

so if there was a change in this topic, it would be on the transition
from C to C++, not from Classic C++ to ISO C++. I don't have a C
standard at hand, but my guess is that even in C, precendence was the
same and the above mentioned page got it wrong.


* Sent from RemarQ http://www.remarq.com The Internet's Discussion Network *
The fastest and easiest way to search and participate in Usenet - Free!


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: davea@quasar.engr.sgi.com (David B Anderson)
Date: 1999/11/22
Raw View
In article <000b8d9b.c545ff39@usw-ex0102-014.remarq.com>,
Ronny  <ronald_fNOroSPAM@myremarq.com.invalid> wrote:
>
>In article <199911212100.QAA05752@crete.cs.brown.edu>, er@cs.brown.edu
>(Manos Renieris) wrote:
>> Sometime between Stroustrup 2nd and the standard, the ternary
>> operator
>> ? : and the assignment operators exchanged priorities. Why did this
>> happen (and when exactly)?
>
>How did you come to the conclusion that this has changed? I looked at
>the ARM 2nd ed and did not find any precedence table at all (if you
>know where it is, please tell me the page number), but there are
>examples that suggest ?: having a higher precedence than assignment.
>In Stroustrup's "C++ Programming Language 3rd ed." on page  121, which
>covers the standard, ?: has higher priority as well. I checked the
>standard but again did not find a precedence table.
>
>Interestingly, http://devcentral.iftech.com/learning/tutorials/c-cpp/c/
>says that in C, assignment is higher than ?:, which would make

No, that web page says :? is higher than = and that is correct.

  ?: was higher then = in K&R, first edition.
And is so in the C std. And in the C++ std.
And on
http://devcentral.iftech.com/learning/tutorials/c-cpp/c/15.asp

> x=p?y:z
>
>parse as
>
> (x=p)?y:z
>
>so if there was a change in this topic, it would be on the transition

No change.

>from C to C++, not from Classic C++ to ISO C++. I don't have a C
>standard at hand, but my guess is that even in C, precendence was the
>same and the above mentioned page got it wrong.

davea@sgi.com


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]