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

Perl 6 and Perl 5 parsing

by Anonymous Monk
on Jan 14, 2011 at 09:39 UTC ( [id://882311]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

After reading couple of posts on the Interwebs about Parsing Perl. And reading this thread on Perl monks. I have a few question on Perl 6 Parsing.

Also I read this post by Chromatic on Modern Perl books. My question is..

Perl 6 syntax is more complex, in the sense the grammar is mutable. So how exactly is this done? I mean to say willn't it suffer from the same problem. If the grammar is muted runtime, shouldn't it be sent back to the parser. Which in case it won't be parsed statically(I can't take the bytecode alone and run it, I will always need the parser along with it).

Can somebody who is aware of how this has been worked around give a explanation on how it was done.

Replies are listed 'Best First'.
Re: Perl 6 and Perl 5 parsing
by masak (Scribe) on Jan 14, 2011 at 10:00 UTC
    Perl 6 syntax is more complex, in the sense the grammar is mutable.

    That's not what "syntax" means.

    I mean to say willn't it suffer from the same problem. If the grammar is muted runtime, shouldn't it be sent back to the parser. Which in case it won't be parsed statically(I can't take the bytecode alone and run it, I will always need the parser along with it).

    Not sure it makes sense for the grammar to be mutated at runtime, since the source code will already have been parsed and generated by then.

    You are correct in the sense that Perl 6 leans heavily on a way to switch between compile-time and run-time, so the two components need to be connected somehow. This is nothing new, though: BEGIN and &eval have allowed this for a long time in Perl.

      Then whats the point in separating compile time and run time in Perl6? In a way so as long there is a BEGIN block isn't that impossible.
        Then whats the point in separating compile time and run time in Perl6? In a way so as long there is a BEGIN block isn't that impossible.

        In the general case it's impossible to "separate" compile time and run time, yes. In Perl 5 as well as 6.

        But please remember that BEGIN and &eval aren't there because one day we looked the other way and they just snuck in. They're there because sometimes we need them as escape hatches when what we want to do can't be done by less crude means.

        Then whats the point in separating compile time and run time

        Speed. There's no reason to compile code every time it's executed. That would be very slow, and there's no advantages to doing it that way.

        In a way so as long there is a BEGIN block isn't that impossible.

        Not at all. It just prevents the compile-time of all code blocks from happening at the same time.

      That's not what "syntax" means.
      Really?

        Heh, I'm not arguing against the fact that grammars specify syntax. I'm saying that just because Perl 6 as a language allows for more explicit hooks into its grammar engine than Perl 5 does, it doesn't then follow that "Perl 6 syntax is more complex".

Re: Perl 6 and Perl 5 parsing
by ikegami (Patriarch) on Jan 14, 2011 at 19:02 UTC
    Perl5's grammar is mutable too.
    $ perl -E'sub f() { 0 } say f + 1;' 1 $ perl -E'sub f($) { 0 } say f + 1;' 0
    $ perl -E'sub f { say $_[0]; } f { foo => "bar" };' HASH(0x816c158) $ perl -E'sub f(&) { say $_[0]; } f { foo => "bar" };' CODE(0x817bc80)
    $ perl -E' $x = foo; say $x; sub foo { "sub" }' foo $ perl -E'sub foo; $x = foo; say $x; sub foo { "sub" }' sub

    Update: Added an example to show that prototypes aren't the only source.

      Interestingly, because Perl 5's mutability under sub declarations was so confusing to people; Perl 6's sub declarations never mutate the grammar. Apart from explicit macros or mixed-in grammar rules, the only thing that can change the grammar is a constant declaration, which declares a word that never takes arguments, much like a 0-ary sub in Perl 5. (Type declarations may be considered a specialized kind of constant declaration, where the constant being declared just happens to be a type.) In Perl 6, there are no barewords, so any unrecognized word is assumed to be a list operator that will be declared later in the same file.

      So while Perl 6's grammar is more mutable in theory; it is less frequently mutated by accident.

      Another interesting consequence of this decision is that the blocks supplied to map and grep are not a special syntax, so they require a comma. Likewise use of & in a sub signature does not change parsing. On the other hand, bare blocks are just anonymous closures in Perl 6, so they can be passed in any position, and are not restricted to the first argument as they are with Perl 5's & prototype.

Re: Perl 6 and Perl 5 parsing
by moritz (Cardinal) on Jan 14, 2011 at 18:37 UTC
    So how exactly is this done?

    Perl 6 named regexes are basically just methods in classes, which we call "grammars".

    A grammar modification creates a "slang" (short for sub language). The grammar modification is encapsulated in a role, and the process of modifying the grammar is "just" mixing the role into the current grammar object.

    That's the technical side. The user-facing side is that a Perl 6 grammar is indeed mutable, but the mutations are always well declared, and always contained to a lexical scope.

    Such a declaration usually looks like use SomeModule; or macro ... or augment slang ...

    I mean to say willn't it suffer from the same problem.

    Which problem exactly are you referring to? The fact that you can't parse Perl without running it?

    If yes, the bad news is that that's indeed still true, and the good news is that it's easier to know if an attempt to statically parse a Perl 6 source file has been successful, or has been foiled by grammar modifications.

    I can't take the bytecode alone and run it, I will always need the parser along with it

    That's only the case if you call eval(). As masak already explained, parsing happens at compile time, and running the code at run time. So while you need a run time environment for parsing, you don't need the parser at run time. Once the code is parsed, it will never be reparsed in a different way.

    In fact rakudo already demonstrates that today: you can compile a source file to various intermediate formats, and then run these files (including executables) without the parser being called.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://882311]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-03-19 10:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found