Re: option: /<option/i /\w+/ optarg(?) '>'
[email protected] ("Ron D. Smith") Thu, 20 May 2004 10:30:13 -0700
| Newsgroups | perl.recdescent |
|---|---|
| Message-ID | <[email protected]> |
On Thursday, May 20, 2004 Alexander Farber said:
> Hi,
>
> why doesn't the question mark in the optarg(?)
> below work as I expect? In the trace I see that
> optarg consumes the ">" character and then the
> option rule fails, since the final ">" is missing.
>
> Isn't P:RD supposed to backtrack and say
> "ok, the optarg(?) didn't match anything here"?
Um, why would it do this? In fact, "optarg" *does* match zero or one times,
it is "option" that does not match. This is a simple problem with your
grammar where you got (exactly...) what you asked for.
As an example, if you change optarg it will be "better":
optarg: /[^\s>]+/
Also, there is more than one way to do it but I don't "like" your first rule
for option "/<option/" but would prefer '<' 'option' as it is easier to
maintain. If the problem is that there should not be a space between '<' and
'option' than change skip.
But this is not related to your question, this is just me butting into your
style... ;-)
>
> Of course the script below works when I change
> the optarg from /\S+/ to /\w+/, but I'm curious,
> why doesn't optarg(?) mean "ZERO or one" here?
>
> Thank you
> Alex
>
> #!/usr/bin/perl -w
>
> use strict;
> use vars qw($parser $text %option);
> use Data::Dumper;
> use Parse::RecDescent;
> $RD_WARN = 1;
> $RD_HINT = 1;
> $RD_TRACE = 120;
> $parser = Parse::RecDescent->new(q(
>
> genfile: chunk(s) /^\Z/
> chunk: option | <error>
> option: /<option/i /\w+/ optarg(?) ">" {
> push @{$::option{lc $item[2]}}, $item[-2];
> }
> optarg: /\S+/
>
> )) or die 'Bad grammar';
> $text .= $_ while (<DATA>);
> defined $parser->genfile($text) or die 'Bad text';
> print STDERR Data::Dumper->Dump([\%option], [qw(option)]);
> __DATA__
> <option one>
> <option two>
>
--
Intel, Corp.
5000 W. Chandler Blvd.
Chandler, AZ 85226
--
Intel, Corp.
5000 W. Chandler Blvd.
Chandler, AZ 85226