I notice that there is another (unanswered) thread about this problem in the 
archive.  I believe I have a solution.

I posted the following to the comp.lang.perl.modules Usenet group:

**********************

There seems to be a problem in the parsing of the grammer parser in that
the subrule argument passing and the one-or-more left op specifiers
interact in such a way to ignore the argument passing.

For example, the following code "does not work":

use Parse::RecDescent;

my $grammar_all=q(
<autotree>
top:
definition[$item[1],'foo'](s /;/)
{
    print STDERR "parsing called with \"@arg\" and found @item\n"
}
definition:
word word 
{
    print STDERR "parsing called with \"@arg\" and found @item\n"
}
word: /\w+/ {$item[1]}
                  );
my $parserRef = new Parse::RecDescent($grammar_all); 
$res=$parserRef->top('foo bar; splat squiggle',1,2,3);

What happens is that the arguments are not passed to the definition
rule:

parsing called with "" and found definition foo bar
parsing called with "" and found definition splat squiggle
parsing called with "2 3" and found top ARRAY(0x8a4906c)

However, if the input is changed to the semanticaly equivalent leftop
syntax, the result appears correct:

top:
<leftop: definition[$item[1],'foo'] /;/ definition[$item[1],'foo']>
{
    print STDERR "parsing called with \"@arg\" and found @item\n"
}

parsing called with " foo" and found definition foo bar
parsing called with " foo" and found definition splat squiggle
parsing called with "2 3" and found top ARRAY(0x8a71bfc)

I have looked at the source code (if you can believe that) and aside
from coming to the conclusion, that Damian, you are clearly not of this
Earth (but instead from a Higher Plane), I think I know what is wrong.

In _generate() is the following code:

_parse("a one-or-more subrule match",
$aftererror,$line,"$code$argcode($1)");
if ($2)
{
  my $pos = pos $grammar;
  substr($grammar,$pos,0,"<leftop: $name $2 $name> ");
...

The $argcode seems to get lost in the substr conversion.  So if you
change the code to:

  substr($grammar,$pos,0,"<leftop: $name$argcode $2 $name$argcode> ");

it seems to work.  However, I am completely intimidated by this source
code so hesitate to state so with Complete Confidence.

Damian, did I get this right?  Are there other cases that also need to
be fixed in _generate that I'm missing?

*************************

Well, I went back and did some more testing, and more or less came to the 
conclusion that all of the substr() calls have to receive the same 
modification to interpolate the $argcode into the substitution, and this 
should be done between line numbers 2311-2507.  I added $argcode to both 
occurrences of $name in 6 places.

I did not do "extensive" regression testing, but it seems to work "better".
--
 Intel, Corp.
 5000 W. Chandler Blvd.
 Chandler, AZ 85226

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


Reply via email to