Preview

Postfix Notation

Good Essays
Open Document
Open Document
779 Words
Grammar
Grammar
Plagiarism
Plagiarism
Writing
Writing
Score
Score
Postfix Notation
POSTFIX NOTATION
Postfix also known as Reverse Polish Notation (or RPN), is a notational system where the operation/function follows the arguments. For example, "1 2 add" would be postfix notation for adding the numbers 1 and 2.
Most programming languages use either prefix notation ("add(1, 2)" or "(add 1 2)") or infix notation ("1 add 2" or "1 + 2"). Prefix and infix are more familiar to most people, as they are the standard notations used for arithmetic and algebra.
Why then should we use postfix notation when in actual fact it seems difficult to understand?
Postfix is useful, especially for programming, because it clearly shows the order in which operations are performed, and because it disambiguates operator groupings. For example, the following postfix expression: 1 2 + 3 * 6 + 2 3 + / means "take 1 and 2, add them, take 3 and multiply, take 6 and add, take 2 and 3, add them, and divide". In contrast, the equivalent expression in Infix Notation is:
(((1 + 2) * 3) + 6) / (2 + 3)

This may seem more familiar, but note the need for parentheses to control the order of evaluation. The prefix notation would be: (/ (+ (* (+ 1 2) 3) 6) (+ 2 3)) which can be read "inside-out" to evaluate the expression.

In postfix notation which is also called Reverse Polish Notation (RPN) the operator follows the two operands, for example
Infix postfix A+B = A B+ A*(B+C) = ABC+* A*B/C = AB*C/

TRANSLATE INFIX TO POSTFIX o As before, you read the infix from left to right looking at each character in return o As you go along you copy these operands and operators to the postfix output string the trick knowing when to copy what o If the character in the infix string is an operand you copy it immediately to the postfix string. Knowing when to copy an operator is more complicated but is the same as the rule to evaluating infix expression.

Translating A+B*(C-D) to postfix

Character read from infix Expression Infix Expression parsed so far Postfix

You May Also Find These Documents Helpful