ruscoekm has asked for the wisdom of the Perl Monks concerning the following question:
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.)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Backtracking in Parse::RecDescent
by gjb (Vicar) on Dec 06, 2002 at 13:18 UTC | |
by ruscoekm (Monk) on Dec 06, 2002 at 13:33 UTC | |
|
Re: Backtracking in Parse::RecDescent (solution)
by gjb (Vicar) on Dec 06, 2002 at 22:37 UTC | |
by ruscoekm (Monk) on Dec 06, 2002 at 22:50 UTC | |
by Anonymous Monk on Dec 07, 2002 at 01:47 UTC | |
by ruscoekm (Monk) on Dec 09, 2002 at 08:15 UTC | |
|
Re: Backtracking in Parse::RecDescent
by demerphq (Chancellor) on Dec 06, 2002 at 21:38 UTC | |
by ruscoekm (Monk) on Dec 09, 2002 at 08:35 UTC | |
|
Re: Backtracking in Parse::RecDescent
by pg (Canon) on Dec 06, 2002 at 16:48 UTC | |
by ruscoekm (Monk) on Dec 06, 2002 at 16:57 UTC |