Is operator precendence in standard

T

ToTo

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!
 
R

red floyd

ToTo said:
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].
 
V

Victor Bazarov

ToTo said:
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
 
J

James Kanze

ToTo wrote:
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: =, ,, ?:
 
T

terminator

ToTo said:
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.
 
A

Andrew Koenig

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
 
J

James Kanze

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,270
Messages
2,571,353
Members
48,038
Latest member
HunterDela

Latest Threads

Top