Search code examples
data-structuresprogramming-languagescompiler-construction

Infix vs Postfix


Had this question in the interview yesterday. Which is better to use? Infix(with parenthesis) or Postfix? State with reason..

I only could tell them that:

  1. it is easier for the compilers to process postfix expression for arithmetic evaluations and operator precedence.
  2. More memory is used for storing and processing the parenthesis.

Please throw some light on whether I am right on this?


Solution

  • Postfix doesn't require any operator-driven order of operations; it's always explicit. So for a stack-based compiler, it's very easy to implement, and for humans, it's easy to understand the order of operations.

    On the other hand, infix doesn't require you to read all the verbs at the end :-P. Humans work on infix or even prefix; "add A to B" makes more sense than "A and B: add them".

    But, this question is fundamentally subjective. Postfix will always be better for the computer system, but infix has its merits.