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


In reply to RecDescent Issue: using item hash with (?) repetition specifier by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.