Re: Perl regular expression for implementing infix expression parser?

2016-08-14 Thread Richard Heintze via beginners
Well, I'm thinking of a simple syntax tree that would fit on a one-liner? For the first cut, I think I'll ignore unary operators and just accommodate numeric integer literals for addition, subtraction, multiplication and division. Later we can add variables, unary operators, functions like

Re: Perl regular expression for implementing infix expression parser?

2016-08-14 Thread David Mertens
Hello siegried, This is a fun question! In fact, v5.10 introduced "recursive subpatterns" into the regex lexicon, so using v5.10 or later, it is possible to implement (rather arcane) recursive descent regexes without resorting to eval-based tricks like what you have. But before diving in, a few

Perl regular expression for implementing infix expression parser?

2016-08-13 Thread Richard Heintze via beginners
I this perl one liner for evaluating infix expressions in VI and emacs/VI emulator mode: .! perl -MPOSIX   -pe ' BEGIN{ $np = qr{ \( (?: (?> [^()]+ ) | (??{ $np }) )* \) }x;$funpat = qr/$np/;} s/($funpat)=($funpat|(")[^\"]*(")|[0-9\.]+)/"$1=$3".eval($1)."$4"/ge' That $np is from the camel