You are on page 1of 7

# Theory of Computation (With Automata Theory)

## TOPIC TITLE: Ambiguity

Specific Objectives:
At the end of the topic session, the students are expected to:
Cognitive:
1. Formally define ambiguity in context-free grammars.
2. Show if strings are derived ambiguously given a certain
grammar.
Affective:
1. Listen to others with respect.
2. Participate in class discussions actively.

MATERIALS/EQUIPMENT:
o
o

topic slides
OHP

TOPIC PREPARATION:
o
o
o
o

Ambiguity

## Have the students review related topics that were discussed in

previous courses.
Prepare the slides to be presented in class.
It is imperative for the instructor to incorporate various kinds of
teaching strategies while discussing the suggested topics.
Prepare additional examples on the topic to be presented.

*Property of STI
Page 1 of 7

## Theory of Computation (With Automata Theory)

Ambiguity
Page 1 of 9

Ambiguity
Ambiguity in Context-Free Grammars
Introduction
This section will deal with grammars that may generate the same string in different
ways. This means that a string may have different meanings or structures. For
some situations, this may not cause any problem. But for applications like
programming languages where it is important to have a unique structure or
interpretation for each line of code, this may cause numerous errors.
Consider the following grammar G = (V, , R, S) where
V = {<expr>}

= {+, , a, b, c, (, )}
S = {<expr>}
and the set of rules R is
R = {<expr><expr>+<expr><expr><expr>(<expr>)abc}
Context-Free Languages
Page 2 of 9

Assume that the string a + b + c is to be derived from this grammar. The leftmost
derivation for the given string will be as follows:
1.

Starting production
<expr> <expr> + <expr>

2.

## Replacing the leftmost <expr> with a

<expr> a + <expr>

3.

## Replacing the remaining <expr> with <expr> + <expr>

<expr> a + <expr> + <expr>

4.

## Replacing the leftmost <expr> with b

<expr> a + b + <expr>

5.

<expr> a + b + c

<expr>

<expr>

<expr>

<expr>

Ambiguity

<expr>

*Property of STI
Page 2 of 7

## Theory of Computation (With Automata Theory)

Context-Free Languages
Page 3 of 9

## However, there is another leftmost derivation for the string a + b + c, which is as

follows:
1.

Starting production
<expr> <expr> + <expr>

2.

## Replacing the leftmost <expr> with <expr> + <expr>

<expr> <expr> + <expr> + <expr>

3.

## Replacing the leftmost <expr> with a

<expr> a + <expr> + <expr>

4.

## Replacing the leftmost <expr> with b

<expr> a + b + <expr>

5.

<expr> a + b + c

## The parse tree for the derivation is

<expr>

<expr>

<expr>

Context-Free Languages
Page 4 of 9

<expr>

<expr>

Looking at the parse tree of the first derivation, it can be shown that b and c were
added first because they are grouped together in the same subtree. This is shown
below:
<expr>

<expr>

## This implies that b + c

is grouped together

<expr>

<expr>

<expr>

After performing b + c, a is then added to the sum. In other words, the string
a + b + c in this derivation actually meant a + (b + c).

Ambiguity

*Property of STI
Page 3 of 7

## Theory of Computation (With Automata Theory)

In the second derivation, the parse tree shows that a and b are added first because
they are grouped together in the same subtree. This is shown below:
<expr>

<expr>

<expr>

<expr>

<expr>

## This implies that a + b

is grouped together

After performing a + b, c is then added to the sum. In other words, the string
a + b + c in this derivation actually meant (a + b) + c.
This is now an example of how a certain string can be derived differently using the
same grammar. For each derivation, the string a + b + c actually has two different
meanings or interpretations.
If a string can be derived from a grammar in more than one way, then the grammar
is said to be ambiguous and the string is derived ambiguously. So the string
a + b + c was derived ambiguously using grammar G. Therefore, grammar G is
ambiguous.
Another way of stating this is when a string has two or more different leftmost
derivations (or rightmost derivations) or two or more derivation trees, then the
grammar is ambiguous.
In this example, no damage was done by this ambiguity since a + (b + c),
(a + b) + c, and a + b + c give the same result as a mathematical expression.
Context-Free Languages
Page 5 of 9

## As another example, consider the derivation of the string a + b c. The leftmost

derivation for this string is as follows:
1.

Starting production
<expr> <expr> + <expr>

2.

## Replacing the leftmost <expr> with a

<expr> a + <expr>

3.

## Replacing the remaining <expr> with <expr> <expr>

<expr> a + <expr> <expr>

4.

## Replacing the leftmost <expr> with b

<expr> a + b <expr>

5.

<expr> a + b c

Ambiguity

*Property of STI
Page 4 of 7

## Theory of Computation (With Automata Theory)

<expr>

<expr>

<expr>

<expr>

Context-Free Languages
Page 6 of 9

<expr>

## However, there is another leftmost derivation for the string a + b c, which is as

follows:

1.

Starting production
<expr> <expr> <expr>

2.

## Replacing the leftmost <expr> with <expr> + <expr>

<expr> <expr> + <expr> <expr>

3.

## Replacing the leftmost <expr> with a

<expr> a + <expr> <expr>

4.

## Replacing the leftmost <expr> with b

<expr> a + b <expr>

5.

<expr> a + b c

<expr>

<expr>

<expr>

Ambiguity

<expr>

<expr>

*Property of STI
Page 5 of 7

## Theory of Computation (With Automata Theory)

Context-Free Languages
Page 7 of 9

Looking at the parse tree of the first derivation, it can be shown that b and c were
multiplied first because they are grouped together in the same subtree. This is
shown below:
<expr>

<expr>

<expr>

<expr>

## This implies that b x c

is grouped together

<expr>

After performing b c, a is then added to the product. In other words, the string
a + b c in this derivation actually meant a + (b c).
In the second derivation, the parse tree shows that a and b were added first
because they are grouped together in the same subtree. This is shown below:
<expr>

<expr>

<expr>

<expr>

<expr>

b
This implies that a + b
is grouped together

After performing a + b, c is then multiplied to the sum. In other words, the string
a + b c in this derivation actually meant (a + b) c.
As in the previous example, the string a + b c now has two different meanings or
interpretations.
Because the multiplication operation has a higher precedence over the addition
operation, the first derivation will produce the correct result. In other words the
string a + b c may be interpreted as a + (b c) but not (a + b) c.
Hence, in this example, there will be an error if the second interpretation was used.
If ambiguity in a context-free grammar will produce erroneous results, the grammar
must be rewritten to remove any ambiguities. Although removing ambiguity from a
CFG is not a part of this lesson, it will be shown in the next slide as an example.

Ambiguity

*Property of STI
Page 6 of 7

## Theory of Computation (With Automata Theory)

Context-Free Languages
Page 8 of 9

1.

2.

## <term> <factor><term> <factor>

3.

<factor> (<expr>)abc

## The parse tree for the a + b c is then given by

<expr>

<expr>

<term>

<term>

<term>

<factor>

<factor>

<factor>

Since there is only one leftmost (or rightmost derivation) for the string a + b c,
then there is only one interpretation and that is, a + (b c). The ambiguity has
therefore been removed.
Take note that it is not necessary to show how the grammar G was rewritten since
resolving ambiguity is not part of this discussion. IT is sufficient to simply show that
rewriting a grammar is just one method of address the problems caused by
ambiguity in context-free grammars.
Context-Free Languages
Page 9 of 9

## Formal Definition of Ambiguity in Context-Free Grammars

A string w is derived ambiguously in context-free grammar G if it has two or more
distinct leftmost derivations, two or more distinct rightmost derivations, or two or
more distinct parse trees.
Grammar G is ambiguous if it generates some string ambiguously.

[Ambiguity, Pages 19 of 9]

Ambiguity

*Property of STI
Page 7 of 7