20.2. Grammars - Part 2¶
20.2.1. RP 2 part 1¶
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.
20.2.1.1. Example Grammar 2¶
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.
Or we could also start with the production having the \(*\) operator, in which case the parse proceeds as shown in the following slide-show.
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.
20.2.2. RP 2 part 2¶
This problem is again about determining how many parse trees a given string has in a given grammar.
20.2.3. RP 2 part 3¶
This problem is once more about determining how many parse trees a given string has in a given grammar.
20.2.4. RP 2 part 4¶
This problem will help you discover ambiguities in grammars as well as convince yourself that a grammar is not ambiguous.