Andre said:
I think that for you to have "postfix", or "wathever-fix", you need:
* One operator
* One or more operands
What are the operands for the last "+" in the expression?
Ther result of 2 3 + 4 *
and the 1
You obviously never had a HP pocket calculator
Consider the tree
+
/ \
1 *
/ \
+ 4
/ \
2 3
There are various ways to run through the tree:
preorder, inorder, postorder
preorder: the node first, then left subtree, right subtree
inorder: left subtree first, node, right subtree
postoder: left subtree first, right subtree, node
doing this for the above tree gives:
preorder: + 1 * + 2 3 4
inorder: 1 + 2 + 3 * 4
postorder: 1 2 3 + 4 * +
All 3 representations represent the same tree (same expression),
but only inorder requires some ( )
1 + ( ( 2 + 3 ) * 4 )
to guide you through the presedence during evaluation. The other 2 don't need that.
This is just the same as:
push 1
push 2
add
push 4
multiply
add
You forget one
push 3
I don't know if it is "superior", but it is much easier to parse since
there are no recursions.
Parsing is not the problem. Evaluating is!
With inorder notation, you need to incorporate the arithmetic rules
of precedence. It is not uncommon for compilers to first build
an expression tree from an infix notation, traverse that expression
tree in postorder and emit the code from that postorder traversel.
Why? Because arithmetic precedence has already been dealt with during
parsing and the tree has organized the opertions already correctly.
Sorry, I don't understand this :-(
+ add the next 2 expressions
1 the immediatly following expression equals 1
* + 2 3 4 thats the second expressions that gets added (since it starts with *
it is not a number, but an expression)
More exactly, it is the result of that expression that participates
in the add, so lets evaluate that
* multiply
+ 2 3 the result of that
4 with 4
Now we need to evaluate + 2 3
+ add
2 this
3 with that
in a more functional approach (as eg. in Lisp) the whole expression can be written
as:
add( 1, times( add( 2, 3 ), 4 ) )