Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I know that subrules can have repetition counts, but I can't get something like

String: 's: ' Count '"' Cdata($item{Count}) '";' to work

I'm a mere beginner with P:RD, but previously I have found the lack of backreferences to be very frustrating. Rule names are not double quote-ish things, so variables are not interpolated into rule names. But literal strings or regexes in your rules are double quote-ish things, so variables are interpolated into them. You can use that fact to create a backreference:

use strict; use warnings; use 5.012; use Parse::RecDescent; $::RD_ERRORS = 1; #Parser dies when it encounters an error $::RD_WARN = 1; #Enable warnings - warn on unused rules &c. $::RD_HINT = 1; #Give out hints to help fix problems. #$::RD_TRACE = 1; #Trace parsers' behaviour my $text = <<'END_OF_TEXT'; 5:abcdefghi END_OF_TEXT my $grammar = <<'END_OF_GRAMMAR'; { use 5.012; use Data::Dumper; my $count_match; #****DECLARE A VARIABLE**** } line: section(s /:/) section: count { $count_match = $item{count} } #**SET THE VARIABLE** | word {say Dumper(\@item)} count: m{ \d+ }xms word: m| .{$count_match} |xms #***INTERPOLATE THE VARIABLE*** END_OF_GRAMMAR my $parser = Parse::RecDescent->new($grammar) or die "Bad grammar!\n"; defined $parser->line($text) or die "Can't match text"; --output:-- $VAR1 = [ 'section', 'abcde' ];

However, your grammar needs to employ recursion in order to match your more complicated strings, i.e patterns within a pattern. And recursion is very hard. The only way I know how to do recursion in a programming language whose syntax is not specifically designed for recursion, i.e. perl, is by trial and error (thousands of times) until I get it right.

If you are just beginning with P::RD, here are some tips I wrote up:

In reply to Re: Parse::RecDescent - specifying dynamic subrule repetition by 7stud
in thread Parse::RecDescent - specifying dynamic subrule repetition by leahymr

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (6)
    As of 2019-02-16 11:43 GMT
    Find Nodes?
      Voting Booth?
      I use postfix dereferencing ...

      Results (95 votes). Check out past polls.