Re: grammar problems
[email protected] (Jonas Wolf) Fri, 25 Jun 2004 15:11:50 +0100
| Newsgroups | perl.recdescent |
|---|---|
| Message-ID | <OF102CD646.F9B5C1F1-ON80256EBE.004DD672-80256EBE.004D951E@uk.ibm.com> |
I have posted about this earlier, but abandoned the work then because I
had to work on something else. Now I'm coming back to this, and I have a
question. Basically I am trying to write a parser for simple boolean
expressions. I want to be able to use 'and' and 'or' to qualify these
expressions, where 'and' should be optional. 'or' and 'and' are not
allowed to appear as a search term, but "and" forces a search for the term
'and'. To prevent 'and' and 'or' to appear as search terms, I am trying to
use the grammar definition as below:
ident_no_key : /[a-zA-Z\d]+/
{
$return = ($item[1]=~/^(OR|AND)$/i) ? undef : new
ident_no_key(@item[1..$#item]);
}
However, this gives me an error message:
Can't use string ("ident_no_key") as a SCALAR ref while "strict refs" in
use at
(eval 17) line 1375.
What am I doing wrong and how can I fix it?
Jonas
__END__
package QueryParser;
use strict;
use Parse::RecDescent;
my $grammar = q
{
<autotree>
disj : conj disjOp disj | conj
conj : term conjOp(?) conj | term
term : brack | phrase | ident_no_key
brack : '(' disj ')'
phrase : '"' ident_key(s?) '"'
ident_no_key : /[a-zA-Z\d]+/
{
$return = ($item[1]=~/^(OR|AND)$/i) ? undef : new
ident_no_key(@item[1..$#item]);
}
ident_key : /[a-zA-Z\d]+/
conjOp : /AND/i
disjOp : /OR/i
};
sub new
{
my $class = shift;
my $self = {
result => undef,
parser => new Parse::RecDescent($grammar),
};
die "Bad grammar - $!" unless ($self->{parser});
bless ($self, $class);
return $self;
}
sub parse
{
my ($self, $text) = @_;
$self->{result} = $self->{parser}->disj($text);
return defined $self->{result};
}
# ...
package ident_no_key;
sub printTree
{
my ($self, $lead, $depth) = @_;
# terminal
print $lead x $depth . "ident_no_key: " . $self->{__VALUE__} .
"\n";
}
sub formQuery
{
my ($self) = @_;
return $self->{__VALUE__};
}
sub getAllIdentifiers
{
my ($self) = @_;
return $self->{__VALUE__};
}
# ...
my $parser = QueryParser->new;
$parser->parse("@ARGV");
$parser->printTree();