About a year ago, I asked this question
http://ubuntuforums.org/showthread.php?t=1996202

And the problem was resolved thanks to 11jmb's suggestion of using shunt yard algorithm

But if I can do a constant folding after generating the RPN ?
for example,
1 + 2*x + 3*x + 3
1 2 x * + 3 x * + 3 +
_00 = 2*3
_01 = 1 + _00
_02 = 3*x
_03 = _01 + _02
_04 = _03 + 3

can it be simplified into
5*x + 4 ?

Thanks