<leftop> backtracks and successfully matches 4. Since the production matches, pending errors are canceled. The parsing error happens later, when * {201({2}} attempts to match /^\Z/.

This problem would be solved by committing after matching the *. Since <leftop> doesn't provide a means for us to do that, we'll have to roll out an alternative to <leftop>. The code in section 3.c.ii of Operator Associativity and Eliminating Left-Recursion in Parse::RecDescent suits your needs perfectly.

prod_op : term prod_op_[ $item[1] ] prod_op_ : PROD <commit> term prod_op_[ [ $item[1], $arg[0], $item[2] +] ] | { $arg[0] }

We can even add some extra diagnostics:

prod_op : term prod_op_[ $item[1] ] prod_op_ : PROD <commit> term prod_op_[ [ $item[1], $arg[0], $item[2] +] ] | <error?: Expecting term following operator> <reject> | { $arg[0] }

Using the test program found below generated the following output:

4 * 5 >> match ===== {201({2}} ERROR (line 1): GRRRRRRRRRRRR >> no match ===== 4 * {201({2}} ERROR (line 1): GRRRRRRRRRRRR ERROR (line 1): Expecting term following operator >> no match
use strict; use warnings; use Parse::RecDescent qw( ); { my $grammar = <<'__EOI__'; { use strict; use warnings; } parse : prod_op /^\Z/ prod_op : term prod_op_[ $item[1] ] prod_op_ : PROD <commit> term prod_op_[ [ $item[1], $arg[0], +$item[2] ] ] | <error?: Expecting term following operator> <rejec +t> | { $arg[0] } term : NUMBER | abstraction abstraction : '{' NUMBER '(' <commit> NUMBER ')' '}' | '{' <commit> NUMBER '}' | <error?: GRRRRRRRRRRRR> <reject> PROD : '*' NUMBER : /[+-]?(?:\d+\.?\d*|\.\d+)/ __EOI__ my $parser = Parse::RecDescent->new($grammar) or die; # $::RD_TRACE = 'defined'; my $not_first; foreach my $text ( '4 * 5', '{201({2}}', '4 * {201({2}}', ) { print("\n=====\n\n") if $not_first++; print("$text\n"); print("\n>> ", defined($parser->parse($text)) ? 'match' : 'no ma +tch', "\n"); } }

In reply to Re^3: Forcing parse to fail in PArse::RecDescent by ikegami
in thread Forcing parse to fail in Parse::RecDescent by suaveant

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.