Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Parse::RecDescent problem regarding rule matching

Reply
Thread Tools

Parse::RecDescent problem regarding rule matching

 
 
Rui Maciel
Guest
Posts: n/a
 
      07-27-2008
I'm a Perl newbie and I've started to look into the Parse::RecDescent
module. Meanwhile I've stumbled on a rule matching problem. I've defined
a couple of rules to be able to deal with two different types of numbers:
integers and decimal fractions with a single decimal place. The problem
is that RecDescent returns false positives by matching the integer rule
on decimal fraction numbers.

As far as I can tell, that could only happen if somehow RecDescent
doesn't make any use of any terminal symbol to specify if the rule really
matches a pattern, which I believe could lead to a lot of false positives.

So, am I missing something or is there no solution to this problem?


Some test code follows and thanks in advance
Rui Maciel


#! /usr/bin/perl -w

use strict;
use Parse::RecDescent;
use Encode;

my $text;
my $grammar = <<'EOG';
startrule: grade1 grade2
{ print "$item[1]\t$item[2]\n";
}
grade1: /\d{1,2}\.\d/
grade2: (/\d{1,2}/|"NA")
EOG

my $parser = new Parse::RecDescent($grammar) or die "Bad grammar!\n";

open(FILE, '-') or die "CRAP ON A STICK!";
while($text = <FILE>)
{
chomp($text);
defined $parser->startrule($text) or print "$text\t<------\n";
}

 
Reply With Quote
 
 
 
 
Brian Helterlilne
Guest
Posts: n/a
 
      07-28-2008
Rui Maciel wrote:
> I'm a Perl newbie and I've started to look into the Parse::RecDescent
> module. Meanwhile I've stumbled on a rule matching problem. I've defined
> a couple of rules to be able to deal with two different types of numbers:
> integers and decimal fractions with a single decimal place. The problem
> is that RecDescent returns false positives by matching the integer rule
> on decimal fraction numbers.
>
> As far as I can tell, that could only happen if somehow RecDescent
> doesn't make any use of any terminal symbol to specify if the rule really
> matches a pattern, which I believe could lead to a lot of false positives.
>
> So, am I missing something or is there no solution to this problem?



Rules match in the order give. Different productions within a rule are
matched in the order given. RecDescent does not try to match the
"longest" production, only the first match succeeds. This means you
need to order your productions appropriately.
given the following rules, integer would always match before fraction in
the 'number' rule but the 'number2' rule would work correctly - it would
try to match a fraction and if that failed, it would try to match an
integer.

integer: \d{1,2}
fraction: \d{1,2}\.\d

number: integer | fraction
number2: fraction | integer

[test coded snipped]

--
-brian
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[rake] rule with regex executed twice for same matching string Joel VanderWerf Ruby 1 11-10-2006 08:33 PM
how to add validation rule for url in the validation-rule.xml ,I added some thing like this but......... shailajabtech@gmail.com Java 0 10-12-2006 08:36 AM
Remove parent element with a child element matching a given rule patrizio.trinchini@googlemail.com XML 4 08-22-2006 11:31 AM
Pattern matching : not matching problem Marc Bissonnette Perl Misc 9 01-13-2004 05:52 PM
New rule (in our favor) regarding photo equipment carry-on luggage. Patrick Lockwood Digital Photography 34 11-25-2003 08:46 AM



Advertisments