This is not just a "lex"-like scanner as noted in perlre,
but a fully recursive parser (note the calls to expr() inside
some of the actions).
It will properly handle multiple levels of parentheses.
The advice I would give is to instrument it up with debug prints to see what's happening,
also adding a recursion level counter to track recursive calls.
It is loosely based on this expression parser I wrote a while ago:
#!/usr/bin/perl
use strict; # mini.pl - modified Pratt parser by tybalt89
use warnings; # https://en.wikipedia.org/wiki/Pratt_parser
sub error { die "ERROR ", s/\G/ <@_> /r, "\n" }
sub expr # two statement parser - precedences: (3 **) (2 * /) (1 + -)
{
my $answer =
/\G\s* ((?:\d+(?:\.\d*)?|\.\d+)(e[+-]?\d+)?) /gcxi ? $1 :
/\G\s*\(/gc ? (expr(0), /\G\s*\)/gc || error 'missing )')[0] :
/\G\s* - /gcx ? -expr(3) : # unary minus
/\G\s* \+ /gcx ? +expr(3) : # unary plus
error 'bad operand';
$answer =
$_[0] <= 3 && /\G\s* \*\* /gcx ? $answer ** expr(3) :
$_[0] <= 2 && /\G\s* \* /gcx ? $answer * expr(3) :
$_[0] <= 2 && /\G\s* \/ /gcx ? $answer / expr(3) :
$_[0] <= 1 && /\G\s* \+ /gcx ? $answer + expr(2) :
$_[0] <= 1 && /\G\s* \- /gcx ? $answer - expr(2) :
return $answer while 1;
}
for ( @ARGV ? @ARGV : scalar <> ) # source as commandline args or stdi
+n
{
my $answer = expr(0);
/\G\s*\z/gc ? print s/\s*\z/ = $answer\n/r : error 'incomplete parse
+';
}
which was also the basis for this Re: Parsing Boolean expressions
but it doesn't need the precedence stuff.
-
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.
|