On Friday, Jun 13, 2003 Richard Jelinek said:

> Hi Descendants od Rec.
> 
> Subrule/production ordering. I just don't get it.
> 
> given this grammar
> 
>     return new Parse::RecDescent (q{
>        meaning:     proplist
>                    { if(length($text)) {
>                       print ">>$text<< remains unparsed.\n";
>                       return undef;
>                      }
>                      1; }
>                  | { return undef; }
> 
>       property:    phrase '(' proplist ')'
>                  | '(' proplist ')'
>                  | '~' property
>                  | phrase
> 
> 
>       proplist:    property xor_prop(s?)
>                  | property and_prop(s?)
> 
>       xor_prop:    '|' property
>       and_prop:    ',' property
> 
> 
>       phrase:      /[^\(\)\=\,\|\>\~]+/
> 
>         });
> 
> when I try to parse 'SYN(d(e|f))'
> 
> everything goes well. If the string is 'SYN(d(e,f))', a syntax error
> is spilled out. If I swap the first two subrules/productions of
> proplist, the situation is vice versa.

Ordering is how you establish priority in ambiguous situations.  If you turn 
on $::RD_TRACE=100 you will see what the parser is doing.  If you follow the 
second SYN(d(e,f)) case you will see that the parser is correct, the grammar 
you describe does not parse that input.  Its not a problem with the order, 
its a fundamental problem with your grammar.  So its not that you do not 
understand the ordering rules, its that you do not understand your grammar.

This successfully parses both, but I'm, not sure its what you want.

$::RD_TRACE=100;

    $it= new Parse::RecDescent (q{
       meaning:     proplist
                   { if(length($text)) {
                      print ">>$text<< remains unparsed.\n";
                      return undef;
                     }
                     1; }
                 | { return undef; }

      property:    phrase '(' proplist ')'
                 | '(' proplist ')'
                 | '~' property
                 | phrase


      proplist:    property xorand_prop(s?)

      xorand_prop: xor_prop|and_prop
      xor_prop:    '|' property
      and_prop:    ',' property


      phrase:      /[^\(\)\=\,\|\>\~]+/

        });

$it->meaning('SYN(d(e|f))');
$it->meaning('SYN(d(e,f))');

> 
> I found nowhere in the Parse::RecDescent docs, that the ordering of
> productions does matter. But it seems it does. If there isn't
> something blatantly evident I've overseen this makes writing and
> maintenance of these grammars harder than it could be.
> 
> -- 
> best regards,
> 
>      Dipl.-Inf. Richard Jelinek
> 
>      - The PetaMem Group - Prague/Nuremberg - www.petamem.com -
>                      -= 2325182 Mind Units =-

--
 Intel, Corp.
 5000 W. Chandler Blvd.
 Chandler, AZ 85226

-- 
 Intel, Corp.
 5000 W. Chandler Blvd.
 Chandler, AZ  85226


Reply via email to