Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Can I get some rules help with PARSE::RECDESCENT

by 7stud (Deacon)
on Mar 10, 2018 at 10:32 UTC ( [id://1210605]=note: print w/replies, xml ) Need Help??


in reply to Can I get some rules help with PARSE::RECDESCENT

I know I am missing something fundamental... I don't have any Idea where that array ref is coming from

Have you taken a look at Some Tips(from a beginner) for using Parse::RecDescent:

I suggest Dumping @item as the first line in an action and not writing any additional code in the action until you examine the output.

Eventually, your expr rule gets an @item array that looks like this:

expr rule: $VAR1 = [ 'expr', 'cvtype=\'problem\'', [ { 'and ' => 'problem_description match \'*\'' } ] ];

Then you return the hash ref  {$item[2], $item[1]}, where the key is $item[2], which is an array ref (it's surrounded by brackets in the Data::Dumper output). That's why you see:

$VAR1 = { 'ARRAY(0xa102e8)' => 'cvtype=\'problem\'' };

For instance:

my $href = { [1, 2, 3], "hello" }; say Dumper($href); --output:-- $VAR1 = { 'ARRAY(0x7fe1bf803638)' => 'hello' };
Here's the proof:

use strict; use warnings; use 5.020; use autodie; use Data::Dumper; use Parse::RecDescent; my $grammar =<<'END_OF_GRAMMAR'; #Start up action(executed in parser namespace): { use 5.012; #enable say() use Data::Dumper; } startrule: expr { say "startrule:"; say Dumper(\@item); $return = \@item; } expr: operand operation(s?) { say "expr rule:"; say Dumper(\@item); my $result = @{$item[2]} ? { $item[2], $item[1]} : $item[1 +]; say 'Inside expr rule: $result='; say Dumper($result); $return = $result; } operation: /and | or/ operand { say "operation rule:"; say Dumper(\@item); $return = { $item[1], $item[2] }; } operand: '(' expr ')' { say "operand rule:"; say Dumper(\@item); $return = $item[2]; } | term {say "term rule:";say Dumper(\@item);$item[1] } term: /[\w\s=><\/:"'\*_]+/ END_OF_GRAMMAR my $input = "(cvtype='problem') and (problem_description match '*')"; print "Text: $input\n"; my $parser = Parse::RecDescent->new($grammar); my $result = $parser->startrule($input) or die "Could Not Parse!\n"; say '$result:'; print Dumper $result; --output:-- Text: (cvtype='problem') and (problem_description match '*') term rule: $VAR1 = [ 'operand', 'cvtype=\'problem\'' ]; expr rule: $VAR1 = [ 'expr', 'cvtype=\'problem\'', [] ]; Inside expr rule: $result= $VAR1 = 'cvtype=\'problem\''; operand rule: $VAR1 = [ 'operand', '(', 'cvtype=\'problem\'', ')' ]; term rule: $VAR1 = [ 'operand', 'problem_description match \'*\'' ]; expr rule: $VAR1 = [ 'expr', 'problem_description match \'*\'', [] ]; Inside expr rule: $result= $VAR1 = 'problem_description match \'*\''; operand rule: $VAR1 = [ 'operand', '(', 'problem_description match \'*\'', ')' ]; operation rule: $VAR1 = [ 'operation', 'and ', 'problem_description match \'*\'' ]; expr rule: $VAR1 = [ 'expr', 'cvtype=\'problem\'', [ { 'and ' => 'problem_description match \'*\'' } ] ]; Inside expr rule: $result= $VAR1 = { 'ARRAY(0x7fb68384ac98)' => 'cvtype=\'problem\'' }; startrule: $VAR1 = [ 'startrule', { 'ARRAY(0x7fb68384ac98)' => 'cvtype=\'problem\'' } ]; $result: $VAR1 = [ 'startrule', { 'ARRAY(0x7fb68384ac98)' => 'cvtype=\'problem\'' }, $VAR1 ];

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1210605]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-20 01:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found