Close
Register
Close Window

OpenDSA Stand-alone Modules

Chapter 0 modules

Show Source |    | About   «  0.229. Grammars - Part 1 - Derivations and Parse Trees   ::   Contents   ::   0.231. Grammars - Part 3 - Establishing Order of Operations  »

Grammars - Part 2 - Ambiguous Grammars

1. Ambiguous Grammars

The main topic of this module is ambiguous grammars. In Example Grammar 1 of Grammars - Part 1, we developed a grammar for algebraic expressions that involved three non-terminals \(<exp>, <trm>, <pri>\). Could we develop a simpler grammar for the same language that only required two non-terminals? Here is a candidate for doing that with only two non-terminals.

1.1. Example Grammar 2

\[\begin{split}\begin{eqnarray*} <exp> &::=& <exp> + <exp> \\ &|& <exp> - <exp> \\ &|& <exp> * <exp> \\ &|& <exp> / <exp> \\ &|& <pri> \\ &|& ( <exp> ) \\ <pri> &::=& A | B | C | \ldots | X | Y | Z \end{eqnarray*}\end{split}\]

Let's try to parse the expression \(A+B*C\) using this grammar. The grammar offers us a lot of options for how to start the parse. We could choose to first use the production with the \(+\) operator, as is done in the following slide-show.

Settings

Proficient Saving... Error Saving
Server Error
Resubmit

Or we could also start with the production having the \(*\) operator, in which case the parse proceeds as shown in the following slide-show.

Settings

Proficient Saving... Error Saving
Server Error
Resubmit

Note that both of the slide-shows above produce valid parse trees for this grammar. The problem, however, is that the two parse trees are different. In the first of the two parse trees, B would multiply C, which would correspond to usual operator precedence. However, in the parse tree produced by the second slide-show, B would be added to A, a result that is contrary to usual operator precedence. A grammar such as this, which allows two different parse trees for the same expression, is called an ambiguous grammar. Ambiguous grammars should always be avoided. Although they allow you to determine the syntactic correctness of an expression, the variety of parse trees they allow confounds our ability to use the parse trees for any type of reliable semantic action.

The review problem set for this module contains four review problems, the first three of which refer to the same grammar. The first problem is about determining how many parse trees a given string has in a given grammar.

2. Ambiguous Grammars (2)

This problem is again about determining how many parse trees a given string has in a given grammar.

3. Ambiguous Grammars (3)

This problem is once more about determining how many parse trees a given string has in a given grammar.

4. Ambiguous Grammars (4)

This problem will help you discover ambiguities in grammars as well as convince yourself that a grammar is not ambiguous.

   «  0.229. Grammars - Part 1 - Derivations and Parse Trees   ::   Contents   ::   0.231. Grammars - Part 3 - Establishing Order of Operations  »

nsf
Close Window