Listing 2 grammtest.pl
#! /usr/local/bin/perl -sw
#
use strict;
use Parse::RecDescent;
#
my $rval = 0;
my $grammar =
q{
Command :nooptcmd EndOfString
{ $return = $item[1]; }
{ print "item0=$item[0] item1=$item[1]\n"; }
| oneoptcmd anoption EndOfString
{ $return = "$item[1] $item[2]"; }
{ print "item0=$item[0] item1=$item[1] item2=$item[2]\n"; }
| twooptcmd anoption anoption EndOfString
{ $return = "$item[1] $item[2] $item[3]"; }
{ print "item0=$item[0] item1=$item[1] item2=$item[2] item3=$item[3]\n"; }
| <error>
oneoptcmd : 'script1'
twooptcmd : 'script2' | 'script1'
nooptcmd : 'script3'
anoption : /[A-Za-z0-9\-\_\*\.]+/
EndOfString : /\Z/
};
my $parse = new Parse::RecDescent ($grammar);
print "> ";
while (<>)
{
$rval = $parse->Command($_);
if (defined $rval) { print "$rval\n"; }
print "> ";
}
|