beautypg.com

Post-fix notation – AMT Datasouth PAL User Manual

Page 11

background image

PAL Fundamentals

5

2.6.

Post-Fix Notation

PAL receives data objects and operation objects in an order known as post-fix notation. This
means that the data upon which an operator will operate occurs before the operator itself. This
differs from the algebraic notation which everyone learned in school.

In school, algebraic equations looked like the following.

( 1 + 2 ) × ( 4 + 5 )

In post-fix notation, this same equation has the following format.

1 2 + 4 5 + ×

Post-fix notation has the advantage that it does not require parethesis or other special symbols to
override operator precedence. The preceding algebraic equation required parenthesis it order to
give the addition operations precedence over the multipication operation. In post-fix notation, the
operators have no implied precedence. The left-most operation occurs first, and the right-most
operator occurs last. Therefore, both the computer and human need only perform the equation from
left to right as written.

In order to perform very complex equations, both algebraic and post-fix notation rely upon an op-
erand stack. When humans perform complex algebraic calculations manually, they imitate an op-
erand stack using a piece of scratch paper. When using an algebraic calculator, the calculator
contains an internal operand stack. The "(" and ")" keys on the calculator instruct the calculator to
perform push and pop operations. As the equations above show, an algebraic calculation would
require 12 key presses, not including the final "=" key, on a calculator.

Hewlett-Packard calculators have been based on reverse polish notation (or "RPN" for short) for
years. Reverse polish notation operates the same as post-fix notation. The post-fix notation equa-
tion shown above also shows seven of the nine key strokes necessary to perform the calculation
using a Hewlett-Packard RPN calculator. The equation does not show the necessary "enter" key
press following the 1 and 4.

Although not as familiar to us as algebraic notation, post-fix notation actually provides a faster and
more straight-forward approach to performing calculations.

PAL performs all of its operations using post-fix notation. As PAL encounters data objects, it
automatically pushes the objects onto the operand stack. When PAL encounters an object which
indicates an action to perform, PAL pops any necessary operands from the operand stack, performs
the action, and pushes any results onto the operand stack. In PAL, the preceding equation would
appear as the following sequence of PAL objects.

1 2 add 4 5 add mul

When PAL encounters the data object "1," it pushes the object onto the operand stack. PAL
performs the same action for the data object "2." PAL then encounters the operator "add." In
response, the interpreter pops the "2" and the "1" off the stack and adds them together. This pro-
duces the result data object "3," which PAL pushes onto the stack.

Next PAL encounters the data objects "4" and "5," which it pushes onto the stack. Then PAL
encounters the operator "add," so PAL pops the "4" and "5," adds them together, and pushes the
resulting "9". Finally, PAL encounters the "mul" operator. In response, PAL pops the "9" from the