mattford63 has asked for the wisdom of the Perl Monks concerning the following question:
Here's some parser code I've written in RecDescent.
The data format has dynamic columns e.g., the cpu column specifies there are 4 cpus and that the next 4 columns contain cpu values. Using another prayer here Parse::RecDescent and Dynamically Matched Subrule Repetition I've got some code, via recusion, that does what I want.
However,if I add an autoaction or an autotree directive (uncomment these in the code to replicate). The code breaks - the recursion fails. There is something i'm not understanding regarding the way code blocks return (I think). It says in the manual that autoactions (and autotree) will not modify rules that have code blocks so I don't get why the recursion stops. Any thoughts wise ones?
(an aside: is { [ $item[1] ] } equivalent to { $return=[ $item[1] ]} )?
#!/usr/bin/perl use strict; use warnings; use Parse::RecDescent; use Data::Dumper; #$::RD_AUTOACTION = q { print $item[0]."=".$item[1]."\n" }; my $parser=Parse::RecDescent->new(<<'__END_OF_GRAMMAR__'); #<autotree> line: time load cpus avg_time avg_load eof {\%item} time: int load: int avg_time: int avg_load: int #Recursively build a list of cpus based on the number of cpus reported cpus: cpu_num cpu_list[ $item{cpu_num} ] { $return=[ $item[0], $item[2 +] ] } cpu_list: { $arg[0] == 0 ? $return=[] : undef } | cpu_elem cpu_list[ $arg[0]-1 ] { $return=[ $item[1], @{$item +[2]} ] } cpu_elem: int cpu_num: int int: /\d+/ eof: /\Z/ __END_OF_GRAMMAR__ my @input=( "10 12 4 3 2 9 2 7 567", "8 9 3 1 2 3 8 8", "6 2 1 1 4 8", "6 2 0 1 4", ); foreach my $input(@input){ print Dumper($parser->line($input)); };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Parse::RecDescent and auto{actions,trees} and stuff
by pc88mxer (Vicar) on Jul 16, 2008 at 14:33 UTC | |
by mattford63 (Sexton) on Jul 16, 2008 at 14:56 UTC | |
by pc88mxer (Vicar) on Jul 16, 2008 at 16:15 UTC | |
by mattford63 (Sexton) on Jul 16, 2008 at 17:07 UTC | |
by ikegami (Patriarch) on Jul 16, 2008 at 20:14 UTC | |
|