Migrating from Parse::RecDescent

Showing 1-4 of 4 messages
Migrating from Parse::RecDescent Ed Avis 9/10/12 8:20 AM
I have some existing code that uses Parse::RecDescent.  Rather than using a
callback style, it builds a complete syntax tree using

   $::RD_AUTOACTION = '[ @item ]'

as suggested in the Parse::RecDescent documentation.  The structure of the tree
returned follows the structure of the grammar.

What is the best way to migrate this code to Marpa?  I am looking for two things:
firstly to translate the grammar, and secondly to get a complete parse tree and
ideally translate it into the same format returned by Parse::RecDescent.

Has anyone experience of migrating?

--
Ed Avis <e...@waniasset.com>

Re: Migrating from Parse::RecDescent Ron Savage 9/10/12 3:14 PM
Hi Ed
Speaking for myself only, I have not gone thru such a transition,
although in the distant past I have used Parse::RecDescent.

However, I feel I must warn you that what you're asking is unreasonable.

The only realistic way to adopt Marpa is to embrace its way of doing
things /in its entirety/.

IMHO your phrase 'into the same format...' is a recipe for pain.

Yes, I do realize it's an understandable attitude. I would have asked
the same question years ago :-)).

--
Ron Savage
http://savage.net.au/
Ph: 0421 920 622
Re: Migrating from Parse::RecDescent Jeffrey Kegler 9/10/12 6:10 PM
Now that I think back, I once took a parser written in Parse::RecDescent and converted it to Marpa for a blog post.  If I recall correctly, I read the Parse::RecDescent solution as if it were a specification document, and then wrote a Marpa parser "from the specs".

A major difference is that Parse::RecDescent is deterministic, so that you can think in terms of "do this here", where "here" means "at location X, trying possible parse Y".  Marpa is non-deterministic, so that "here" means "at location X, trying all possible parses".  This makes Marpa a lot more powerful and allows its parsing logic to be written in C, which makes it much faster.  But the transition can take some getting used to.

Hope this helps, jeffrey
Re: Migrating from Parse::RecDescent rns 9/10/12 9:27 PM
IMHO, an approximate Marpa equivalent of RecDescent's 

$::RD_AUTOACTION = '[ @item ]' 


sub default_action { 

    # The first argument is the per-parse variable.
    # At this stage, just throw it away
    shift;

    # Throw away any undef's
    my @items = grep { defined } @_;
    
    # Return what's left
    my $result = scalar @items > 1 ? \@items : [ shift @items ];

    # If you need the matched rule's name in the parse tree
    # Get the matched rule's lhs and prepend it to the values
    my $lhs = ($Marpa::R2::Context::grammar->rule($Marpa::R2::Context::rule))[0];
    unshift @$result, $lhs;
    
    $result;
}

and setting default_action in the grammar constructor as follows

my $grammar = Marpa::R2::Grammar->new(
    {   start   => 'top_rule',
        default_action => 'default_action',
        rules => [
             # ... your rules here ...
             { 
                lhs => 'Rule1', 
                rhs => [qw(Symbol1 Symbol2)],    # Alternative1
                # leave action blank
             },
             [ 
                'Rule1', [qw(Symbol3 Symbol4)]Alternative2
                # leave action blank
             ]
        ]
    }
);

$grammar->precompute();

Hope this helps.