Subject: | Perl: Regexp:Grammars |
Date: | Fri, 12 Sep 2014 10:39:03 +0200 |
To: | bug-regexp-grammars [...] rt.cpan.org |
From: | "ch_mail [...] gmx.at" <ch_mail [...] gmx.at> |
Hello!
I am not sure its a bug or I am just to stupid to do it correctly.
But I am sure you can help :-)
I am trying to use RegexpGrammars for defining a grammar in Perl.
With my first attempt I got 2 questions.
Please have a quick look at my short code.
use strict;
use warnings;
use Regexp::Grammars;
my $gr =
qr
{
<debug: off>
<warning: off>
<root>
<objrule: root>
^<X=val> <O=op> <Y=val>$
<MATCH=(?{
if ($MATCH {O} eq "+")
{
$MATCH = $MATCH {X} + $MATCH {Y};
}
elsif ($MATCH {O} eq "*")
{
$MATCH = $MATCH {X} * $MATCH {Y};
}
elsif ($MATCH {O} eq "/")
{
$MATCH = $MATCH {X} / $MATCH {Y};
}
print"\nCALC=$MATCH";
})>
<objtoken: val>
<X=([0-9]+)>
<MATCH=(?{ print "\nVAL: " . $MATCH {X}; $MATCH = $MATCH {X};
})>
<objtoken: op>
<X=([\+\*\/])>
<MATCH=(?{ print "\nOP: " . $MATCH {X}; $MATCH = $MATCH
{X}; })>
}xms;
##########################################
my $input = "10 + 3";
if ($input =~ $gr)
{
foreach (keys %/)
{
print "\nHSH: \"$_\" = " . $/{$_};
}
}
The output is:
VAL: 10
OP: +
VAL: 3
[eos] \_____<grammar> matched '10 + 3'
CALC=13
HSH: "root" = 1
HSH: "" = 10 + 3
So I think it is working correctly (CALC=13) but I am not sure about the
[eos] line.
I disabled debugging.
And I also expected 13 to be in the result-hash but root = 1 instead of
13...
Using the lastest version of the module with Perl 5.20.
Thank you!
Best regards,
Chris