Evaluating mathematical expressions in Python

Posted by vander on Stack Overflow See other posts from Stack Overflow or by vander
Published on 2011-02-19T07:23:19Z Indexed on 2011/02/19 7:25 UTC
Read the original article Hit count: 260

Hi,

I want to tokenize a given mathematical expression into a binary tree like this:

((3 + 4 - 1) * 5 + 6 * -7) / 2

                          '/'
                        /     \
                       +        2
                    /     \
                  *         *
                /   \     /   \
               -     5   6     -7
             /   \
            +     1
          /   \
         3     4

Is there any pure Python way to do this? Like passing as a string to Python and then get back as a tree like mentioned above.

Thanks.

© Stack Overflow or respective owner

Related posts about python

Related posts about parsing