Parse::RecDescent syntax of grammar rules???
Todd Rockhold
todd.rockhold at ontogen.com
Fri Mar 10 02:20:14 CST 2000
~sdpm~
This is a simple example script that comes with Parse::RecDescent. As far
as I know the syntax of the grammar is correct, but the following error
messages are generated when I run the script. Any help would
be appreciated. FWIW I am trying to run this using ActiveState build 522
(Perl 5.005_03).
-------------------- start of script -----------------------
#! /usr/local/bin/perl -sw
# WHO IS NEXT TO WHOM?
use lib 'C:/Perl/lib/Parse/Parse-RecDescent-1.65/lib' ;
use Parse::RecDescent;
$::RD_HINT = 1 ;
$grammar =
q{
input : input(s)
input : who_question "\n" {1}
| is_question "\n" {1}
| statement "\n" {1}
| /bye|quit|exit/ { exit }
| <reject:!$text> <error> # ERROR IF NOT END OF
TEXT
| <resync>
statement: namelist are <commit> 'next' 'to' namelist
{ ::nextto $item[1], $item[6], $thisline; 1
}
| <error?> <reject>
who_question
: 'who' <commit> are 'next' 'to' name '?'
{ ::whonextto $item[6] ; 1 }
| <error?> <reject>
is_question
: 'is' <commit> name 'next' 'to' name '?'
{ ::isnextto($item[3], $item[6]); 1 }
| <error?> <reject>
namelist : name(s) 'and' <commit> namelist
{ [ @{$item[1]}, @{$item[3]} ] }
| name(s)
name : ...!'who' ...!'and' ...!are /[A-Za-z]+/
are : 'is' | 'are'
};
$parse = new Parse::RecDescent ($grammar);
$parse->{tokensep} = '[ \t]*';
$input = '';
print "> ";
while (<>)
{
if (/^\.$/) { $parse->inputs($input) || print "huh?\n"; $input = ''
}
else { $input .= $_ }
print "> ";
}
sub nextto($$$)
{
foreach $A ( @{$_[0]} ) {
foreach $B ( @{$_[1]} ) {
nexttoAB($A,$B,$_[2]);
}
}
print "okay\n";
}
sub nexttoAB($$$)
{
$nextto{$_[0]} or $nextto{$_[0]} = [];
$nextto{$_[1]} or $nextto{$_[1]} = [];
push @{$nextto{$_[0]}}, $_[1];
push @{$nextto{$_[1]}}, $_[0];
print "Learnt something from line $_[2]\n";
}
sub whonextto($)
{
if (defined $nextto{$_[0]})
{ print join(" and ", @{$nextto{$_[0]}}) . ".\n"; }
else
{ print "sorry, I've never heard of $_[0].\n"; }
}
sub isnextto($$)
{
if (!$nextto{$_[0]})
{ print "sorry, I've never heard of $_[0].\n"; }
elsif (!$nextto{$_[1]})
{ print "sorry, I've never heard of $_[1].\n"; }
else
{
foreach $name (@{$nextto{$_[0]}})
{
if ($name eq $_[1]) { print "yes\n"; return }
}
print "no\n";
}
}
---------- end of script -------------------------
Errors:
ERROR (line 15): Unexpected colon encountered
(Hint: Did you mean "|" (to start a new production)? Or
perhaps you forgot that the colon in a rule
definition
must be on the same line as the rule name?)
ERROR (line 20): Unexpected colon encountered
(Hint: Did you mean "|" (to start a new production)? Or
perhaps you forgot that the colon in a rule
definition
must be on the same line as the rule name?)
~sdpm~
The posting address is: san-diego-pm-list at hfb.pm.org
List requests should be sent to: majordomo at hfb.pm.org
If you ever want to remove yourself from this mailing list,
you can send mail to <majordomo at happyfunball.pm.org> with the following
command in the body of your email message:
unsubscribe san-diego-pm-list
If you ever need to get in contact with the owner of the list,
(if you have trouble unsubscribing, or have questions about the
list itself) send email to <owner-san-diego-pm-list at happyfunball.pm.org> .
This is the general rule for most mailing lists when you need
to contact a human.
More information about the San-Diego-pm
mailing list