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");

In reply to Parse::RecDescent tutorial by thunders

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.