Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > In how many ways should this fail?

Reply
Thread Tools

In how many ways should this fail?

 
 
Ben Bacarisse
Guest
Posts: n/a
 
      02-01-2012
"BartC" <(E-Mail Removed)> writes:

> "Tim Rentsch" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>> The program fragment (written as an expression statement)
>>
>> a > b ? a : b = 42;
>>
>> is a syntax error. It has no valid parse under the standard
>> C grammar, any more than
>>
>> a + b = c;

>
> But is it actually reported as a syntax error?


No, not by either gcc or clang. This is one reason I missed the fact
that it's a syntax error (the other being I did not read the grammar!).

But that's OK. Errors ("constraint violations") need to be reported,
but there's no requirement on exactly how this is done.

> I would suggest this would be a type error rather than syntax (for
> example you can't apply address-of & to the result of a+b).


That's how both clang and gcc decide to do it. They alter the grammar
and later report a missing lvalue on the left of the assignment. This
is a risky strategy, since you have to be sure that everything that
you've let though will get caught later.

A little experimenting suggests that both alter the grammar to be:

assignment-expression:
conditional-expression
conditional-expression assignment-operator assignment-expression

rather than

assignment-expression:
conditional-expression
unary-expression assignment-operator assignment-expression

The change permits, on the left of an assignment, expressions with
operators whose "precedence" is between that of the unary operators and
the conditional operator. I think this is safe because none of these
extra expression forms can be an lvalue, so all of the newly accepted
syntax will be reported later.

--
Ben.
 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      02-01-2012
On 02/01/2012 07:59 AM, Ben Bacarisse wrote:
> Tim Rentsch <(E-Mail Removed)> writes:

....
>> posts in this thread. I will make a different assertion.
>> The program fragment (written as an expression statement)
>>
>> a > b ? a : b = 42;
>>
>> is a syntax error.

>
> And you would be right to make that assertion! The grammar is indeed
> different between C and C++ but in C there is no valid parse for the
> above.


Yes there is: ((a>b) ? a : b) = 42. That parse is a constraint
violation, but not a syntax error.
--
James Kuyper
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      02-01-2012
James Kuyper <(E-Mail Removed)> writes:

> On 02/01/2012 07:59 AM, Ben Bacarisse wrote:
>> Tim Rentsch <(E-Mail Removed)> writes:

> ...
>>> posts in this thread. I will make a different assertion.
>>> The program fragment (written as an expression statement)
>>>
>>> a > b ? a : b = 42;
>>>
>>> is a syntax error.

>>
>> And you would be right to make that assertion! The grammar is indeed
>> different between C and C++ but in C there is no valid parse for the
>> above.

>
> Yes there is: ((a>b) ? a : b) = 42. That parse is a constraint
> violation, but not a syntax error.


assignment-expression:
conditional-expression
unary-expression assignment-operator assignment-expression

If 'a > b ? a : b = 42' is to be parsed as an assignment expression,
what precedes the '=' must be produced by unary-expression. There's no
sequence of productions from unary-expression that can produce
'a > b ? a : b'.

--
Ben.
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      02-01-2012
On 02/01/2012 11:40 AM, Ben Bacarisse wrote:
> James Kuyper <(E-Mail Removed)> writes:

....
>> Yes there is: ((a>b) ? a : b) = 42. That parse is a constraint
>> violation, but not a syntax error.

>
> assignment-expression:
> conditional-expression
> unary-expression assignment-operator assignment-expression


You're right. Sorry - I'm not sure how I reached that conclusion.

You know, there are people who've accused me of thinking I'm infallible.
I can't imagine how they reached that conclusion - I make mistakes like
this constantly.
--
James Kuyper
 
Reply With Quote
 
Tim Rentsch
Guest
Posts: n/a
 
      03-08-2012
James Kuyper <(E-Mail Removed)> writes:

> On 02/01/2012 11:40 AM, Ben Bacarisse wrote:
>> James Kuyper <(E-Mail Removed)> writes:

> ...
>>> Yes there is: ((a>b) ? a : b) = 42. That parse is a constraint
>>> violation, but not a syntax error.

>>
>> assignment-expression:
>> conditional-expression
>> unary-expression assignment-operator assignment-expression

>
> You're right. Sorry - I'm not sure how I reached that conclusion.
>
> You know, there are people who've accused me of thinking I'm infallible.
> I can't imagine how they reached that conclusion - I make mistakes like
> this constantly.


Could it be because, despite continuing to make them, there is a
tendendency to tune out contrary suggestions rather than further
discussion and re-examination?

(Yes, I'm aware of the irony in my comment.)
 
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: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How many ways we can call servlet from jsp? Garg Java 1 08-04-2006 05:59 AM
I need help in many ways (with Usenext) Nightcrawler2525 Computer Support 8 08-02-2006 11:19 PM
How many ways to watch Media Center recording on another TV? Lew DVD Video 0 07-03-2006 01:21 AM
how many ways to convert a integer to a string apple.davinci@gmail.com C Programming 12 03-10-2006 09:18 PM



Advertisments