in reply to To make the Model WORK
but I can't see where it's going south and returning an array (or the string expression of an array). In other similar cases (outside the ifthenelse-decoder) sub-expression results are returned properly.
Since there may be more than one elsif, and else may be optional, these corresponding subexpressions return arrays (see Subrules from P::RD's docs). For example, in this production, $item[2] is an array of the semantic values of all the elsif nonterminals that were found at this point.ifthenelse: if elsif(s?) else(?) endif
Two points:
This returns an array that is structured like this:use Parse::RecDescent; use Data::Dumper; my $parser = Parse::RecDescent->new(<<'END_GRAMMAR'); expr: /\d+/ { $item[1] } | ifthenelse { $item[1] } ifthenelse: "if" expr "then" expr elsif(s?) else(?) "endif" { [ if => [@item[2,4]], @{$item[5]}, ref $item[6] ? $item[6][0] : undef ] } elsif: "elsif" expr "then" expr { [ @item[2,4] ] } else: "else" expr { $item[2] } END_GRAMMAR for (<DATA>) { print $_; print Dumper $parser->expr($_); } __DATA__ if 1 then 2 else 3 endif if 1 then 2 endif if 1 then 2 elsif 3 then 4 elsif 5 then 6 endif if 1 then 2 elsif 3 then 4 else 5 endif
This data structure is fairly uniform, and should be easy to evaluate for whatever it is you're doing.[ "if", [ cond1, result1 ], # first "if" condition/result expression + pair [ cond2, result2 ], # 0 or more "elsif" expression pairs ... otherwise ] # else branch expression, or undef if no +ne
blokhead
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: To make the Model WORK
by ikegami (Patriarch) on Jul 13, 2005 at 16:48 UTC | |
|
Re^2: To make the Model WORK
by samizdat (Vicar) on Jul 13, 2005 at 16:18 UTC |