I know what you are saying. I can recursively search my tree for operators whose children (operands) are all constant (non-variables) and perform the operation on them. For example,
x+2+3 would be parsed into + x + 2 3 (read it backwards and it's RPN)
My program would identify the second + node and see that it has two children, 2 and 3, and perform the operation. The resulting tree would simply be + x 5.
However, 2+x+3 would be parsed into + 2 + x 3
Here there is no easy solution. There are no nodes with all constant children. It would seem that I have to somehow detect that the two + nodes are commutative, and try mixing the children around until I get a node with two constants.
Quite a brainteaser, but I know that computer algebra systems have been around for forty years. Actually, I got the incentive to start this project because my TI-89 graphing calculator is basically a computer algebra system that supports factoring, differentiation, integration, limits, sums, and products, matrices, etc.