Hi,
I am facing an issue with using the %item hash for subrules which have a "(?)" repetition specifier (i.e. match one or zero times). As an example, please have a look at the code below: For the below code, I am expecting that I get following output input
2 0 a
instead, I get
input a
The documentation states that when we use (s), the value associated with the rule is not a scalar but a reference to a list of scalars. Does this hold for the (?) construct too? If yes, then the below code should work as I dereference the value "$item{range}->[0]".
Thanks and Regards,
Aditya
use Parse::RecDescent; my $text = "input [2:0] a"; my $grammar = q { rule : 'input' range(?) identifier { print $item[1]."\n"; if (defined @{$item{range}->[0]}) { my @arr = @{$item{range}->[0]}; foreach my $i (@arr) { print $i."\n"; } } print $item{identifier}; } range : "[" from_num ":" to_num "]" {$return = [$item{from_num}, $ +item{to_num}]} from_num : number {$return = $item{number}} to_num : number {$return = $item{number}} number : /[0-9][0-9_]*/ identifier : /[a-zA-Z][a-zA-Z_0-9\.]*/ }; my $parser = new Parse::RecDescent($grammar) or die "Bad Grammar"; $parser->rule($text);
Code tags added by GrandFather
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |