C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

What does "right associative" mean for the ternary operator?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language (Moderated)
View previous topic :: View next topic  
Author Message
Anton Treuenfels
Guest





PostPosted: Mon May 07, 2007 9:12 am    Post subject: What does "right associative" mean for the ternary operator? Reply with quote



To me "left associative" means that if two operators of the same precedence
occur in succession, the left one is evaluated first, and "right
associative" means the opposite.

However an expression like this:

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

in my understanding is parsed like:

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

so that if (a) is non-zero, the result is (b), and (c), (d) and (e) are
never evaluated. OTOH if "?:" really is right-associative, why isn't the
second "?:" evaluated first, ie. (c) then (d) or (e), the result possibly
being discarded if (a) is non-zero? Not of any particular use, but seemingly
more consistent with "right associative".

Is it simply that "left associative" would probably mean:

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

so that (a), then (b) or (c), and then (d) or (e) would all be evaluated,
and that, not being of much practical use either, leaves "right associative"
as the only reasonable terminology alternative?

I ask because I'm playing around with adding the ternary conditional to an
expression parser I'm toying with. In my own actual programming I've never
gone beyond

(a) ? (b) : (c)

but I figure a parser also ought to be able to handle

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

as

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

and

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

as

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

and

(a) ? (b) ? (c) : (d) : (g) ? (h) : (i)

as

(a) ? ( (b) ? (c) : (d) ) : ( (g) ? (h) : (i) )

and I've pretty much gotten them to parse that way. Now I'm wondering if my
interpretation is correct (at least w/r/t C - if it turns out to be much
different/harder I'll probably just stick with what I've got!).

- Anton Treuenfels
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Francis Glassborow
Guest





PostPosted: Tue May 08, 2007 7:08 am    Post subject: Re: What does "right associative" mean for the ternary opera Reply with quote



Anton Treuenfels wrote:
Quote:
To me "left associative" means that if two operators of the same precedence
occur in succession, the left one is evaluated first, and "right
associative" means the opposite.

However an expression like this:

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

in my understanding is parsed like:

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

so that if (a) is non-zero, the result is (b), and (c), (d) and (e) are
never evaluated. OTOH if "?:" really is right-associative, why isn't the
second "?:" evaluated first, ie. (c) then (d) or (e), the result possibly
being discarded if (a) is non-zero? Not of any particular use, but seemingly
more consistent with "right associative".

Is it simply that "left associative" would probably mean:

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

so that (a), then (b) or (c), and then (d) or (e) would all be evaluated,
and that, not being of much practical use either, leaves "right associative"
as the only reasonable terminology alternative?

Associativity applies between sequence points. Because ?: contains an

internal
sequence point your questions are moot.

As I am in the process of preparing to move house all my reference books
are packed away so I cannot easily elaborate but I suspect that the
significance of associativity in the context of ?: is for mixing it with
any other operators of the same precedence.

Another way of looking at this is that your second ?: in your example is
_part of_ the first one and not a free standing sub-expression.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Hans-Bernhard Bröker
Guest





PostPosted: Tue May 08, 2007 7:08 am    Post subject: Re: What does "right associative" mean for the ternary opera Reply with quote



Anton Treuenfels wrote:
Quote:
To me "left associative" means that if two operators of the same precedence
occur in succession, the left one is evaluated first, and "right
associative" means the opposite.

Well, sorry, but that's not what assiociativity really means. It's not
about order of evaluation, but about parsing hierarchy, i.e.
effectively: where the parser will place its implied parentheses that
break a sequence of operations into individual ones behind the scenes.

Quote:
However an expression like this:

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

in my understanding is parsed like:

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

| |
Exactly. That extra pair of parantheses is given to you by the
operator's associativity.

Quote:
so that if (a) is non-zero, the result is (b), and (c), (d) and (e) are
never evaluated. OTOH if "?:" really is right-associative, why isn't the
second "?:" evaluated first,

Because associativity is about parsing, not about semantics (i.e.
sequence of evaluation).
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Barry Schwarz
Guest





PostPosted: Tue May 08, 2007 7:08 am    Post subject: Re: What does "right associative" mean for the ternary opera Reply with quote

On 07 May 2007 06:41:06 GMT, "Anton Treuenfels"
<atreuenfels (AT) earthlink (DOT) net> wrote:

Quote:
To me "left associative" means that if two operators of the same precedence
occur in succession, the left one is evaluated first, and "right
associative" means the opposite.

No, associativity does not define order of evaluation. For example, +
has a particular associativity. But in the statement
i = j + k;
you have no idea if j or k is evaluated first.

However, associativity does tell us how an expression is parsed. In a
statement like
a = b += c;
we know that it will be parsed as
a = (b += c);
and not as
(a = b) += c;

Quote:

However an expression like this:

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

in my understanding is parsed like:

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

so that if (a) is non-zero, the result is (b), and (c), (d) and (e) are
never evaluated. OTOH if "?:" really is right-associative, why isn't the
second "?:" evaluated first, ie. (c) then (d) or (e), the result possibly
being discarded if (a) is non-zero? Not of any particular use, but seemingly
more consistent with "right associative".

Associativity does not control evaluation.

Quote:

Is it simply that "left associative" would probably mean:

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

so that (a), then (b) or (c), and then (d) or (e) would all be evaluated,
and that, not being of much practical use either, leaves "right associative"
as the only reasonable terminology alternative?

Associativity does not control evaluation.

snip


Remove del for email
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Richard Tobin
Guest





PostPosted: Wed May 09, 2007 10:37 pm    Post subject: Re: What does "right associative" mean for the ternary opera Reply with quote

In article <clcm-20070507-0010 (AT) plethora (DOT) net>,
Francis Glassborow <francisG (AT) robinton (DOT) demon.co.uk> wrote:

Quote:
Associativity applies between sequence points.

I find this comment strange. Associativity is a property of the
syntax, used when parsing. Sequence points are runtime events. There
is no reason why associativity cannot be used to describe the parsing
of an expression that contains any number of sequence points.

Do you mean that it is only between sequence points that associativity
(and hence tree structure of the parsed program) correspond to
evaluation order?

It seems that associativity and precedence are confusing notions
except in the case of binary operators. The production notation used
in the standard has the advantage of being quite unambiguous.

-- Richard
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Francis Glassborow
Guest





PostPosted: Wed May 09, 2007 10:37 pm    Post subject: Re: What does "right associative" mean for the ternary opera Reply with quote

Hans-Bernhard Bröker wrote:
Quote:
Because associativity is about parsing, not about semantics (i.e.
sequence of evaluation).

Well, yes and no :)

Note that those extra parentheses actually inject and order of
evaluation. If they did not we would be in deep trouble:

a = 20 * 3 / 6;
or even better:

b = 20 / 2 / 2;

Like it or not, associativity imposes a partial ordering on the
evaluations of operators.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Kenneth Brody
Guest





PostPosted: Wed May 09, 2007 10:38 pm    Post subject: Re: What does "right associative" mean for the ternary opera Reply with quote

Barry Schwarz wrote:
Quote:

On 07 May 2007 06:41:06 GMT, "Anton Treuenfels"
[...]
However an expression like this:

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

in my understanding is parsed like:

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

so that if (a) is non-zero, the result is (b), and (c), (d) and (e) are
never evaluated. OTOH if "?:" really is right-associative, why isn't the
second "?:" evaluated first, ie. (c) then (d) or (e), the result possibly
being discarded if (a) is non-zero? Not of any particular use, but seemingly
more consistent with "right associative".

Associativity does not control evaluation.
[...]


And, in this particular case, the rules of ?: guarantee that the
right-side is _not_ evaluated if the condition is true. Therefore,
in the expression

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

the "( (c) ? (d) : (e) )" _cannot_ be evaluated first and "the result
possibly discarded if (a) is non-zero".

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:ThisIsASpamTrap (AT) gmail (DOT) com>
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Kenneth Brody
Guest





PostPosted: Thu May 10, 2007 9:11 am    Post subject: Re: What does "right associative" mean for the ternary opera Reply with quote

Francis Glassborow wrote:
Quote:

Hans-Bernhard Bröker wrote:
Because associativity is about parsing, not about semantics (i.e.
sequence of evaluation).

Well, yes and no :)

Note that those extra parentheses actually inject and order of
evaluation. If they did not we would be in deep trouble:

a = 20 * 3 / 6;
or even better:

b = 20 / 2 / 2;

Like it or not, associativity imposes a partial ordering on the
evaluations of operators.

Not really. Consider the following:

#include <stdio.h>

int twenty(void) { printf("twenty\n"); return 20; }
int two_1(void) { printf("two_1\n"); return 2; }
int two_2(void) { printf("two_2\n"); return 2; }

int main(void)
{
int b = twenty() / two_1() / two_2();
printf("%d\n",b);
exit(0);
}

What is the output?

Yes, the first division must take place before the second division.
However, the order of evaluation of the parameters to the divisions
is unspecified.

Now, is a compiler is allowed to treat:

a / b / c

as if it were

a / ( b * c )

because the results are mathematically the same? (Though in integer
arithmetic, they may be different, so maybe it's not allowed.)


--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:ThisIsASpamTrap (AT) gmail (DOT) com>
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Keith Thompson
Guest





PostPosted: Thu May 10, 2007 9:11 am    Post subject: Re: What does "right associative" mean for the ternary opera Reply with quote

Francis Glassborow <francisG (AT) robinton (DOT) demon.co.uk> writes:
Quote:
Hans-Bernhard Bröker wrote:
Because associativity is about parsing, not about semantics
(i.e. sequence of evaluation).

Well, yes and no :)

Note that those extra parentheses actually inject and order of
evaluation. If they did not we would be in deep trouble:

a = 20 * 3 / 6;
or even better:

b = 20 / 2 / 2;

Like it or not, associativity imposes a partial ordering on the
evaluations of operators.

But given either

a = x * y / z;

or

b = x / y / z;

with or without added parentheses, the operands x, y, and z can be
evaluated in any of the 6 possible orders (which can be significant if
any of the operands have side effects). Associativity affects the
meaning of the expression, and requires some operators to be evaluated
before others, but doesn't control the order of evaluation of the leaf
operands.

--
Keith Thompson (The_Other_Keith) kst-u (AT) mib (DOT) org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Hans-Bernhard Bröker
Guest





PostPosted: Thu May 10, 2007 9:11 am    Post subject: Re: What does "right associative" mean for the ternary opera Reply with quote

Richard Tobin wrote:

Quote:
I find this comment strange. Associativity is a property of the
syntax, used when parsing.

It's a property of *some* languages' syntax, but not of C's, because the
C grammar is expressed without referring to any associativity (nor
operator precedence, for that matter).

Quote:
Sequence points are runtime events.

No. Unless volatiles or short-circuiting operators like &&, || or ??
are involved, there's no rule that sequence points even have to be
identifiable in generated code, much less correspond to a definable
event at runtime.

Quote:
There is no reason why associativity cannot be used to describe the
parsing of an expression that contains any number of sequence points.

It can be used, but to no particularly good effect. It fails to address
precedence, sequence points, and guaranteed non-evaluation of operands.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Francis Glassborow
Guest





PostPosted: Tue May 15, 2007 9:06 pm    Post subject: Re: What does "right associative" mean for the ternary opera Reply with quote

Kenneth Brody wrote:
Quote:
Francis Glassborow wrote:
Hans-Bernhard Bröker wrote:
Because associativity is about parsing, not about semantics (i.e.
sequence of evaluation).
Well, yes and no :)

Note that those extra parentheses actually inject and order of
evaluation. If they did not we would be in deep trouble:

a = 20 * 3 / 6;
or even better:

b = 20 / 2 / 2;

Like it or not, associativity imposes a partial ordering on the
evaluations of operators.

Not really. Consider the following:

#include <stdio.h

int twenty(void) { printf("twenty\n"); return 20; }
int two_1(void) { printf("two_1\n"); return 2; }
int two_2(void) { printf("two_2\n"); return 2; }

int main(void)
{
int b = twenty() / two_1() / two_2();
printf("%d\n",b);
exit(0);
}

What is the output?

Yes, the first division must take place before the second division.
However, the order of evaluation of the parameters to the divisions
is unspecified.

Now, is a compiler is allowed to treat:

a / b / c

as if it were

a / ( b * c )

because the results are mathematically the same? (Though in integer
arithmetic, they may be different, so maybe it's not allowed.)


Did you note that I wrote 'partial ordering'? And referred to operators

not sub-expressions?
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Hans-Bernhard Bröker
Guest





PostPosted: Tue May 15, 2007 9:06 pm    Post subject: Re: What does "right associative" mean for the ternary opera Reply with quote

Keith Thompson wrote:

Quote:
But given either

a = x * y / z;

or

b = x / y / z;

with or without added parentheses, the operands x, y, and z can be
evaluated in any of the 6 possible orders (which can be significant if
any of the operands have side effects).

If we want to be really nitpicky, the number of possible orders is
actually a lot larger than 6. It's generally boundless. Evaluating one
of the operands could involve a million atomic steps on the host
platform, and there's nothing to stop the translator to interleave these
sequences at random. It could even do all three evaluations
simultaneously, if it has enough CPU cores to do that.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Clark Cox
Guest





PostPosted: Thu May 17, 2007 9:11 am    Post subject: Re: What does "right associative" mean for the ternary opera Reply with quote

On 2007-05-09 22:20:56 -0700, Kenneth Brody <kenbrody (AT) spamcop (DOT) net> said:

Quote:
Francis Glassborow wrote:

Hans-Bernhard Bröker wrote:
Because associativity is about parsing, not about semantics (i.e.
sequence of evaluation).

Well, yes and no :)

Note that those extra parentheses actually inject and order of
evaluation. If they did not we would be in deep trouble:

a = 20 * 3 / 6;
or even better:

b = 20 / 2 / 2;

Like it or not, associativity imposes a partial ordering on the
evaluations of operators.

Not really. Consider the following:

#include <stdio.h

int twenty(void) { printf("twenty\n"); return 20; }
int two_1(void) { printf("two_1\n"); return 2; }
int two_2(void) { printf("two_2\n"); return 2; }

int main(void)
{
int b = twenty() / two_1() / two_2();
printf("%d\n",b);
exit(0);
}

What is the output?

Any one of the following:
"twenty\ntwo_1\ntwo_2\n5\n"
"twenty\ntwo_2\ntwo_1\n5\n"
"two_1\ntwenty\ntwo_2\n5\n"
"two_1\ntwo_2\ntwenty\n5\n"
"two_2\ntwenty\ntwo_1\n5\n"
"two_2\ntwo_1\ntwenty\n5\n"

Quote:
Yes, the first division must take place before the second division.
However, the order of evaluation of the parameters to the divisions
is unspecified.

Now, is a compiler is allowed to treat:

a / b / c

as if it were

a / ( b * c )

because the results are mathematically the same?

The compiler is allowed to do whatever it wants as long as:
1) It arrives at the correct value.
2) All of the sub-expressions are evaluated before the next sequence point.

Quote:
(Though in integer arithmetic, they may be different, so maybe it's
not allowed.)

They can be different if floating point arithmetic as well.


--
Clark S. Cox III
clarkcox3 (AT) gmail (DOT) com
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Numeromancer
Guest





PostPosted: Fri May 18, 2007 9:11 am    Post subject: Re: What does "right associative" mean for the ternary opera Reply with quote

Francis Glassborow wrote:
Quote:
Hans-Bernhard Bröker wrote:
Because associativity is about parsing, not about semantics (i.e.
sequence of evaluation).

Well, yes and no :)

Note that those extra parentheses actually inject and order of
evaluation. If they did not we would be in deep trouble:

a = 20 * 3 / 6;
or even better:

b = 20 / 2 / 2;

Like it or not, associativity imposes a partial ordering on the
evaluations of operators.

This last statement cannot be wrong; nor can it be right. It does not
rise even to the dignity of error because it has no meaning.

Operators are not evaluated, expressions are; out of (e-) an operator
no value (-valu-) can one get (-ate). Associativity determines the
expressions, and there is a partial ordering on the evaluation of those
expressions: subexpressions must be evaluated before their
superexpressions. This ordering is essentially the nature of
expressions, and accidentally a property of the C language.

Tim S
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Douglas A. Gwyn
Guest





PostPosted: Sat May 19, 2007 9:11 am    Post subject: Re: What does "right associative" mean for the ternary opera Reply with quote

Numeromancer wrote:
Quote:
... subexpressions must be evaluated before their
superexpressions.

Not entirely. Consider (*a++ + *b++).
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.