Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

When the score is settled, each character of the text can only be matched by two rules if one rule is a production of another.

text: struct { int foo ; int bar ; } ------ - --- --- - --- --- - - IDENT "{" IDENT IDENT ";" IDENT IDENT ";" "}" --- --- --- --- type var type var --------- --------- decl decl ------------------------- decl_list --------------------------------------------- struct --------------------------------------------- parse

But in reaching that state, a rule can match, then be unmatched by a backtrack. For example, given the grammar

parse : foo1 foo2 | bar1 bar2

foo1 could matched, but PRD will backtrack if it can't follow with a foo2 match. It will then try bar1.

I'm guessing one of your productions has side-effects, so you falsely believed it has matched even though a backtrack unmatched it. I could very well be wrong because I have very little data to go on.

Update: In the following example, you'll see foo1 on the screen even though it wasn't matched.

use strict; use warnings; use Parse::RecDescent qw( ); my $grammar = <<'__EOI__'; { use strict; use warnings; } parse : foo1 foo2 /\Z/ { [ @item[0,1,2] ] } | bar1 bar2 /\Z/ { [ @item[0,1,2] ] } foo1 : "X" { print("$item[0]\n"); [ @item[0,1] ] } foo2 : "Y" { print("$item[0]\n"); [ @item[0,1] ] } bar1 : "X" { print("$item[0]\n"); [ @item[0,1] ] } bar2 : "Z" { print("$item[0]\n"); [ @item[0,1] ] } __EOI__ Parse::RecDescent->Precompile($grammar, 'Grammar') or die("Bad grammar\n");
use strict; use warnings; use Data::Dumper qw( Dumper ); use Grammar qw( ); my $parser = Grammar->new(); my $matches = $parser->parse('XZ') or die("Bad input\n"); print("\n"); print(Dumper($matches));
foo1 bar1 bar2 $VAR1 = [ 'parse', [ 'bar1', 'X' ], [ 'bar2', 'Z' ] ];

In reply to Re: Parse::RecDescent matching same line twice by ikegami
in thread Parse::RecDescent matching same line twice by kudra

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.
  • Log In?
    Username:
    Password:

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

    How do I use this?Last hourOther CB clients
    Other Users?
    Others taking refuge in the Monastery: (4)
    As of 2025-07-20 05:42 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?
      erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.