I don't know if I triggered L~R's hunt, but I was asking in the CB about modules for expression parsing and he expressed an interest.
I was looking for something that would be supplied with a) the string to parse, and b) the parse rules - a set of functions, binary and unary operators, and atoms. Something like this:
Expr::Parse->new({
function => [
{
name => 'p',
type => 'bool',
args => [ 'int' ],
test => sub { _is_prime($_[1]) },
},
{
name => 'rev',
type => 'int',
args => [ 'int' ],
test => sub { scalar reverse $_[1] },
},
],
binop => [
{
name => '+',
type => 'int',
args => [ 'int', 'int' ],
prec => 4,
test => sub { $_[1] + $_[2] },
},
{
name => '*',
type => 'int',
args => [ 'int', 'int' ],
prec => 3,
test => sub { $_[1] * $_[2] },
},
{
name => '=',
type => 'bool',
args => [ 'int', 'int' ],
prec => 6,
test => sub { $_[1] == $_[2] },
},
unop => [
{
name => '!',
type => 'bool',
args => [ 'bool' ],
prec => 1,
test => sub { $_[1] ? 0 : 1 },
},
],
atom => [
{
name => 'const',
pat => '\d+',
type => 'int',
test => sub { $_[1] },
},
],
});
Nobody in the CB could suggest anything at the time, so I wrote my own constructing a Tree::Simple tree as output. I found it surprisingly hard to write, even treating '(', ',' and ')' as builtins (for the function(arg, list) support), and ignoring the type information - I wrote and threw away hundreds of lines of code in at least half a dozen trial implementations before finally coming up with something workable if not particularly pretty.
If your parser can easily be extended to handle function calls I'll have a go at adapting it for my application.
Hugo
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.