1

I am using Parse::RecDescent(the below code) to parse something like this

this x=2 and y=2 and z=3

why the below code only print x!=2 and not all the above line (i.e x!=2 and y!=2 and z!=3) even the above line is parsed by the below code any idea what wrong with the below code ?

use strict;
use warnings;
use Parse::RecDescent;

my $input = 'x=2 and y=3 and z=3';

my $grammar = q{

startrule: expr(s?)



expr: statement operator(s?)

{ return  $item{statement}. $item{operator}; }


statement : operand equal value

{ return $item{operand}.' ' .'!='.' '.$item{value}  }



equal : /\=/

operator : /and/i

operand: /\w+/

value : /\w+/

};

my $parser = Parse::RecDescent->new($grammar);
my $result = $parser->startrule($input) or die "Couldn't parse!\n";



use Data::Dumper;
$Data::Dumper::Indent = 1;
$Data::Dumper::Sortkeys = 1;
print Dumper $result;
1
  • 1
    startrule: expr(s?) should be startrule: expr(s?) /\Z/ { $item[1] } As this reveals, x=2 and y=2 and z=3 doesn't match your grammar. Your grammar expects something like x=2 and and and. Read up on <leftop>. Commented Oct 26, 2018 at 1:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.