Migrating from Parse::RecDescent

21 views
Skip to first unread message

Ed Avis

unread,
Sep 10, 2012, 11:18:11 AM9/10/12
to marpa-...@googlegroups.com
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>

Ron Savage

unread,
Sep 10, 2012, 6:17:11 PM9/10/12
to marpa-...@googlegroups.com
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

Jeffrey Kegler

unread,
Sep 10, 2012, 9:10:27 PM9/10/12
to marpa-...@googlegroups.com
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

rns

unread,
Sep 11, 2012, 12:27:47 AM9/11/12
to marpa-...@googlegroups.com, e...@waniasset.com
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.
Reply all
Reply to author
Forward
0 new messages