Using Marpa::R2::NAIF to clean up Context-Free Grammars

11 views
Skip to first unread message

Ruslan Shvedov

unread,
Sep 26, 2014, 12:30:56 PM9/26/14
to marpa-...@googlegroups.com
Unproductive and unaccessible rules can be removed from a grammar easily using code like this:

# cleanup the grammar using Marpa NAIF
my $grammar = Marpa::R2::Grammar->new({
start => ...,
rules => ...,
unproductive_ok => 1,
inaccessible_ok => 1,
});
$grammar->precompute();

my $rules = $grammar->show_rules;
my @cleaned;
for my $rule (grep {!/unproductive|inaccessible/} split /\n/m, $rules){
my (undef, $lhs, @rhs) = split /^\d+:\s+|\s+->\s+|\s+/, $rule;
push @cleaned_pfg, [ $lhs, \@rhs ];
}

Working example can be seen here. Just a quick hack, but kind of cool and saved my day. I wonder whether such use is legitimate and, if so, can it be worth a special method, e.g. Marpa::R2::Grammar::cleanup().

Christopher Layne

unread,
Sep 26, 2014, 5:54:52 PM9/26/14
to marpa-...@googlegroups.com
Couldn't you just not use unproductive_ok and inaccessible_ok?
> --
> You received this message because you are subscribed to the Google Groups "marpa parser" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to marpa-parser...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Ruslan Shvedov

unread,
Sep 27, 2014, 1:33:05 AM9/27/14
to marpa-...@googlegroups.com
On Sat, Sep 27, 2014 at 12:54 AM, Christopher Layne <cla...@anodized.com> wrote:
Couldn't you just not use unproductive_ok and inaccessible_ok?
Yes I could, and tried that at first, but that issued warnings I hated silencing.
Reply all
Reply to author
Forward
0 new messages