thunders has asked for the wisdom of the Perl Monks concerning the following question:
I'm trying my best to understand how Parse::RecDescent works, and I found what seems like a pretty good tutorial on Perl.com. However, there appear to be a couple of bugs in the code.
I've pasted the code from the article verbatim, with a couple of test cases that do not perform as expected. In particular any invocation of print_instruction after the initial call fail. Additionally any complex expression within an assign_instruction will succeed(I tested that the variable gets the correct value), but immediately halts parsing of the rest of the rule.
I couldn't find the author, Jeffrey Goff's email address on perl.com, or I'd contact him first. I noticed that this is an eight year old article, but I don't think the errors are version specific. But beyond that, I'd just like to understand why these cases are failing, since the grammar appears correct to me as well.
Are there any P::RD gurus who can take one look at this and tell me what I'm missing. I tried to RTFM, but the manual didn't illuminate me on this one.
#!/usr/bin/perl -w use strict; use Parse::RecDescent; use Data::Dumper; use vars qw(%VARIABLE); # Enable warnings within the Parse::RecDescent module. $::RD_ERRORS = 1; # Make sure the parser dies when it encounters an er +ror $::RD_WARN = 1; # Enable warnings. This will warn on unused rules &c +. $::RD_HINT = 1; # Give out hints to help fix problems. my $grammar = <<'_EOGRAMMAR_'; # Terminals (macros that can't expand further) # OP : m([-+*/%]) # Mathematical operators INTEGER : /[-+]?\d+/ # Signed integers VARIABLE : /\w[a-z0-9_]*/i # Variable expression : INTEGER OP expression { return main::expression(@item) } | VARIABLE OP expression { return main::expression(@item) } | INTEGER | VARIABLE { return $main::VARIABLE{$item{VARIABLE}} } print_instruction : /print/i expression { print $item{expression}."\n" } assign_instruction : VARIABLE "=" expression { $main::VARIABLE{$item{VARIABLE}} = $item{expres +sion} } instruction : print_instruction | assign_instruction startrule: instruction(s /;/) _EOGRAMMAR_ sub expression { shift; my ($lhs,$op,$rhs) = @_; $lhs = $VARIABLE{$lhs} if $lhs=~/[^-+0-9]/; return eval "$lhs $op $rhs"; } my $parser = Parse::RecDescent->new($grammar); #These Work as Expected print "a=2\n"; $parser->startrule("a=2"); print "a=1+3\n"; $parser->startrule("a=1+3"); print "print 5*7\n"; $parser->startrule("print 5*7"); print "print 2/4\n"; $parser->startrule("print 2/4"); print "print 2+2/4\n"; $parser->startrule("print 2+2/4"); print "print 2+-2/4\n"; $parser->startrule("print 2+-2/4"); print "a = 5 ; print a\n"; $parser->startrule("a = 5 ; print a"); #This works too... print "a = 5; b = 2; print a+b\n"; $parser->startrule("a = 5; b=2; pri +nt a+2"); #These Do not. #print bug print "a = 5; b = 2; print a; print 1+1\n"; $parser->startrule("a = 5; + b=2; print a; print 1+1"); #assign bug print "a = 5+1 ; print a\n"; $parser->startrule("a = 5+1 ; print a");
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Parse::RecDescent tutorial
by jethro (Monsignor) on Aug 05, 2009 at 16:01 UTC | |
by thunders (Priest) on Aug 05, 2009 at 16:17 UTC | |
Parse::RecDescent is superceded by Regexp::Grammars?
by metaperl (Curate) on Aug 05, 2009 at 15:19 UTC | |
Re: Parse::RecDescent tutorial
by JadeNB (Chaplain) on Aug 05, 2009 at 21:40 UTC | |
Re: Parse::RecDescent tutorial
by missingthepoint (Friar) on Aug 06, 2009 at 02:12 UTC | |
by thunders (Priest) on Aug 06, 2009 at 02:26 UTC | |
by Anonymous Monk on Aug 06, 2009 at 02:42 UTC |