Ted Zlatanov
Wed, 31 Oct 2007 05:31:27 -0800
On Tue, 30 Oct 2007 13:41:22 -0400 [EMAIL PROTECTED] wrote:
txb> But given all that, and the toy grammar below, which matches "sir george",
txb> how could we modify it to match "io;ajwer;i324 sir george"
Here's my solution:
1) treat the whole input as a line made of items
2) each item can be a word or a name (we try to match a 'name' first)
3) a word is any number of non-space characters
The key is to walk through the input, looking for names. I tried a few
other inputs and they seemed to work the way you want.
Ted
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
use Parse::RecDescent;
{
#last;
#$::RD_WARN++;
#$::RD_HINT++;
$::RD_TRACE++;
}
$::RD_AUTOACTION =
q { [EMAIL PROTECTED] } ;
my $G = << 'EOGRAMMAR' ;
line: item(s)
item: name | word
name: name_types eofile { $return = $item[1] }
eofile: /^\Z/
word: /\S+/
name_types: royal
royal: title firstname of(?)
title: 'sir' | 'his holiness'
firstname: 'george' | 'john'
of: 'of' place
place: 'kent'
EOGRAMMAR
my $p = Parse::RecDescent->new($G) ;
my @strings = ("hello there sir george",
"io;ajwer;i324 sir george",
"welcome his holiness john of kent");
my $parser = 'line' ;
foreach my $string (@strings)
{
my $r = $p -> $parser ( $string ) ;
warn Dumper $r;
}