Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re^2: Surprised by Perl parse of ternary operator

by eyepopslikeamosquito (Chancellor)
on Dec 16, 2011 at 12:52 UTC ( #943938=note: print w/replies, xml ) Need Help??

in reply to Re: Surprised by Perl parse of ternary operator
in thread Surprised by Perl parse of ternary operator

Curiously, both Ruby and PHP, both with the same relative precedence as Perl for the ternary and assignment operators, bend the rules differently. For example, this Ruby program:

config = '' fred = 42 fred == 42 ? config = 'k1' : config = 'k2' print "config=#{config}\n"
and PHP program:
<?php $config = ''; $fred = 42; $fred == 42 ? $config = 'k1' : $config = 'k2'; echo "config=$config\n"; ?>
both run without error, but, unlike Perl (which produces config=k2), both Ruby and PHP produce config=k1.

Arguably, all three languages should produce a syntax error.

Update: gcc produces a syntax error "f.c:6: error: lvalue required as left operand of assignment" with this test program (further update: g++ compiles it happily though and with the same semantics as the Ruby and PHP test programs):

#include <stdio.h> int main() { int config = 0; int fred = 42; fred == 42 ? config = 1 : config = 2; printf("config=%d\n", config); return 0; }
Adding parentheses to line 6:
fred == 42 ? (config = 1) : (config = 2); // this works fred == 42 ? config = 1 : (config = 2); // also works fred == 42 ? (config = 1) : config = 2; // syntax error
makes it work the same as the Ruby and PHP test programs.

Replies are listed 'Best First'.
Re^3: Surprised by Perl parse of ternary operator
by BrowserUk (Pope) on Dec 16, 2011 at 13:18 UTC
    bend the rules differently.

    I wouldn't exactly call it "bending the rules". For perl or the other two.

    I read it that perl uses a minimal munch approach at that point of the syntax and hence sees:

    ( ( $fred == 42) ? ( $config = 'k1') : ( $config ) ) = 'k2';

    Whereas the other two use a maximal munch approach and so see:

    ( $fred == 42 ) ? ( $config = 'k1' ) : ( $config = 'k2' );

    I think the use case for Perl's way is using the ternary as the lvalue target of assignment. Something like:

    $lo = $hi = 0; $x = 7; $mid = 5; $x < $mid ? $lo : $hi = $x; print "lo:$lo hi:$hi";; lo:0 hi:7

    Can Ruby or PHP do that?

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      Can Ruby or PHP do that?

      No, at least Ruby seems to lack a concept of a "variable" lvalue. (foo = bar) = 5 gives a syntax error, and x < mid ? lo : hi = x gets parsed as x < mid ? lo : (hi = x)
Re^3: Surprised by Perl parse of ternary operator (parsing)
by tye (Sage) on Dec 16, 2011 at 14:53 UTC

    Are you unfamiliar with how an LALR(1) grammar is parsed? (No, I don't know if Perl strictly constitutes an LALR(1) grammar, but it does use yacc / bison and so at least much of the parsing actually follows the LALR(1) rules.)

    Your conclusion would only be correct if both PHP and Ruby behaved the same for the equivalent of Perl's:

    ( $config = 'k1' ) = 'k2';

    which I couldn't even get PHP to compile (no Ruby handy).

    If you think Perl is bending some rules, then please point out the rule-bending code that it gives to yacc / bison. The fact is that declared precedence only comes into play when a "shift/reduce conflict" is found in generating the parser state machine.

    What I see is simply:

    %right <i_tkval> ASSIGNOP %right <i_tkval> '?' ':' ... termbinop: term ASSIGNOP term /* $x = $y */ ... | term '?' term ':' term

    Perl could make the grammar definition more complicated by not using yacc / bison declared precedence to implement the precedence of just the one ternary operator since it is the only case where there is a "middle argument" where declared precedence doesn't matter. But that would involve specifying yet another primitive for "expression" and having to pick the right one to use in each of the dozens of places where the current 'term' is used.

    I find it very Perlish that ?: precedence is enforced by declared precedence, not proscriptive grammar definition.

    (Updated: to add the relevant bits from perly.y)

    - tye        

      if I remember correctly, perl does not use LALR (yacc/bison) parsing but rather uses a Pratt-style precedence parser which is a kind of recursive descent but in which each operator is assigned a distinct left and right precedence...

        I quoted the relevant bits of perly.y and that file does indeed get processed by bison (see Makefile.SH and "git grep -i pratt" finds 0 hits. Perhaps a "Pratt-style precedence parser" is used, but, if so, it seems quite weird that bison declarations of precedence are still included in the source code.

        So, to me, it looks pretty likely that Perl's parsing of ?: is mostly how bison-style declared precedence works.

        - tye        

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://943938]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2017-05-25 04:07 GMT
Find Nodes?
    Voting Booth?