Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
XP is just a number
 
PerlMonks  

Parse::RecDescent tutorial

by thunders (Priest)
on Aug 05, 2009 at 14:49 UTC ( #786132=perlquestion: print w/ replies, xml ) Need Help??
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");

Comment on Parse::RecDescent tutorial
Download Code
Parse::RecDescent is superceded by Regexp::Grammars?
by metaperl (Curate) on Aug 05, 2009 at 15:19 UTC
    Recently Regexp::Grammars hit CPAN. It seems to be Parse::RecDescent rolled into Perl regular expressions.

    It's not clear what support there is for it (in the docs). There's no mailing list, etc.

    But my guess is that Regexp::Grammars is going to phase out Parse::RecDescent.

Re: Parse::RecDescent tutorial
by jethro (Monsignor) on Aug 05, 2009 at 16:01 UTC
    The problem is that 'return()' finishes parsing.

    Parse::RecDescent works with a variable $return to give back results from subrules to parent rules. If you change the three occurences of "return" inside the grammar (but not inside the sub 'expression') to "$return=" the parser works

      Thanks jethro++! That fixed it!
Re: Parse::RecDescent tutorial
by JadeNB (Chaplain) on Aug 05, 2009 at 21:40 UTC
    I see that jethro already answered your question (boy, how many times I ran up against that one, and every time had to puzzle it out anew!); but I thought I'd also mention that, if you're trying to learn Parse::RecDescent, it might also be worthwhile knowing about Parse::RecDescent::FAQ—I found it a very helpful resource.
Re: Parse::RecDescent tutorial
by missingthepoint (Friar) on Aug 06, 2009 at 02:12 UTC
    the manual didn't illuminate me on this one...

    My sympathy and empathy. I find the PRD docs rather dense. But I do think that you should have RTM'd a *little* more:

    Gotchas:
    2. Using a return in an action

    But you're right - it's poor form having untested code in a tutorial. :(


    S.D.G.

      Thanks, I saw that after jethro pointed me in the right direction. Doh!

      All I can say is that the docs are definitely dense, and I had my own needlessly complicated notion of what the problem was, and I just didn't search out the right section.

        We all have our moments ;)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://786132]
Approved by broomduster
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (12)
As of 2014-04-19 05:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (478 votes), past polls