I've been playing around with
Parse::RecDescent, and have encountered something that I'm unclear on what the correct behavior should be.
The code fragment below represents a portion of a grammar for an assembler. There are two instructions, RET and RETI. If RET is specified before RETI in the grammar, RETI is not recognized by the parser. However, reversing them solves the problem, causing both to be recognized.
As I understand Parse::RecDescent, the specification of the grammar should not be order dependent. I believe this for several reasons. 1) I never saw it in the documentation (though I could have missed it); 2) One of the whole points of a recursive descent parser is cases just like this; 3) If one has a machine generated grammar, it could be very difficult if not impossible to determine a prerequired order of the grammar statements.
My question is: Am I mis-understanding a requirement of the grammar specification, or have I run across a bug in Parse::RecDescent (the former more like, the latter less so. After all, it's Damianware!)
#!/usr/local/bin/perl -ws
use strict;
use Parse::RecDescent;
{
$| = 1;
my $grammar;
{
local $/;
$grammar = <DATA>;
}
my $parser = Parse::RecDescent->new ($grammar);
print ">";
while (<>)
{
print $parser->main ($_), ">" || die "Bad Code";
}
}
__DATA__
main:
/^\s*\Z/
| input_line EOL
| <error>
EOL:
/\s*\Z/
input_line:
opcode_8031
#
# The opcode code list. Reverse these two to fix, or RET/RETI orderi
+ng to break.
#
opcode_8031:
opcode_ret
| opcode_reti
opcode_ret:
OP_RET
opcode_reti:
OP_RETI
#
# 8031 opcodes
#
OP_RET: 'ret'
OP_RETI: 'reti'
--Chris
e-mail jcwren
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.