the output is "Bad!". The reason is that the first branch of the alternation matched, then the next subrule failed.use strict; use warnings; use Parse::RecDescent; my $grammar = 'startrule: ( "aa" | "a" ) "a"'; my $parser = Parse::RecDescent->new($grammar); my $text = 'aa'; print defined($parser->startrule($text)) ? "Good!\n" : "Bad!\n";
Now, I understand from the P::RD manpage that bottom-up parsers try all possible matches and select the longest one, so I guess the yacc equivalent would have worked. However, I still found the behaviour of P::RD a little surprising.
My question is: Is there any way to persuade a top-down parser like P::RD to accept the above text as valid? I know that, in this simple example, I could easily rewrite the grammar (either by saying ("a" | "aa" ) "a" or "aa" a" | "a" "a"). What I mean is: Is there any additional feature I have missed which would allow the grammar as is to parse the text successfully?
To put the question another way, can I get P::RD to behave more like a regex engine? After all, even an NFA engine would backtrack to try all possible alternatives before failing :-) (Perhaps parsers just do not backtrack past individual subrules under any circumstances.)
In reply to Backtracking in Parse::RecDescent by ruscoekm
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |