Phoenix.pm: Re: Parse::RecDescent
Douglas E. Miles
doug.miles at bpxinternet.com
Fri Oct 29 22:15:04 CDT 1999
>"Douglas E. Miles" wrote:
>>
>> Anybody out there use Parse::RecDescent? I won't pollute the list with
>> my question if nobody knows anything about it. :)
>>
>> --
>> For a list of the ways which technology has failed
>> to improve our quality of life, press 3.
>
>Yes. What's up?
I'm trying to parse something in the form of <something> <is|are|was>
<something else>. Where something and something else can be multiple
words. I know that the code below is totally wrong, but hopefully gives
you the idea. The problem is that the word(s) (/\w+/) greedily gobbles
up all of the words in the statement. I have tried several variations
with lookaheads (...) but haven't had any luck. ANY ideas would be
great. Thanks! OK, here's my code:
#! /usr/bin/perl
use Parse::RecDescent;
my $grammar =
q{
statement : x verb y
{
print join('|', @item) . "\n";
}
x : word(s)
{
print join('|', @item) . "\n";
$::x = $item[1];
}
y : word(s) /$/
{
print join('|', @item) . "\n";
$::y = $item[1];
}
word : /\w+/
{
print join('|', @item) . "\n";
}
verb : /is|are|was/i
{
print join('|', @item) . "\n";
$::verb = $item[1];
}
};
my $parse = new Parse::RecDescent ($grammar);
print "> ";
while (<>)
{
$parse->statement($_);
print "> ";
}
--
"We've heard that a million monkeys at a million keyboards could
produce the Complete Works of Shakespeare; now, thanks to the
Internet, we know this is not true."
--Robert Wilensky
More information about the Phoenix-pm
mailing list