Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is operator precendence in standard

Reply
Thread Tools

Is operator precendence in standard

 
 
ToTo
Guest
Posts: n/a
 
      10-04-2007
Hello all!
Where can I find a real standard reference of the C++ operator precedence?
I presented to my students plainly the table presented in the book "The C++
Programmin Language" from Mr. Stroustrup (with my annotations). I have only
a working draft of the C++ standard where I could not find any such
table... Or does it mean it is _not_ part of the standard?

Thank you in advance!
 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      10-04-2007
ToTo wrote:
> Hello all!
> Where can I find a real standard reference of the C++ operator precedence?
> I presented to my students plainly the table presented in the book "The C++
> Programmin Language" from Mr. Stroustrup (with my annotations). I have only
> a working draft of the C++ standard where I could not find any such
> table... Or does it mean it is _not_ part of the standard?
>

There's no explicit table, but operator precedence can be determined
from the grammar in paragraph A.4 [gram.expr].
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-04-2007
ToTo wrote:
> Where can I find a real standard reference of the C++ operator
> precedence?


It's derivative of the grammar. Get a copy of the Standard and
read the grammar section (annex A).

> I presented to my students plainly the table presented in
> the book "The C++ Programmin Language" from Mr. Stroustrup (with my
> annotations). I have only a working draft of the C++ standard where I
> could not find any such table... Or does it mean it is _not_ part of
> the standard?


Pretty much, yes.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      10-05-2007
On Oct 4, 10:29 pm, red floyd <no.s...@here.dude> wrote:
> ToTo wrote:


> > Where can I find a real standard reference of the C++
> > operator precedence? I presented to my students plainly the
> > table presented in the book "The C++ Programmin Language"
> > from Mr. Stroustrup (with my annotations). I have only a
> > working draft of the C++ standard where I could not find any
> > such table... Or does it mean it is _not_ part of the
> > standard?


> There's no explicit table, but operator precedence can be
> determined from the grammar in paragraph A.4 [gram.expr].


There is no real operator precedence. Consider:

a = b ? c : d, e ; // precedence: ?:, =, ,
a ? b = c, d : e ; // precedence: =, ,, ?:

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 
Reply With Quote
 
terminator
Guest
Posts: n/a
 
      10-05-2007
On Oct 5, 12:38 pm, James Kanze <james.ka...@gmail.com> wrote:
> On Oct 4, 10:29 pm, red floyd <no.s...@here.dude> wrote:
>
> > ToTo wrote:
> > > Where can I find a real standard reference of the C++
> > > operator precedence? I presented to my students plainly the
> > > table presented in the book "The C++ Programmin Language"
> > > from Mr. Stroustrup (with my annotations). I have only a
> > > working draft of the C++ standard where I could not find any
> > > such table... Or does it mean it is _not_ part of the
> > > standard?

> > There's no explicit table, but operator precedence can be
> > determined from the grammar in paragraph A.4 [gram.expr].

>
> There is no real operator precedence. Consider:
>
> a = b ? c : d, e ; // precedence: ?:, =, ,
> a ? b = c, d : e ; // precedence: =, ,, ?:
>


this has nothing to do with precedence or assocaitivity ;conditional
is in priority but it needs its operands calculated before being
called , and since it is ternary the pair '?',':' behave as matching
braces and calculate the second parameter as if it were written with
paranthesis:

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

conditional is unique in being ternary.

In VC++`s documentation , platform specific stuff is enclosed between
'microsoft specific' & 'end microsoft' phrases and the table of
operators is not guarded in that way ; Although It may be a bug in the
compiler`s documentation , I guess that this matter is - either
explicitly or implicitly - well defined in the standard .
Furthermore, how can you write a program that claims for portability
unless operator precedence (read syntax) is standardized?
Could such a fundamental matter have been neglected by the standard?
Or maybe since all the compilers follow similar semantics(due to some
common compiler source code somehow) standard commitee has not felt
any need for clarification????

regards,
FM.

 
Reply With Quote
 
Andrew Koenig
Guest
Posts: n/a
 
      10-05-2007
"James Kanze" <> wrote in message
news: oups.com...
On Oct 4, 10:29 pm, red floyd <no.s...@here.dude> wrote:

> There is no real operator precedence. Consider:


> a = b ? c : d, e ; // precedence: ?:, =, ,
> a ? b = c, d : e ; // precedence: =, ,, ?:


Although it may not be obvious, it is possible to apply precedence rules
even in the case of ?:.

Here are the rules:

1) Replace every ? with ?( and every : with ):
2) It is now possible to speak unambiguously about the precedence of ?:
3) The precedence rule may be somewhat surprising:
?: has the same precedence as the assignment operators,
and, like the assignment operators, is right-associative.

These rules handle your examples as follows:

a = b ? c : d , e becomes a = b ?( c ): d , e because of rule (1) above
?: has higher precedence than , so this example is equivalent to
(a = b ?( c ): d), e

a ? b = c, d : e becomes a ?(b = c, d ): e because of rule (1) above
= has higher precedence than , so this example is equivalent to
a ?((b = c), d): e


 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      10-05-2007
Andrew Koenig wrote:
> "James Kanze" <> wrote in message
> news: oups.com...
> On Oct 4, 10:29 pm, red floyd <no.s...@here.dude> wrote:


> > There is no real operator precedence. Consider:


> > a = b ? c : d, e ; // precedence: ?:, =, ,
> > a ? b = c, d : e ; // precedence: =, ,, ?:


> Although it may not be obvious, it is possible to apply precedence rules
> even in the case of ?:.


> Here are the rules:


> 1) Replace every ? with ?( and every : with ):
> 2) It is now possible to speak unambiguously about the precedence of ?:
> 3) The precedence rule may be somewhat surprising:
> ?: has the same precedence as the assignment operators,
> and, like the assignment operators, is right-associative.


That's a clever solution. I was aware, more or less, that the
motivation behind the anomaly (which is manifest in the standard
by a forward reference in the grammar) was due to the fact that
? and : do sort of work like braces---a ? must have a following
: somewhere, so there's no problem allowing operators with lower
precedence between them. But it never occured to me to use a
simple rewrite to analyse it.

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Question about precendence: "-" vs "<<". Joshua Maurice C++ 2 02-28-2010 10:02 PM
Re: Question about precendence: "-" vs "<<". tonydee C++ 1 02-28-2010 12:55 PM
What are the standard network functions provided in standard C? disappearedng@gmail.com C Programming 5 06-10-2008 08:57 PM
add pexpect to the standard library, standard "install" mechanism. funkyj Python 5 01-20-2006 08:35 PM
How standard is the standard library? steve.leach Python 1 04-18-2005 04:07 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57