Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: In how many ways should this fail? |
| Date | 2012-01-30 16:17 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <4F2708F5.6050402@verizon.net> (permalink) |
| References | (2 earlier) <jg6hcq$3mc$1@dont-email.me> <4F26D3FE.4010606@verizon.net> <jg6mrf$6it$1@dont-email.me> <4F26E9F6.3020101@verizon.net> <jg6r4h$1fh$1@dont-email.me> |
On 01/30/2012 02:30 PM, BartC wrote: > "James Kuyper" <jameskuyper@verizon.net> wrote in message > news:4F26E9F6.3020101@verizon.net... >> On 01/30/2012 01:16 PM, BartC wrote: > >>> Only if the precedence of ?: is lower than for assignment. Used the other >>> way: >> >> The ?: operator can't be explained in terms of a simple precedence >> relative to other operators. >> >>> x = (a>b) ? a : b; >>> >>> You would expect the conditional expression to be evaluated first. Why >>> not >>> the same with the conditional on the left? >> >> Because I when gwowen wrote his comment, it sounded familiar, like >> something I already knew to be true, so I didn't bother to check. I just >> checked, and he was wrong, and I was therefore wrong to agree with his >> parse. The C grammar is: >> >> logical-OR-expression ? expression : conditional-expression > > That doesn't make sense at all. ... It makes sense to me. More importantly, it's precisely what the C standard specifies. > ... The conditional-operator syntax is more > like: > > expression1 ? expression2 : expression3 Not according to the C standard. > Parentheses are not usually needed because "the precedence of ?: is very > low, just above assignment" (from K&R). Why does the C grammar call > expression3 a conditional expression? Why is expression1 a > logical-OR-expression, when it can be anything that yields 0 or not 0? expression1 and expression3 can't both be allowed to be conditional expressions - that would make the parse of a ? b : c ? d : e ambiguous. The standard made the choice that corresponds to parsing as a ? b : (c ? d : e) rather than (a ? b : c) ? d : e. I find this choice convenient for writing a chain of ?: operators, which I presume is the reason why they made that choice. The C99 rationale provides no insight into that decision. It also provides no insight about why they decided to disallow an assignment expression as the third operand. As C++ has shown, there's some use-cases for which that would be convenient. > Anyway, being just above assignment, your would expect a?b:c=d to evaluate > a?b:c just before being assigned d. No, I wouldn't, because I know that in C, it's a constraint violation, and in C++, it gets parsed differently. >> In the following lines, I've inserted spaces to make corresponding parts >> line up with each other, but the alignment will not come out correctly >> unless viewed using a monospaced font: >> >> According to that grammar, >> a || b ? d , e : f ? g : h >> must be parsed as >> (a || b) ? (d , e) : (f ? g : h) >> but >> a ? b : c ? d , e : f = g >> must be parsed as >> (a ? b : (c ? (d , e) : f)) = g >> (that last parse results in a constraint violation, but it's not a >> syntax error). > > Sorry, I don't get the above at all; what has a||b got to do with anything, > and why are there two lots of ?: on each line? The syntax shown on p. 51 of > K&R2 is pretty much what I wrote above. A logical OR expression is the type of expression with the highest "precedence" that can be the left operand of an ?: without using parentheses; a conditional expression is the one with the lowest "precedence" that requires parentheses to be used as the left operand of another conditional expression. A conditional expression is the type of expression with the highest "precedence" that can be used without parentheses as the right operand of a conditional expression. An assignment expression is the type of expression that has the lowest precedence that requires use of parentheses to be used as the right operand of a conditional operator. Any expression can be used without parenthesis as the middle operand of a conditional expression. A comma expression is the type that has the lowest precedence. I put my examples together using ||, two copies of ?:, =, and a comma operator, in order to demonstrate all of those cases. >> In all other contexts, the C grammar can be understood as giving || a >> higher precedence than =, giving both of them higher precedence than the >> comma operator. However, there's no way to insert ?: into that >> precedence hierarchy that explains both of the above parses. >> >> The C++ grammar is different, as I said, but I had the difference >> backwards: >> logical-or-expression ? expression : assignment-expression > > I don't know what the C Standard document is on about; looking to the next > section, 6.5.16 Assignment, the conditional-expression occurs in the the > syntax for that too! That's as it should be. The key thing that is peculiar about the conditional operator is that the middle operand is completely unambiguous: it must be bracketed by ? and :. As a result, any expression is allowed for the middle operand. In all other respects, the conditional operator has higher precedence than the assignment operator. > Actually I still don't know whether you agree with my interpretation of > a?b:c=d (as (a?b:c)=d rather than a?b:(c=d)) or not.. No, I do not. The C standard specifies a syntax that mandates parsing it as (a?b:c)=d, and specifies a constraint that is necessarily violated by that parse. If an implementation should choose to accept the program after issuing the mandatory diagnostic, the behavior of any such program is undefined. The C++ standard specifies a syntax that mandates parsing it as a ? b : (c=d). The expression (a?b:c)=d is not a constraint violation in C++, but the parenthesis are not optional.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
In how many ways should this fail? Anders Wegge Keller <wegge@wegge.dk> - 2012-01-30 16:32 +0100
Re: In how many ways should this fail? James Kuyper <jameskuyper@verizon.net> - 2012-01-30 11:07 -0500
Re: In how many ways should this fail? James Kuyper <jameskuyper@verizon.net> - 2012-01-30 11:11 -0500
Re: In how many ways should this fail? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-01-30 17:42 +0000
Re: In how many ways should this fail? gwowen <gwowen@gmail.com> - 2012-01-30 08:16 -0800
Re: In how many ways should this fail? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-01-30 17:35 +0000
Re: In how many ways should this fail? gwowen <gwowen@gmail.com> - 2012-01-30 23:47 -0800
Re: In how many ways should this fail? Tim Rentsch <txr@alumni.caltech.edu> - 2012-02-01 00:00 -0800
Re: In how many ways should this fail? "BartC" <bc@freeuk.com> - 2012-02-01 12:14 +0000
Re: In how many ways should this fail? James Kuyper <jameskuyper@verizon.net> - 2012-02-01 08:04 -0500
Re: In how many ways should this fail? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-02-01 13:31 +0000
Re: In how many ways should this fail? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-02-01 12:59 +0000
Re: In how many ways should this fail? James Kuyper <jameskuyper@verizon.net> - 2012-02-01 08:37 -0500
Re: In how many ways should this fail? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-02-01 16:40 +0000
Re: In how many ways should this fail? James Kuyper <jameskuyper@verizon.net> - 2012-02-01 12:13 -0500
Re: In how many ways should this fail? Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-08 12:49 -0800
Re: In how many ways should this fail? Anders Wegge Keller <wegge@wegge.dk> - 2012-01-30 19:20 +0100
Re: In how many ways should this fail? Devil with the China Blue Dress <chine.bleu@yahoo.com> - 2012-01-30 08:30 -0800
Re: In how many ways should this fail? "BartC" <bc@freeuk.com> - 2012-01-30 16:43 +0000
Re: In how many ways should this fail? Devil with the China Blue Dress <chine.bleu@yahoo.com> - 2012-01-30 09:17 -0800
Re: In how many ways should this fail? Kaz Kylheku <kaz@kylheku.com> - 2012-01-30 17:31 +0000
Re: In how many ways should this fail? James Kuyper <jameskuyper@verizon.net> - 2012-01-30 12:31 -0500
Re: In how many ways should this fail? "BartC" <bc@freeuk.com> - 2012-01-30 18:16 +0000
Re: In how many ways should this fail? James Kuyper <jameskuyper@verizon.net> - 2012-01-30 14:05 -0500
Re: In how many ways should this fail? "BartC" <bc@freeuk.com> - 2012-01-30 19:30 +0000
Re: In how many ways should this fail? Keith Thompson <kst-u@mib.org> - 2012-01-30 12:50 -0800
Re: In how many ways should this fail? James Kuyper <jameskuyper@verizon.net> - 2012-01-30 16:17 -0500
Re: In how many ways should this fail? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-01-30 21:48 +0000
Re: In how many ways should this fail? "BartC" <bc@freeuk.com> - 2012-01-31 12:43 +0000
Re: In how many ways should this fail? Zoltan Kocsi <zoltan@bendor.com.au> - 2012-02-01 00:46 +1100
Re: In how many ways should this fail? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-01-31 13:57 +0000
Re: In how many ways should this fail? "BartC" <bc@freeuk.com> - 2012-01-31 15:05 +0000
Re: In how many ways should this fail? Devil with the China Blue Dress <chine.bleu@yahoo.com> - 2012-01-31 07:16 -0800
Re: In how many ways should this fail? "BartC" <bc@freeuk.com> - 2012-01-31 17:37 +0000
Re: In how many ways should this fail? James Kuyper <jameskuyper@verizon.net> - 2012-01-31 11:34 -0500
Re: In how many ways should this fail? "BartC" <bc@freeuk.com> - 2012-01-31 17:39 +0000
Re: In how many ways should this fail? Keith Thompson <kst-u@mib.org> - 2012-01-31 19:42 -0800
Re: In how many ways should this fail? "BartC" <bc@freeuk.com> - 2012-02-01 11:57 +0000
Re: In how many ways should this fail? James Kuyper <jameskuyper@verizon.net> - 2012-02-01 08:14 -0500
Re: In how many ways should this fail? Philip Lantz <prl@canterey.us> - 2012-02-01 00:04 -0800
Re: In how many ways should this fail? James Kuyper <jameskuyper@verizon.net> - 2012-01-31 09:05 -0500
Re: In how many ways should this fail? Devil with the China Blue Dress <chine.bleu@yahoo.com> - 2012-01-31 06:45 -0800
Re: In how many ways should this fail? "BartC" <bc@freeuk.com> - 2012-01-31 17:34 +0000
Re: In how many ways should this fail? Keith Thompson <kst-u@mib.org> - 2012-01-31 19:26 -0800
Re: In how many ways should this fail? Keith Thompson <kst-u@mib.org> - 2012-01-31 19:29 -0800
Re: In how many ways should this fail? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-01-30 21:28 +0000
Re: In how many ways should this fail? timprince <tprince@computer.org> - 2012-01-30 11:44 -0500
Re: In how many ways should this fail? Kaz Kylheku <kaz@kylheku.com> - 2012-01-30 17:35 +0000
Re: In how many ways should this fail? Kaz Kylheku <kaz@kylheku.com> - 2012-01-30 17:21 +0000
Re: In how many ways should this fail? Anders Wegge Keller <wegge@wegge.dk> - 2012-01-30 19:22 +0100
Re: In how many ways should this fail? 88888 Dihedral <dihedral88888@googlemail.com> - 2012-01-30 11:09 -0800
Re: In how many ways should this fail? jacob navia <jacob@spamsink.net> - 2012-01-30 19:04 +0100
Re: In how many ways should this fail? "BartC" <bc@freeuk.com> - 2012-01-30 18:21 +0000
Re: In how many ways should this fail? Jens Gustedt <jens.gustedt@loria.fr> - 2012-01-30 20:15 +0100
Re: In how many ways should this fail? "BartC" <bc@freeuk.com> - 2012-01-30 19:32 +0000
Re: In how many ways should this fail? Jens Gustedt <jens.gustedt@loria.fr> - 2012-01-30 21:01 +0100
Re: In how many ways should this fail? "BartC" <bc@freeuk.com> - 2012-01-30 20:22 +0000
Re: In how many ways should this fail? Keith Thompson <kst-u@mib.org> - 2012-01-30 12:56 -0800
Re: In how many ways should this fail? blp@cs.stanford.edu (Ben Pfaff) - 2012-01-30 10:07 -0800
csiph-web