How create a operator tree?
Dmitry Olshansky
dmitry.olsh at gmail.com
Fri Aug 17 01:18:21 PDT 2012
On 17-Aug-12 03:41, Namespace wrote:
> On Thursday, 16 August 2012 at 22:05:44 UTC, Namespace wrote:
>> Is there a simple function to create an operator tree of a term?
>>
>> For example:
>> Term: 4 + 5 * 8
>> Tree:
>> [*, 5, 8, +, 4]
>>
>> Or:
>> Term: 2 * 2 + 2:
>> Tree: [*, 2, 2, +, 2]
I do suspect you want Polish notation (and it does describe tree).
Though in this respect the above is equivalent to
+ * 2 2 2
>
> That seems to work: http://dpaste.dzfl.pl/95dccfa4
> But it's more of a quick and dirty solution of me. Does anyone know, how
> can i do it more elegant?
Wait, wait, wait.
regex is not the ultimate answer put it aside for a moment please :)
What you need is a proper operator precedence parser:
http://en.wikipedia.org/wiki/Shunting-yard_algorithm
or even simpler recursive descent parser.
>
> P.S.: Exists such "Diff" function as i used there in the standard
> library? I think something like that would be very useful.
--
Olshansky Dmitry
More information about the Digitalmars-d-learn
mailing list