in reply to Re^2: Parse::Recdescent optional subrule commit
in thread Parse::Recdescent optional subrule commit

It offhand seems to me that the behavior you might be seeking is one thing that a recursive-descent parser really does not do:   it does not “back up.”   A parser that is based, say, on Yacc or Bison-type technology can explore an avenue, find it to be fruitless, and then fall back several levels in quest of something else.   An RD parser does that sort of thing with much more difficulty, or not at all.   In particular, if you have used commit then you have cut-off any fallback beyond that point; even if the grammar-rule containing it subsequently “fails.”   The call has already been made:   its presence in the grammar is not a so-called “promise,” but an actual reality that occurs when the element is encountered.

There are other parser technologies available in the Perl environment, and it may well be that your particular requirements would be more suitable to one of them.   Otherwise, the grammar structures that you must use (such as ones which I suggested) may not entirely represent your conceptual notion of how the language is put together ... but are, in part, shaped by the characteristics of the RD parser.   I encountered this many times in my previously mentioned major parsing project.

Replies are listed 'Best First'.
Re^4: Parse::Recdescent optional subrule commit
by campugnatus (Initiate) on May 02, 2012 at 12:19 UTC

    I thought backtracking is exactly how RecDescent does selection between multiple alternatives.

    But anyway, I don't see this as an obstacle here. The desired behaviour could already be achieved if there was some way to write an action that would only be executed in case the rule it is placed in is committed.

    And I don't see any way to do so. I thought of using the $commit variable but it shows the state of commitment to the current production, not the whole rule.