Chromatic answered: In this case, with simple lines composed of only one literal apiece, you'll either have to specify the longest token first, or rewrite the grammar slightly:

This is not entirely true. If you look at the docs for Parse::RecDescent you will see that what he says is that the "best" match is chosen, and this will be the first successful match in an alternation unless you decide to use a scored grammar in which case you suffix each rule with a score directive and all alternatives are tried and the one with the best score wins.

So, in his case, he could have a simple score directive like so:

opcode: /match_text/ <score: { length join '' @item}>
and append such lines in any order for opcode and instead of the alternatives being chosen based on order within alternation, they would be chosen based on how long a string they produced, @item being a list of the things with matched for this production.

Just look for the section "Scored productions" in the .pod documentation.


In reply to Re: Parse::RecDescent and grammar specification by metaperl
in thread Parse::RecDescent and grammar specification by jcwren

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.