Using an Expression Stack
To implement this method we remember scanned
tokens on a stack, which allows the
following operations:
- Shift: the transfer of an input token to
the top of the stack, and
- Reduce: the action of removing the top
three items on the stack, performing the
operation they specify, and putting the
result on top of the stack.
In the process of evaluating an expression,
we may need to take the following additional
actions:
- Accept: when an expression is
successfully evaluated and the result
(from the top of the stack) is returned.
- Error: when the expression is determined to be
syntactically incorrect, e.g. (2 + 3 4)
A special token, $, is used to mark the
bottom of the stack and the end of the input
expression.