CS 1104 Introduction to Computer Science

COMPILERS AND TRANSLATORS

 

Expression Analysis

BUT what about parentheses? Extend the hierarchy table to include parentheses as "additive" precedence:

Operator
Precedence
(
+10
)
-10
^
4
* or /
3
~ (unary)
2
+ or - (dyadic)
1
=
0

EXAMPLE

Given the expression:

5 + (7 - 3) * 2

Start by adding the hierarchy value to each operator:

5 +1 (+10 7 -1 3 )-10 *3 2

The parentheses can now be removed and the increment of hierarchy be transferred to the interior operators:

5 +1 7 -11 3 *3 2

The evaluation then proceeds:

5 +1 7 -11 3 *3 2

5 +1 4 *3 2

5 +1 8

13

 

[TOC]

Last updated 00/03/31
© J.A.N. Lee, 2000.