note
ikegami
<p>When the score is settled, each character of the text can only be matched by two rules if one rule is a production of another.
<c>
text: struct { int foo ; int bar ; }
------ - --- --- - --- --- - -
IDENT "{" IDENT IDENT ";" IDENT IDENT ";" "}"
--- --- --- ---
type var type var
--------- ---------
decl decl
-------------------------
decl_list
---------------------------------------------
struct
---------------------------------------------
parse
</c>
<p>But in reaching that state, a rule can match, then be unmatched by a backtrack. For example, given the grammar
<c>
parse : foo1 foo2
| bar1 bar2
</c>
<p><c>foo1</c> could matched, but PRD will backtrack if it can't follow with a <c>foo2</c> match. It will then try <c>bar1</c>.
<p>I'm guessing one of your productions has side-effects, so you falsely believed it has matched even though a backtrack unmatched it. I could very well be wrong because I have very little data to go on.
<p><b>Update</b>: In the following example, you'll see <c>foo1</c> on the screen even though it wasn't matched.
<c>
use strict;
use warnings;
use Parse::RecDescent qw( );
my $grammar = <<'__EOI__';
{
use strict;
use warnings;
}
parse : foo1 foo2 /\Z/ { [ @item[0,1,2] ] }
| bar1 bar2 /\Z/ { [ @item[0,1,2] ] }
foo1 : "X" { print("$item[0]\n"); [ @item[0,1] ] }
foo2 : "Y" { print("$item[0]\n"); [ @item[0,1] ] }
bar1 : "X" { print("$item[0]\n"); [ @item[0,1] ] }
bar2 : "Z" { print("$item[0]\n"); [ @item[0,1] ] }
__EOI__
Parse::RecDescent->Precompile($grammar, 'Grammar')
or die("Bad grammar\n");
</c>
<c>
use strict;
use warnings;
use Data::Dumper qw( Dumper );
use Grammar qw( );
my $parser = Grammar->new();
my $matches = $parser->parse('XZ')
or die("Bad input\n");
print("\n");
print(Dumper($matches));
</c>
<c>
foo1
bar1
bar2
$VAR1 = [
'parse',
[
'bar1',
'X'
],
[
'bar2',
'Z'
]
];
</c>
747319
747319