Why do we need prefix, postfix notation

37,526

Solution 1

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.

Solution 2

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.

Solution 3

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

Solution 4

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).

Share:
37,526
Chander Shivdasani
Author by

Chander Shivdasani

A Programmer in race with the universe. While the universe is busy creating fools, im busy writing complex programs.

Updated on July 09, 2022

Comments

  • Chander Shivdasani
    Chander Shivdasani almost 2 years

    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

  • Derek Ledbetter
    Derek Ledbetter over 12 years
    No, Lisp doesn't use prefix notation. In Lisp, everything is parenthesized.
  • Keith Thompson
    Keith Thompson over 12 years
    @DerekLedbetter: Yes, everything is parenthesized, but the stuff between the parentheses uses prefix notation: (+ 2 2). It's not pure prefix notation.
  • Keith Thompson
    Keith Thompson over 4 years
    8 years later: The parentheses make it possible to specify how many arguments are passed to a function without knowing how many it expects.
  • John Kugelman
    John Kugelman about 3 years
    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
    John Kugelman about 3 years
    This is not how infix parsers work. I can't possibly explain how they do work in a comment, but suffice it to say, this isn't it, and no, determining precedence is not O(n^2).
  • farid bekran
    farid bekran almost 3 years
    Recursive descent parser algorithm is a good choice to parse infix expression.
  • user207421
    user207421 over 2 years
    Answer is complete nonsense. Parsing of context-free languages can be accomplished in O(N) time. This was established by Don Knuth in 1965 and known informally for many years previously. Your explanation of how it is done is complete fantasy. Don't post guesswork here.