SimpleExpression : term (AddOperator term )(s?)
$item[1] relates to the first "term".
$item[2] relates to the "(AddOperator term )(s?)". So, $item[2] is an arrayref of the last element of "AddOperator term" (i.e. "term"). As is, there's no way to grab the "AddOperator" values. In fact, this is equivalent to writing two rules:
pair : AddOperator term SimpleExpression : term pair(s?)
The solution is to append an element to end of the "AddOperator term" that evaluates to an expression from which you can obtain "AddOperator" and "term". This will do:
pair : AddOperator term { [EMAIL PROTECTED],2]] } SimpleExpression : term pair(s?)
You can also express this as a single rule due to the lexical scoping on @item:
SimpleExpression : term (AddOperator term { [EMAIL PROTECTED],2]] })(s?)
Now, $item[2] is an arrayref of arrayrefs containing both AddOperator and term.
However, you might want to take a look at the <leftop: ...> directive, which provides a more convenient syntax for this common idiom:
SimpleExpression : <leftop: term AddOperator term>
In fact, there's even a short-hand for the short-hand, and this may work for your application as well:
SimpleExpression : term(s /[-+]/)
See the Parse::RecDescent POD in the section under "Specifying operations".
-davidm
PerlDiscuss - Perl Newsgroups and mailing lists wrote:
With the following code fragment, I realise I can access the array containing the term values using the notation $item[2] as shown below:
SimpleExpression : term (AddOperator )(s?) { my $SimpleExpressionArray = $item[2]; my @SimpleExpression = @$SimpleExpressionArray; }
However, if I have repetition of multiple statements such as (AddOperator term )(s?) as show below. How can I access both the AddOperator values, and the term values. If I access $item[2] as in the above example, it only contains the term rule values and the AddOperator values apear to be lost.
SimpleExpression : term (AddOperator term )(s?) { }
I apologise if I have not included enough detail, I have tried to be direct and to the point. Please let me know if you require more detail.
Paul Kennerley