RE: [PHP-DEV] Possible problem in the parser
| From: | Ford, Mike [LSS] | Date: | Fri, 14 Mar 2003 12:01:15 +0000 |
| Subject: | RE: [PHP-DEV] Possible problem in the parser | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-95171@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: marcus.boerger@t-online.de [mailto:marcus.boerger@t-online.de]
> Sent: 13 March 2003 19:33
> To: Ford, Mike [LSS]
> Cc: 'Andrey Hristov'; php-dev@lists.php.net
> 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: m.ford@lmu.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211