Hi P::RD lovers and FAQ maintainer(s)

I stumled over the problem, that a precompiled grammar isn't
inheritable. The Parse::RecDescent module is perfectly able
to be subclassed but not when precompiled

Normally I don't use the plain P::RD
object since it is tedious and arror proune to write the
action code within the rules or as subs in the startup
action.

Errors in the grammar or as startup actions
are hard to localize and the execution time is
slow.

Normally, I subclass  the P::RD and hold the whole
parser logic in the subclass. Simple action
codes within the grammar just call the methods as
hooks via the inheritence tree. See below:

<<<<<<<<<<<<<<<<<< snip >>>>>>>>>>>>>>>>>>>
#!/usr/local/bin/perl

use MyParser;

my $grammar = <<'EOGRAMMAR';
RULE1 : 'production' { $thisparser->rule1_hook(\%item) }
EOGRAMMAR

my $parser = MyParser->new($grammar)
 or die "can't create parser,";
my $text = join '', <>;
my $res = $parser->RULE1($text));
# do something with $res
exit;
>>>>>>>>>>>>>>>> snip <<<<<<<<<<<<<<<<<<<<<<<<<<<

>>>>>>>>>>>>>>>> snip <<<<<<<<<<<<<<<<<<<<<<<<<<<
# parser module
package MyParser;
use base 'Parse::RecDescent';

sub rule1_hook {
   my ($parser, $items) = @_;
   # process args and return success or undef
   return $items; # or undef
}
1;
>>>>>>>>>>>>>>>> snip <<<<<<<<<<<<<<<<<<<<<<<<<<<


this works fine for me. I think a demo of such an usage could be included in the demo directoty of P::RD or it's for the FAQ, but what was the question?

If I have a big grammar or if I need more than one parser
object it would be fine to use the "precompiled" grammar.

But the precompiled parser module is no longer able to
be subclassed. I found (IMHO) the problematic piece of
code and it would be nice if Damian could incorporate the patch
to the next release (if the patch passes his genius brain).

Best Regards
 Charly

--
Karl Gaissmaier       KIZ/Infrastructure, University of Ulm, Germany
Email:[EMAIL PROTECTED]           Service Group Network






--- lib/Parse/RecDescent.pm     2004-04-04 00:27:16.000000000 +0200
+++ new/Parse/RecDescent.pm     2004-04-04 00:26:30.000000000 +0200
@@ -72,17 +72,20 @@
                foreach ( keys %{$self->{rules}} )
                        { $self->{rules}{$_}{changed} = 1 }
 
-               print OUT "package $class;\nuse Parse::RecDescent;\n\n";
+               print OUT "package $class;\nuse base 'Parse::RecDescent';\n\n";
 
                print OUT "{ my \$ERRORS;\n\n";
 
                print OUT $self->_code();
 
-               print OUT "}\npackage $class; sub new { ";
-               print OUT "my ";
+               print OUT "}\npackage $class; sub new { \n";
+               print OUT '  $class = shift;', "\n";
+               print OUT '  $class = ref $class || $class;', "\n";
+               print OUT "  my ";
 
                require Data::Dumper;
                print OUT Data::Dumper->Dump([$self], [qw(self)]);
+               print OUT '    bless $self, $class;', "\n";
 
                print OUT "}";
 

Reply via email to