A recursive descent parser, as I understand it, doesn't match the longest token first, it matches the first possibility first.

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:

main: /^\s*\Z/ | input_line | <error> EOL: /\s*\Z/ input_line: opcode_8031 | <error> # # The opcode code list. Reverse these two to fix, or RET/RETI orderi +ng to break. # opcode_8031: opcode_ret EOL | opcode_reti EOL opcode_ret: OP_RET { print "Got ret!\n" } opcode_reti: OP_RETI { print "Got reti!\n" } # # 8031 opcodes # OP_RET: 'ret' OP_RETI: 'reti'
The important change is that EOL moves from input_line to the opcode_8031 production. The code blocks in there are for debugging purposes, to make sure what I thought would match does.

Half the trick in a grammar is figuring out where to put the blank spaces. Especially the ones that aren't really blank.


In reply to Re: Parse::RecDescent and grammar specification by chromatic
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.