How create a operator tree?

Timon Gehr timon.gehr at gmx.ch
Fri Aug 17 05:55:23 PDT 2012


On 08/17/2012 10:38 AM, Namespace wrote:
> o_O
> I was hoping that there is a shorter way than this.

I hacked together the following little parser (not extensively tested).

http://dpaste.dzfl.pl/e616692e

It transforms the input expression into polish notation, ignoring space
characters.
Note that it is significantly shorter than the regex attempt.

It handles numbers, unary +, -, binary +, -, *, /, ^, where ^
associates to the right, as well as nested parentheses. It should be
trivial to extend. (in this case you might want to generate the switch
cases automatically from the operator precedence table.)


More information about the Digitalmars-d-learn mailing list