Re: Negative Look-ahead problem
[email protected] (Karl Gaissmaier) Sat, 03 Apr 2004 10:51:20 +0200
| Newsgroups | perl.recdescent |
|---|---|
| Message-ID | <[email protected]> |
Hi Andras,
please stay with your e-mails on the mailing-list.
Therefore we have it.
Andras Karacsony schrieb:
> Hello Karl,
>
> I ran the code you sent me and I got:
>
> $ perl reserved_word.pl
> Parse::RecDescent: Treating "<autotree>" as an autotree marker
> Parse::RecDescent: Treating "RESERVED :" as a rule declaration
> Parse::RecDescent: Treating "AND" as a literal terminal
> Parse::RecDescent: Treating "|" as a new production
> Parse::RecDescent: Treating "OR" as a literal terminal
> Parse::RecDescent: Treating "IDENTIFIER :" as a rule declaration
> Parse::RecDescent: Treating "...!" as a negative lookahead
> Parse::RecDescent: Treating "RESERVED" as a subrule match
> Parse::RecDescent: Treating "m/[a-z0-9]+/i" as an m/../ pattern terminal
> printing code (11155) to RD_TRACE
> ANDY
> ^Z
> 1|IDENTIFIER|Trying rule: [IDENTIFIER] |
> 1|IDENTIFIER| |"ANDY\n"
> 1|IDENTIFIER|Trying production: [RESERVED /[a-z0- |
> | |9]+/i] |
> 1|IDENTIFIER|Trying subrule: [RESERVED] |
> 2| RESERVED |Trying rule: [RESERVED] |
> 2| RESERVED |Trying production: ['AND'] |
> 2| RESERVED |Trying terminal: ['AND'] |
> 2| RESERVED |>>Matched terminal<< (return value: |
> | |[AND]) |
> 2| RESERVED | |"Y\n"
> 2| RESERVED |Trying action |
> 2| RESERVED |>>Matched action<< (return value: [0])|
> 2| RESERVED |>>Matched production: ['AND']<< |
> 2| RESERVED |>>Matched rule<< (return value: [0]) |
> 2| RESERVED |(consumed: [AND]) |
> 1|IDENTIFIER|<<Didn't match subrule: [RESERVED]>> |
> 1|IDENTIFIER| |"ANDY\n"
> 1|IDENTIFIER|<<Didn't match rule>> |
> $VAR1 = undef;
>
>
> I won't pretend that I understand all this,
> but does not seem to be working.
hmmm, I've overseen that the global skip pattern
is qr/\s*/, therefore ANDY is matched as a reserved
word too, since the token prefix can be just nothing.
To change the token prefix in the RESERVED rule
to \s+ (<-- see the + instead of *) should help:
<<<<<<<<<<<<<<<<<<<<<<<<< snip >>>>>>>>>>>>>>>>>>>>>>>
#!/usr/local/bin/perl
use strict;
use warnings;
use Parse::RecDescent;
$::RD_TRACE = 1;
use Data::Dumper;
my $grammar = <<'EOG';
<autotree>
RESERVED : <skip: qr/\s+/> ('AND' | 'OR')
IDENTIFIER : ...!RESERVED m/[a-z0-9]+/i
EOG
my $parser = Parse::RecDescent->new($grammar)
or die "can't create parser,";
my $text = join '', <>;
print Dumper($parser->IDENTIFIER($text));
<<<<<<<<<<<<<<<<<<<<<<<<< snip >>>>>>>>>>>>>>>>>>>>>>>
Best Regards
Charly
--
Karl Gaissmaier KIZ/Infrastructure, University of Ulm, Germany
Email:[email protected] Service Group Network