Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

jdavidb (1361)

  (email not shown publicly)

J. David Blackstone has a Bachelor of Science in Computer Science and Engineering and nine years of experience at a wireless telecommunications company, where he learned Perl and never looked back. J. David has an advantage in that he works really hard, he has a passion for writing good software, and he knows many of the world's best Perl programmers.

Journal of jdavidb (1361)

Wednesday January 12, 2005
03:42 PM

Parse::RecDescent regexes

[ #22684 ]

The Parse::RecDescent documentation states

If a quote-delimited string or a Perl regex appears in a
production, the parser attempts to match that string or
pattern at that point in the text.

I thought I had found a bug in this: when I tried to use /\d/ to match a digit, I got a warning when the parser was created and the parser failed to work. /[0-9]/ seemed to work adequately, but then it would seem that Parse::RecDescent implements only its own subset regex language rather than using perl's.

While trying to post about the issue here, I decided to develop a minimalist program to demonstrate the problem ... and in doing so discovered that the bug was not in Parse::RecDescent at all, but in my own coding. Spot the error in the following:

use Parse::RecDescent;

my $grammar = <<"EOF";
  digit : /\d/

my $parser = Parse::RecDescent->new($grammar);

my $r;
$r = $parser->digit("7");
print "[$r]\n";

Turns out the problem is those double-quotation marks around my EOF. That causes perl in constructing the string to try to interpret the \d, which of course does not work! The warning I was getting was not P::RD, but perl warning me I'd made a string it couldn't completely handle (unrecognized escape sequence). Then of course the parser failed, because it had an empty rule in it.

The moral of the story is when you can't get someone to stand over your shoulder and expose how you're coding like an idiot, post it to use.perl instead so you can spot it yourself in the process.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.