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;
jsor
  • 637
  • 4
  • 10
  • 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 ``. – ikegami Oct 26 '18 at 01:38

0 Answers0