next next up down toc toc mail

10.9.  Ambiguous Precedence

Recall the grammar for expressions given earlier:

1.   <E>:   <N>
2.   <E>:   ( <E> )
3.   <E>:   <E> + <E>
4.   <E>:   <E> - <E>
5.   <E>:   <E> * <E>
6.   <E>:   <E> / <E>
7.   <N>:   0 | 1 | 2 | ... 9

This grammar is ambiguous as shown by the two parse trees for the input string

number + number * number

The first parse tree gives precedence to multiplication over addition; the second parse tree gives precedence to addition over multiplication. In most programming languages, only the former meaning is correct. As written, this grammar is ambiguous with respect to the precedence of the arithmetic operators.

Important note: computer arithmetic is not associative! Because of overflow, it may not be the case that (a-b)+c gives the same result as a-(b+c).

back next up down toc toc mail

Created by unroff & hp-tools. © by Hans-Peter Bischof. All Rights Reserved (1998).

Last modified: 27/July/98 (12:14)