Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Question #1 is ... what do you want? (No, seriously.) What interpretation of these three strings do you want to be “correct?” Are they three distinct cases, or examples of the same one?

Sorry, I know the frustration. I see now that what I said has multiple interpretations, and that I didn't even follow what I tell others to do when posting questions like this. To clarify: I want to be able to apply my grammar to take this string:

     { hello }

...and find a match.. Then I want to be able to apply the same grammar to this string:

     {{ hello }}

...and also find a match. Then I want to be able to apply the same grammar to this string:

     {{{ hello }}}

and also find a match. By "match", I mean that my program should produce no errors, and these lines:

my $parser = Parse::RecDescent->new($grammar) or die "Bad grammar!\n"; defined $parser->startrule($text) or die "Can't match text";

...should not produce any output. So when I run my program three times on each of those strings, each time there should be no output at all. What I am trying to code is a backreference.

But now that I think about it, applying my desired grammar to this three line block of text:

    { hello }
    {{ hello }}
    {{{ hello }}}

...would only require a trivial adjustment to the grammar:

startrule: brace_block(s) #adjustment brace_block: lbrace(1..) 'hello' <something> lbrace: / [{] /xms rbrace: / [}] /xms

In any case, I figured out a solution. The grammar below allows me to parse 'hello', preceded by a variable number of braces, followed by the same number of braces that preceded 'hello':

startrule: <rulevar: $rbraces> startrule: lbrace(s) { my $lbraces = join '', @{$item[1]}; $rbraces = "}" x length $lbraces; } 'hello' "$rbraces" lbrace: / [{] /xms

Instead of using a rule for the right braces, I am using a literal. Is there a better way to produce a backreference?

Please provide a full representative example of the type of data you need to parse, and we’ll help you write the proper grammar-mojo. It does take practice.

Yes, this is part of a larger grammar I'm working on, but I don't think posting the whole thing would be helpful. I would love to have you take a look at the complete grammar when I'm done. My current problem: when I incorporate the solution above into one of my rules, I can't get the blasted rule to return the entire match to other rules without causing the error:

Can't use string ("{{") as an ARRAY ref while "strict refs" in use at +(eval 14) line 377
I find it frustrating that Damian didn't use Carp, so that I could pin down exactly which line in my code is causing that error--I could care less which line in his code is causing the error. Another thing I find frustrating is that a rule doesn't return its whole match to another rule. Why in the heck is the default to return only what matched the last term of a rule? I have no idea how the default behavior would ever be useful.

In reply to Re: Parse::RecDescent: how does <matchrule:> work? by 7stud
in thread Parse::RecDescent: how does <matchrule:> work? by 7stud

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 all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (9)
    As of 2018-04-25 09:10 GMT
    Find Nodes?
      Voting Booth?