Why use postfix /prefix expression instead of infix

cpostfixprefixstack

I understand how to convert infix to postfix/prefix but I do not understand why postfix or prefix expression are used in computer system?

What is the advantage of postfix prefix over infix expression?

Best Answer

Both pre- and postfix have basically the same advantages over infix notation. The most important of these are:

  • much easier to translate to a format that is suitable for direct execution. Either format can trivially be turned into a tree for further processing, and postfix can be directly translated to code if you use a stack-based processor or virtual machine

  • entirely unambiguous. Infix notation requires precedence and associativity rules to disambiguate it, or addition of extra parentheses that are not usually considered part of the notation. As long as the number of arguments to each operator are known in advance, both prefix and postfix notation are entirely unambiguous: "* + 5 6 3" is (5+6)*3, and cannot be interpreted as 5+(6*3), whereas parenthesis is required to achieve with infix.

  • supports operators with different numbers of arguments without variation of syntax. "unary-op 5" and "ternary-op 1 2 3" both work fine, but need special syntax to make them work in infix.

Related Topic