next up previous contents
Next: Implicit uses of Up: CHAPTER.3: PROCEDURES AND Previous: Assignment as a

The stack and arithmetic expressions

The stack is used while evaluating arithmetic expressions so that a statement such as:

    2 + 3 -> x;
actually takes place in four steps, thus:

    (1) Put 2 on the stack
    (2) Put 3 on the stack
    (3) Do the addition, that is remove the top two items on the stack
        (ie 2 and 3) and replace them by their sum (ie 5)
    (4) Remove the top item from the stack (ie 5) and put it in
        the variable X.
A more complicated example, such as:

    2 + 3 * 4 - 5 =>
takes place in EIGHT steps, which are left as an exercise for the reader to describe, some of which are:

    (4) Do a multiplication
    (5) Do an addition
    (7) Do a subtraction
    (8) Print the contents of the stack


Aaron Sloman
Fri Jan 2 03:17:44 GMT 1998