43

I know how each of them can be converted to one another but never really understood what their applications are. The usual infix operation is quite readable, but where does it fail which led to inception of prefix and postfix notation

Chander Shivdasani
  • 9,878
  • 20
  • 76
  • 107
  • 1
    sin(x) is prefix. -x is prefix. Prefix is used regularly along with infix. What more do you need to know except "you're already using it". – S.Lott Sep 26 '11 at 23:24
  • @S.Lott: I believe he means for binary operators. – Peter Alexander Sep 26 '11 at 23:26
  • For postfix, http://en.wikipedia.org/wiki/Reverse_Polish_Notation" "... parenthesis-free as long as operator arities are fixed." – bitbucket Sep 26 '11 at 23:46
  • 4
    It may be that infix is quite readable only because we are so accustomed to it! – Peter Recore Sep 27 '11 at 15:41
  • 1
    I feel that prefix would be more intuitive had we been raised accustomed to it. Prefix is easy to program for in computers, but postfix uses less memory. Infix is the worst in every way, both for humans (IMO) and computers – Mooing Duck Feb 10 '14 at 21:00
  • I suggest you to read about these notations for this link http://itviewson.files.wordpress.com/2012/06/infixprefixpostfix.pdf. It will clear your all confusions – Nomiluks Jun 28 '14 at 04:10
  • Maybe its practically easy for the compilers? – deathrace Dec 09 '17 at 06:04
  • 1
    @MooingDuck If evaluated from right-to-left prefix is same as postfix. If evaluated left-to-right prefix is faster than postfix because it supports short-circuit optimization for boolean operators. – KRoy Nov 19 '18 at 17:23

4 Answers4

65

Infix notation is easy to read for humans, whereas pre-/postfix notation is easier to parse for a machine. The big advantage in pre-/postfix notation is that there never arise any questions like operator precedence.

For example, consider the infix expression 1 # 2 $ 3. Now, we don't know what those operators mean, so there are two possible corresponding postfix expressions: 1 2 # 3 $ and 1 2 3 $ #. Without knowing the rules governing the use of these operators, the infix expression is essentially worthless.

Or, to put it in more general terms: it is possible to restore the original (parse) tree from a pre-/postfix expression without any additional knowledge, but the same isn't true for infix expressions.

Jan Krüger
  • 17,870
  • 3
  • 59
  • 51
  • 2
    Prefix and postfix notation still require one to know how many operands each operator takes. They can't be parsed without that knowledge. Lisp gets around this by parenthesizing each sub-expression. `1 2 # 3 $` could equivalent to `($ (# 1 2) 3)` or `($ 1 (# 2) 3)`. – John Kugelman Mar 11 '21 at 05:00
6

Postfix notation, also known as RPN, is very easy to process left-to-right. An operand is pushed onto a stack; an operator pops its operand(s) from the stack and pushes the result. Little or no parsing is necessary. It's used by Forth and by some calculators (HP calculators are noted for using RPN).

Prefix notation is nearly as easy to process; it's used in Lisp.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
2

At least for the case of the prefix notation: The advantage of using a prefix operator is that syntactically, it reads as if the operator is a function call

beefyhalo
  • 1,691
  • 2
  • 21
  • 33
1

Another aspect of prefix/postfix vs. infix is that the arity of the operator (how many arguments it is applied to) no longer has to be limited to exactly 2. It can be more, or sometimes less (0 or 1 when defaults are implied naturally, like zero for addition/subtraction, one for multiplication/division).