Re: Errors not getting reported correctly
[email protected] ("Ron D. Smith") Fri, 19 Dec 2003 16:30:10 -0700
| Newsgroups | perl.recdescent |
|---|---|
| Message-ID | <[email protected]> |
On Thursday, Dec 18, 2003 "Gary" said:
> No matter what I do, I can't seem to coax Parse::RecDescent into reporting
> errors from the right location in what I've reduced to the following code
> snippet. I would expect for P::RD to report line 7 as an error in the
> following example, but instead, it reports from line 2, near the start of
> the parse tree, regardless of where I put the error. Any ideas?
>
> ===================================
>
> #! /usr/local/bin/perl -w
> use Parse::RecDescent;
> use Data::Dumper;
>
> my $parser = new Parse::RecDescent('
>
> <autotree>
> prog : item eof
> | <error>
> item : /\w+/ list(?)
The problem is that item succeeds, because list is optional. You cannot pass
an error back up through an optional production, because one of the options
is no option, which consumes the error!
> list : "(" item(s) ")"
> eof : /^\Z/
>
> ');
> my $test = '
> a(
> b
> c
> d(
> 1
> 2
> !!wrong!!
> 4
> )
> e
> )
> ';
>
> print Dumper $parser->prog($test);
>
use Parse::RecDescent;
use Data::Dumper;
$::RD_TRACE=50;
#$::RD_HINT=1;
my $parser = new Parse::RecDescent('
<autotree>
prog : item eof
item : /\w+/ list
| /\w+/ ...!"("
| /\S+/ <error: Expected list, received \"$item[1]\" instead.>
list : "(" <commit> item(s) ")"
| /\S+/ <error?: Expected list, received \"$item[1]\" instead.> {1}
eof : /^\Z/
');
my $test = '
a(
b
c
d(
1
2
!!wrong!!
4
)
e
)
';
print Dumper $parser->prog($test);
will produce something like what you want:
ERROR (line 8): Expected list, received "!!wrong!!" instead.
ERROR (line 5): Expected list, received "d(" instead.
ERROR (line 2): Expected list, received "a(" instead.
$VAR1 = undef;
Yes, there are "too many" error messages here, but that is because the
production fails at each level and the error stack unwinds.
--
Intel, Corp.
5000 W. Chandler Blvd.
Chandler, AZ 85226
--
Intel, Corp.
5000 W. Chandler Blvd.
Chandler, AZ 85226