Dogma has asked for the wisdom of the Perl Monks concerning the following question:

Hi Folks,

I have a recursive production and I want to return an array of the all the tokens matched by 'primitive'.

(The complete grammar is rather large so I'm posting just a snippet.)

vvalue: primitive vector_sep(?) vvalue | primitive vector_sep(?)

In an attempt to see the data structure being produced, I tried this:

vvalue: primitive vector_sep(?) vvalue { \@item } | primitive vector_sep(?) { \@item }

Which according to Data:Dumper, produces the structure below from "2,3 5 7,11,13 17".

[ 'vvalue', '2', [ ',' ], [ 'vvalue', '3', [], [ 'vvalue', '5', [], [ 'vvalue', '7', [ ',' ], [ 'vvalue', '11', [ ',' ], [ 'vvalue', '13', [], [ 'vvalue', '17', [], $VAR1->[6]{'value'}[3][3][3][3][3 +][3] ], $VAR1->[6]{'value'}[3][3][3][3][3] ], $VAR1->[6]{'value'}[3][3][3][3] ], $VAR1->[6]{'value'}[3][3][3] ], $VAR1->[6]{'value'}[3][3] ], $VAR1->[6]{'value'}[3] ], $VAR1->[6]{'value'} ],

What I'd like to receive back is [2,3,5,7,11,13,17].

Can someone point me in the right direction?

Cheers,

-J

Replies are listed 'Best First'.
Re: capturing part of a recursive Parse::RecDescent production
by ikegami (Patriarch) on Feb 24, 2005 at 03:04 UTC

    For starters, don't use \@item, use [ @item ]. That'll get rid of the weird junk at the end. Then you can flatten the vvalue return as follows:

    vvalue : primitive vector_sep(?) vvalue { [ $item[1], @{$item[3]} ] } | primitive vector_sep(?) { [ $item[1] ] }

    Your grammar can be optimized to remove unneeded backtracking. Here's a (completely) equivalent grammar:

    vvalue : vvalue_(s) { [ map { @$_ } @{$item[1]} ] } vvalue_ : primitive vector_sep(?) { [ $item[1] ] }

    Both return:

    $result = [ '2', '3', '5', '7', '11', '13', '17' ];