Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re^3: Surprised by Perl parse of ternary operator (parsing)

by tye (Sage)
on Dec 16, 2011 at 14:53 UTC ( #943955=note: print w/ replies, xml ) Need Help??

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

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        

Comment on Re^3: Surprised by Perl parse of ternary operator (parsing)
Select or Download Code
Replies are listed 'Best First'.
Re^4: Surprised by Perl parse of ternary operator (parsing)
by wrog (Friar) on Jan 16, 2012 at 00:27 UTC
    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://943955]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (9)
As of 2015-11-27 18:30 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (731 votes), past polls