next next up down toc toc mail

10.8.  The Problem of Ambiguous Grammars.

A parse tree is supposed to display the structure used by a grammar to generate an input string. This structure is not unique if the grammar is ambiguous. A problem arises if we attempt to impart meaning to an input string using a parse tree; if the parse tree is not unique, then the string has multiple meanings.

We typically use a grammar to define the syntax of a programming language. The structure of the parse tree produced by the grammar imparts some meaning on the strings of the language.

If the grammar is ambiguous, the compiler has no way to determine which of the two meanings to use. Thus, the code produced by the compiler is not fully determined by the program input to the compiler.

Example: Given the following grammar, find a parse tree for the string 1 + 2 * 3:

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
A possible parse tree is:

Parse the tree in In-order and substitute the values in the nodes ASAP. The result is 7.

A possible parse tree is also:

Parse the tree in In-order and substitute the values in the nodes ASAP. The result is 9.


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)