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().