Topic: Is 'i ? j : k = 5' legal? (grammar question)


Author: mwoolf@cix.compulink.co.uk ("Matthew Woolf")
Date: Sun, 4 Dec 1994 01:28:03 GMT
Raw View
Surely (i?j:k)=5 is legal becuase both i and j are reference types
(L-values), so the result of the expression is a reference type. It
occurs to me the the really question here is what is the relationship
between the return type and conditional expression types following the ?.

The MSVC compiler accepts (i?j:k)=5 as above with or without the
parenthesis.

Any comments?




Author: sdouglass@armltd.co.uk (scott douglass)
Date: 5 Dec 1994 15:58:45 GMT
Raw View
In article <D09Iqs.Ipy@cix.compulink.co.uk>, mwoolf@cix.compulink.co.uk
("Matthew Woolf") wrote:

> Surely (i?j:k)=5 is legal ...
>
> The MSVC compiler accepts (i?j:k)=5 as above with or without the
> parenthesis.
>
> Any comments?

Yes, I definitely believe that '(i?j:k)=5' is legal.

The question is:  Is 'i?j:k=5' the same as '(i?j:k)=5'?

I now believe (albeit the precedence tables and ARM examples on pg 78 seem
to disagree) that 'i?j:k=5' is legal and must be parsed in C and C++ as
'i?j:(k=5)'.  To parse it as '(i?j:k)=5' requires 'i?j:k' to be a
<unary-expression>, which it is not.

['(i?j:k)' is a <unary-expression> so that's ok.]

Any C or C++ compiler that compiles 'i?j:k=5' as '(i?j:k)=5' is broken.
At least until they change the std.
[There, maybe that'll provoke more responce.]
       --scott




Author: jason@cygnus.com (Jason Merrill)
Date: Tue, 6 Dec 1994 04:25:46 GMT
Raw View
>>>>> scott douglass <sdouglass@armltd.co.uk> writes:

> I now believe (albeit the precedence tables and ARM examples on pg 78 seem
> to disagree) that 'i?j:k=5' is legal and must be parsed in C and C++ as
> 'i?j:(k=5)'.

Yup.  The grammar in the current WP contains the rule

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

Jason




Author: pkt@lpi.liant.com (Scott Turner)
Date: Mon, 28 Nov 1994 17:41:39 GMT
Raw View
In article <scott-2411941638300001@193.131.176.202>,
scott@newton.apple.com (scott douglass) wrote:

> On page 78 of the ARM (section 5.16), Ellis & Stroustrup give examples of
> the form
>    i ? j : k = 5
> meaning
>    (i ? j : k) = 5
>
> Looking at the grammar I can't find a legal parse for this.

Originally,
   i ? j : k = 5
was not valid, but a couple of years ago the standards committee
adopted a change to the rule for conditional-expression.  It was
changed from
             conditional-expression:
                logical-or-expression ? expression : conditional-expression
to
             conditional-expression:
                logical-or-expression ? expression : assignment-expression
So now it has the same meaning as
   i ? j : (k = 5)
--
Scott Turner
Liant Software Corp., Framingham, Massachusetts, USA
pkt@lpi.liant.com




Author: scott@newton.apple.com (scott douglass)
Date: 24 Nov 1994 16:39:10 GMT
Raw View
On page 78 of the ARM (section 5.16), Ellis & Stroustrup give examples of
the form
   i ? j : k = 5
meaning
   (i ? j : k) = 5

Looking at the grammar I can't find a legal parse for this.

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

The second production is the relavent one, but 'i ? j : k' is not a legal
unary-expression.

I hypothsesize that this part of the grammar came directly from ANSI/ISO
C.  Futher, in C unary-expression is the most general expression that can
result in an lvalue so this isn't a problem in C.

But in C++, conditional-expression is the most general expression that can
result in an lvalue.  Changing the production to 'conditional-expression
assignment-operator assignment-expression' looks like asking for parsing
problems, but it would allow 'i ? j : k = 5'.

Or maybe compilers should not accept 'i ? j : k = 5' and I should just get
used to typing '(i ? j : k) = 5'.
       --scott