http://qs1969.pair.com?node_id=1189681


in reply to Re^2: Regular Expression Test
in thread Regular Expression Test

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.