RE: [PHP-DEV] Possible problem in the parser

[email protected] (Andi Gutmans) Fri, 14 Mar 2003 16:50:07 +0200
Newsgroups php.dev
Message-ID <[email protected]>
You are right that it doesn't behave the same as C. However, personally 
although it might have been better for it to work like C I don't think it's 
a good idea to change it now. First of all it would break backwards 
compatibility in a way which would be hard for people to find where the bug 
is and how to fix it. Secondly, not meaning to insult anyone here, but I 
think people who write such code without using parentheses should improve 
their coding style :)

Andi


At 12:01 PM 3/14/2003 +0000, Ford, Mike               [LSS] wrote:
> > -----Original Message-----
> > From: [email protected] [mailto:[email protected]]
> > Sent: 13 March 2003 19:33
> > To: Ford, Mike [LSS]
> > Cc: 'Andrey Hristov'; [email protected]
> > Subject: RE: [PHP-DEV] Possible problem in the parser
> >
> >
> > At 14:58 13.03.2003, Ford, Mike               [LSS] wrote:
> >
> > >Just to make this completely clear, in left-associative PHP
> > >
> > >    b = a==1? 4:a==2? 5:6;
> > >
> > >is equivalent to
> > >
> > >    b = (a==1? 4:a==2)? 5:6;
> >
> >
> > NO it is not equal. Either '==' has higher precedence OR '?:' has.
> > See one of my previous mails where i showed where the error is.
>
>Yes, it is -- believe me, I've researched this extensively.  It is NOT 
>about precedence, but associativity.  If you want me to be totally 
>completist about this:
>
>Starting from:
>
>    b = a==1? 4:a==2? 5:6;
>
>precedence rules make this equivalent to:
>
>    b = (a==1)? 4:(a==2)? 5:6;
>
>but this is still ambiguous -- which ?: phrase do you evaluate 
>first?  Associativity provides the answer: in PHP, where ?: is left 
>associative (i.e. the left most ?: is evaluated first), the result is 
>equivalent to:
>
>    b = ((a==1)? 4:(a==2))? 5:6;
>
>On the other hand, in c, where ?: is right associative, the equivalent is:
>
>    b = (a==1)? 4:((a==2)? 5:6);
>
>which, apart from the additional (unnecessary) parentheses around the == 
>comparisons, is exactly what I said before.
>
>QED
>
>Cheers!
>
>Mike
>
>---------------------------------------------------------------------
>Mike Ford,  Electronic Information Services Adviser,
>Learning Support Services, Learning & Information Services,
>JG125, James Graham Building, Leeds Metropolitan University,
>Beckett Park, LEEDS,  LS6 3QS,  United Kingdom
>Email: [email protected]
>Tel: +44 113 283 2600 extn 4730      Fax:  +44 113 283 3211
>
>--
>PHP Development Mailing List <http://www.php.net/>
>To unsubscribe, visit: http://www.php.net/unsub.php