Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: Perl Cannot Be Parsed: A Formal Proof

by ViciousFrank (Initiate)
on Jun 11, 2008 at 04:24 UTC ( #691380=note: print w/ replies, xml ) Need Help??

in reply to Perl Cannot Be Parsed: A Formal Proof

I know my post is a little late...

Maybe we have a disctinction in our definition of "static parsing", but I think that no language can actually be statically unparseable as long there is a finite number of parsing rules. Under this condition, there should be a finite number of possible derivations for a certain number of generated symbols. In Kennedy's example :

whatever / 25 ; # / ; die "this dies!";

Two interpretations are possible. There should not be any problem if the compiler find the two ways and produces a check branching to the two interpretations. Something like this:

if (is_nullary(whatever))
  whatever / 25;
  whatever (/ 25 ; # /);
  die "this dies!";
When we think about it, the Perl interpreter already does this. So technicaly, parsing Perl is not a undecidable problem. You only proved that there could not be a single way to parse Perl - and yes, it is a problem for text editor's syntax-highlighting.

Comment on Re: Perl Cannot Be Parsed: A Formal Proof
Download Code
Re^2: Perl Cannot Be Parsed: A Formal Proof
by Jeffrey Kegler (Friar) on Jun 12, 2008 at 03:30 UTC
    It's never too late.

    Your workaround assumes that somehow is_nullary can determine, if perhaps only at run time, whether whatever is nullary or not. But an arbitrary function's prototype is not in general decidable, even at run time.

    Here's some hand-waving, which I hope can render the basic idea plausible: Suppose you can determine whether a piece of arbitrary code establishes (or not) a nullary prototype for whatever. Since the code is arbitary, it can contain lots of stuff, so you've said you can do more or less a complete analysis of what Perl code does. That of course includes whether it ever finishes or not. So you've also solved the Halting Problem. But that you cannot do.

    The two issues -- Halting Problem and undecidability, are intimately connected. Perl is unusual in allowing Turing-equivalent computing before the parse is determined, and in allowing this pre-parse computing to do things which will affect the parse. That's why Perl parsing is undecidable, while the parses for most languages are decidable.

    I've just finished the second of a series of articles in The Perl Review on undecidability in the Perl context. In TPR I lay the proof out more slowly than I did here in perlmonks. My discussion in TPR is aimed at Perl programmers who may not have had any interest in Theory for its own sake. Eventually, I'll put that series of articles on the Internet.

    This is a genuinely difficult area. Even those academics who've mastered the notation and memorized the results, have a very hard time with the ideas. This is why IMHO these topics so often are poorly explained.

      Your workaround assumes that somehow is_nullary can determine, if perhaps only at run time, whether whatever is nullary or not.
      But this is an entirely different problem. No one claims that calling halt() doesn't make any sense just because it is statically undecidable. This is true for arbitrary programs ( functions ) in a Turing complete language and no one claims that function composition is useless because we can never know whether all of them terminate. The implicit assumption of your article is that the parser derives one and only one parse tree ( solves all ambiguities ) from an expression and only this assumption has to be given up. The assertion becomes much weaker then:

      A Perl parser can't resolve all ambiguities of a Perl program by context sensitive analysis

      and the solution to this to derive multiple parse trees for an expression and wrap them into a common node that can represent a conditional expression for example.
        What you say is true -- in fact I am writing a parser which does ambiguous parsing, and it was that which motivated the investigation which resulted in this proof.

        However the claim is that Perl parsing is undecidable -- and listing the possible decisions does not make it decidable. Coming up with all the multiple parse trees may produce an answer adequate for most, or in particular cases, all practical purposes. But it does not decide the parse.

        Similarly, we deal with the halting problem adequately for practical purposes with every program we write. However, the halting problems remains undecidable, and that remains a useful thing to know as we in practice write 1000's upon 1000's of programs which must and do halt.

        I agree, completely. However, the question of whether one can do what you claim remains open. Put another way, if you have an ambiguous program e_0, can you transform it into the equivalent program P

        P = " if (C_0) { e_0 } elsif (C_1) { e_1 } elsif (C_2) { e_2 } ... "

        where C_i are conditionals determining which way to parse e_0, and e_i are syntactic permutations of e_0 that force Perl to parse in the particular way dictated by C_i?

        The broader conjecture is: Given an arbitrary e_0, there exists program transformation F(e_0) = P such that P is (a) finite, (b) parsed exactly one way by Perl, and (c) operationally-equivalent to e_0 -- e_0 and P produce equal results for equal inputs in all contexts.

        This would be a useful result for compiler hackers. It says that you can take a Perl program and statically transform it into a syntax that is guaranteed to remain unchanged during execution. This property allows you to design more aggressive program transformations and optimizations, unencumbered by the confusion of "what might the interpreter do to the program structure?"

        Perl is cool and all, but the fact that it isn't supported by a uniform conceptual model (small kernel of concepts from which many broader concepts are composed) will keep preventing fast compilers/interpreters and good tool support, in general.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://691380]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2014-08-20 09:19 GMT
Find Nodes?
    Voting Booth?

    The best computer themed movie is:

    Results (108 votes), past polls