I am currently making a simple calculator parser in Java, to deal only with + and - operators, and whole numbers. I have read about postfix and infix evaluation, and I am wondering if brackets can be used with either of these methods?
The beauty of the postfix/prefix notation is that you do NOT need the brackets. Brackets are used in infix notation because there are ambiguities, e.g:
a + b - c
This can mean either of the following:
(a + b) - c
a + (b - c)
However, in postfix notation, they are clearly different:
a b + c -
a b c - +
There is no need for parantheses/brackets to enforce evaluation order in postfix notation.
You can take an expression in infix notation with parantheses and convert it to postfix notation, obeying operator precedence. One such algorithm is Edsger Dijkstra's stack-based "shunting-yard algorithm".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With