473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

relative precedence of ?: and assignment

It has been pointed out to me that various C++ books disagree about
the relative precedence of ?: and the assignment operators.

In order to satisfy myself about the matter once and for all,
I looked at the grammar in the C++ standard. The relative fragments
are as follows:

conditional-expression:
logical-or-expression
logical-or-expression ? expression : assignment-expression

assignment-expression:
conditional-expression
logical-or-expression assignment-operator assignment-expression
throw-expression

Ordinarily, this grammar is almost, but not quite, in a well-known
form that, were it adhered to exactly, would say that
conditional-expressions have higher precedence than
assignment-expressions.

However, there is one crucial difference from this form: The rightmost
token in the second line of "conditional-expression" is
"assignment-expression" rather than "conditional-expression".
This difference makes the grammar somewhat harder to understand in
terms of precedence.

However, if we take the definition of assignment-expression and
replace the use of conditional-expression by its alternatives, we get:

assignment-expression:
logical-or-expression
logical-or-expression ? expression : assignment-expression
logical-or-expression assignment-operator assignment-expression
throw-expression

which we can rewrite this way:

cond-or-asn-op:
assignment-operator
? expression :

assignment-expression:
logical-or-expression
logical-or-expression cond-or-asn-op assignment-expression
throw-expression

From this rewrite, it should be clear that the assignment and ?:
operators have the same precedence, and they are right-associative.

Am I missing anything? If not, I'd like to urge all authors and
teachers of C++ to describe the precedence of these operators this
way; I think it's much easier to understand than any alternatives I've
seen.
--
Andrew Koenig, ar*@acm.org
Jul 19 '05 #1
10 3276

Andrew Koenig wrote: <about "teaching">
[...]
Am I missing anything?


Uhmm,

"The syntax specifies the precedence of operators in the evaluation
of an expression, which is the same as the order of the major
subclauses of this subclause, highest precedence first. Thus, for
example, the expressions allowed as the operands of the binary +
operator (6.5.6) are those expressions defined in 6.5.1 through
6.5.6. The exceptions are cast expressions (6.5.4) as operands of
unary operators (6.5.3), and an operand contained between any of
the following pairs of operators: grouping parentheses () (6.5.1),
subscripting brackets [] (6.5.2.1), function-call parentheses ()
(6.5.2.2), and the conditional operator ?: (6.5.15)."

regards,
alexander.

--
"Good job SUN -- you've soiled yourselves with the stink of the
new pariah. The enemy of your enemy in this case was not your
friend. I hope IBM buys your sorry assets out, because we're
done with you."
-- "teambpsi" @ slashdot
Jul 19 '05 #2
Alexander> "The syntax specifies the precedence of operators in the
Alexander> evaluation of an expression, which is the same as the order
Alexander> of the major subclauses of this subclause, highest
Alexander> precedence first. Thus, for example, the expressions
Alexander> allowed as the operands of the binary + operator (6.5.6)
Alexander> are those expressions defined in 6.5.1 through 6.5.6. The
Alexander> exceptions are cast expressions (6.5.4) as operands of
Alexander> unary operators (6.5.3), and an operand contained between
Alexander> any of the following pairs of operators: grouping
Alexander> parentheses () (6.5.1), subscripting brackets [] (6.5.2.1),
Alexander> function-call parentheses () (6.5.2.2), and the conditional
Alexander> operator ?: (6.5.15)."

Regardless of what the descriptive material may say, it does not add
any additional constraints to the grammar. Putting it differently,
if the grammar requires an expression to be parsed in a particular
way, then that is the way it's parsed.

--
Andrew Koenig, ar*@acm.org
Jul 19 '05 #3

Andrew Koenig wrote:

Alexander> "The syntax specifies the precedence of operators in the
Alexander> evaluation of an expression, which is the same as the order
Alexander> of the major subclauses of this subclause, highest
Alexander> precedence first. Thus, for example, the expressions
Alexander> allowed as the operands of the binary + operator (6.5.6)
Alexander> are those expressions defined in 6.5.1 through 6.5.6. The
Alexander> exceptions are cast expressions (6.5.4) as operands of
Alexander> unary operators (6.5.3), and an operand contained between
Alexander> any of the following pairs of operators: grouping
Alexander> parentheses () (6.5.1), subscripting brackets [] (6.5.2.1),
Alexander> function-call parentheses () (6.5.2.2), and the conditional
Alexander> operator ?: (6.5.15)."

Regardless of what the descriptive material may say, it does not add
any additional constraints to the grammar. Putting it differently,
if the grammar requires an expression to be parsed in a particular
way, then that is the way it's parsed.


You might want to take a look at *C* grammer. Or am I just missing
and/or misunderstanding something?

regards,
alexander.

--
"Freedom is a perceived necessity. Freedom of speech is NOT saying
whatever you want. The sooner as you realize that, the better your
life is going to be."
-- Victor Bazarov
Jul 19 '05 #4
Alexander Terekhov wrote:

You might want to take a look at *C* grammer. Or am I just missing
and/or misunderstanding something?


C and C++ are different here. Deliberately.

--

"To delight in war is a merit in the soldier,
a dangerous quality in the captain, and a
positive crime in the statesman."
George Santayana

"Bring them on."
George W. Bush
Jul 19 '05 #5
Alexander Terekhov wrote:

Pete Becker wrote:

Alexander Terekhov wrote:

You might want to take a look at *C* grammer. Or am I just missing
and/or misunderstanding something?


C and C++ are different here. Deliberately.


Heck. Why there should be any difference with respect to "relative
precedence of ?: and assignment" in C and C++?????


Because we changed it.

a ? b : c = 3;

In C, b is unchanged, regardless of the value of a. In C++, depending on
the value of a, 3 is assigned to either b or c.

--

"To delight in war is a merit in the soldier,
a dangerous quality in the captain, and a
positive crime in the statesman."
George Santayana

"Bring them on."
George W. Bush
Jul 19 '05 #6

Pete Becker wrote:

Alexander Terekhov wrote:

Pete Becker wrote:

Alexander Terekhov wrote:
>
> You might want to take a look at *C* grammer. Or am I just missing
> and/or misunderstanding something?
>

C and C++ are different here. Deliberately.


Heck. Why there should be any difference with respect to "relative
precedence of ?: and assignment" in C and C++?????


Because we changed it.


Interesting. Very interesting. It seems that both Annex C and
http://david.tribble.com/text/cdiffs.htm know *nothing* about
that change. Oder?

regards,
alexander.
Jul 19 '05 #7
Pete Becker wrote:

Alexander Terekhov wrote:

Pete Becker wrote:

Alexander Terekhov wrote:
>
> You might want to take a look at *C* grammer. Or am I just missing
> and/or misunderstanding something?
>

C and C++ are different here. Deliberately.


Heck. Why there should be any difference with respect to "relative
precedence of ?: and assignment" in C and C++?????


Because we changed it.


Apparently we didn't. <g> There was a lengthy discussion of how to do
it, complete with ad hoc BNF on whiteboards. Looks like it didn't make
it into the standard.

--

"To delight in war is a merit in the soldier,
a dangerous quality in the captain, and a
positive crime in the statesman."
George Santayana

"Bring them on."
George W. Bush
Jul 19 '05 #8

Pete Becker wrote:
[...]
Heck. Why there should be any difference with respect to "relative
precedence of ?: and assignment" in C and C++?????
Because we changed it.


Apparently we didn't. <g>


Thanks god. ;-)
There was a lengthy discussion of how to do
it, complete with ad hoc BNF on whiteboards. Looks like it didn't make
it into the standard.


<from my email archive>

"Official/legal" C may allow use of conditional expressions as
lvalues in the future, oder? I really want to have *the same*
rules in both C and C++ with respect to "relative precedence of
?: and assignment".

regards,
alexander.
Jul 19 '05 #9
Alexander> "Official/legal" C may allow use of conditional expressions
Alexander> as lvalues in the future, oder? I really want to have *the
Alexander> same* rules in both C and C++ with respect to "relative
Alexander> precedence of ?: and assignment".

The question I am addressing is not what you or I want, but what the
C++ standard actually says.

--
Andrew Koenig, ar*@acm.org
Jul 19 '05 #10

"Andrew Koenig" <ar*@acm.org> wrote in message news:yu**************@tinker.research.att.com...
Alexander> "The syntax specifies the precedence of operators in the
Alexander> evaluation of an expression, which is the same as the order
Alexander> of the major subclauses of this subclause, highest
Alexander> precedence first. Thus, for example, the expressions
Alexander> allowed as the operands of the binary + operator (6.5.6)
Alexander> are those expressions defined in 6.5.1 through 6.5.6. The
Alexander> exceptions are cast expressions (6.5.4) as operands of
Alexander> unary operators (6.5.3), and an operand contained between
Alexander> any of the following pairs of operators: grouping
Alexander> parentheses () (6.5.1), subscripting brackets [] (6.5.2.1),
Alexander> function-call parentheses () (6.5.2.2), and the conditional
Alexander> operator ?: (6.5.15)."

Regardless of what the descriptive material may say, it does not add
any additional constraints to the grammar. Putting it differently,
if the grammar requires an expression to be parsed in a particular
way, then that is the way it's parsed.

One thing people must realize is that the precedence ordering is noit
the same as the grammar. You can't strictly reflect the grammar in
this case with the table.
Jul 19 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

47
by: Jeff Relf | last post by:
Hi All, I plan on using the following C++ code to create nodes with unlimited children: // I would like to declare NodeT like this, // but it won't compile because Lnk_T is not defined yet....
4
by: Master of C++ | last post by:
Hi, This is a simple question. In the following example, .. class Vector .. { .. private: .. int *Numbers; .. int vLength; ..
20
by: Vivek N | last post by:
Hi Folks, This question may well have been asked and answered before. But, sorry that I couldn't find one from the archives. I typed up this program and compiled it with gcc 3.3.2 main() { int...
25
by: noe | last post by:
Hello, I'm writing a file system filter driver and I've found in an example this sentence: if (VALID_FAST_IO_DISPATCH_HANDLER( fastIoDispatch, FastIoRead )) { return...
11
by: Jon Shemitz | last post by:
The new ?? operator doesn't seem to be in the precedence table in VS.05 <ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_csref/html/214e7b83-1a41-4f7c-9867-64e9c0bab39f.htm>, and I...
5
by: junky_fellow | last post by:
Hi, I have a very basic doubt about associativity and precedence of operators. I am not a computer science person and may find it quite weird. Consider an expression 4+5*2
4
by: pauldepstein | last post by:
Take the following code snippet: int x = 5; int i = 7; It's obvious to me what (x+=i )= 25; does and it's obvious to me what x +=(i = 25); does. However, (other than by trying it out), I'd...
7
by: arnuld | last post by:
/* C++ Primer - 4/e * chapter 5 - Expressions * exercises 5.1 and 5.2 */ #include <iostream>
3
by: feeblez | last post by:
Consider the following, #include <iostream> using namespace std; class Foo { public: int i; Foo() : i(0) { cout << "ctor" << endl; }
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.