CS 1104 Introduction to Computer Science

COMPILERS AND TRANSLATORS

Expression Analyzers

EXAMPLE

Given the expression:

~5 + 7 - 3 * 2

Start by adding the hierarchy value to each operator:

~25 +1 7 -1 3 *3 2

The order of evaluation is simply defined as - evaluate the operator with the highest hierarchy (with its adjacent operand(s)) and replace that sub-expression with the result. If two operators have the same hierarchy evaluate that on the left first. Repeat until the process until only a single value remains.

Thus:

~25 +1 7 -1 3 *3 2

~25 +1 7 -1 6

-5 +1 7 -1 6

2 -1 6

-4

NOTE the difference between the unary minus operator (~) applied to the representation of the value 5 and the negative sign associated with the result of the evaluation of that subexpression.

[TOC]

Last updated 00/11/28
© J.A.N. Lee, 2000.