Parse::RecDescent problem regarding rule matching

Discussion in 'Perl Misc' started by Rui Maciel, Jul 27, 2008.

  1. Rui Maciel

    Rui Maciel Guest

    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";
    }
    Rui Maciel, Jul 27, 2008
    #1
    1. Advertising

  2. 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
    Brian Helterlilne, Jul 28, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jean-gert nesselbosch

    XPath1.0-grammar compatible to Parse::RecDescent

    jean-gert nesselbosch, May 12, 2006, in forum: XML
    Replies:
    0
    Views:
    402
    jean-gert nesselbosch
    May 12, 2006
  2. ccm news
    Replies:
    0
    Views:
    3,055
    ccm news
    Jan 15, 2009
  3. Lex Williams

    Alternative to Parse::RecDescent

    Lex Williams, Aug 26, 2008, in forum: Ruby
    Replies:
    4
    Views:
    189
    James Gray
    Aug 27, 2008
  4. Jon Ericson

    Huffman coding and Parse::RecDescent

    Jon Ericson, Apr 22, 2004, in forum: Perl Misc
    Replies:
    4
    Views:
    159
    Jon Ericson
    Apr 23, 2004
  5. A. Farber
    Replies:
    1
    Views:
    108
    Anno Siegel
    May 11, 2004
Loading...

Share This Page