Escolar Documentos
Profissional Documentos
Cultura Documentos
Generation:
Grammars
1
11. Ambiguity of Context-free Grammars
Ambiguity in a language occurs either when a symbol or an expression has more than one meaning (e.g., story), or
when an expression can be (grammatically) parsed in two different ways. The former is called lexical (or semantic)
ambiguity, and the later syntactic (or structural) ambiguity. For example, in natural language, the sentence “A man
entered the room with a picture” can be interpreted (i.e., parsed) into two different grammatical structures as follows.
2
Ambiguity
The Reply:
Dear Son,
I kNOw that astroNOmy, ecoNOmics, and oceaNOgraphy are eNOugh to keep even an hoNOr student busy.
Do NOt forget that the pursuit of kNOwledge is a NOble task, and you can never study eNOugh.
Love,
Dad
- Adrea -
3
Ambiguity
4
Parse Tree Ambiguity
Given a derivation (i.e., sequence of rules applied) for a string w, the parse tree for
w with respect to the derivation is constructed as follows. First put the root node with
the start symbol S. Then, for each leaf node on the current tree with a nonterminal
label, say A, recursively expand the tree as follows:
Suppose that A → β is a rule applied next to derive w. For each symbol X
appearing in β , in the order from left to right, a child node with label X is add to A.
This procedure repeats until the tree has no leaf nodes (labeled with a nonterminal
symbol) to expand. Reading all the leaf nodes left to right on the final tree should
give the string w. This string of terminal symbols is called the yield of the parse tree.
S
S ∨ S
A S ∧ S
G: S → S∨S | S∧S | ∼ S | A p
A A
A →p | q | r
q r
(a) (b)
5
parse tree Ambiguity
S S
S S S S
∨ ∧
A S ∨ S A
S ∧S
G: S → S∨S | S∧S | ∼ S | A p
A →p | q | r A A r
A A
q r p q
(a) (b) p∨(q∧r) (c) (p∨q)∧r
7
Parse Tree and Ambiguity Ambiguity
As we saw in the previous example, the existence of two parse trees yielding the
same string is a problematic property, called ambiguity, of a CFG that should be
eliminated. In real application, we cannot expect the correct result from a program
written in the language of an ambiguous grammar. Before we discuss how to
eliminate ambiguity from a CFG, we need a formal definition of it.
Definition (Ambiguity): A CFG G is ambiguous if there is a string x ∈L(G) for
which there are two parse trees yielding x.
Unfortunately, it is an unsolvable problem to decide whether an arbitrary CFG is
ambiguous or not. Also, there is no algorithm available that given an ambiguous
CFG, converts it to an unambiguous grammar. However, for a certain restricted
construct, it is possible to solve the problems. In this section we will present several
techniques with some examples.
8
Ambiguity
S S
S S S
∨ S
A ∧
G1: S → S∨S | S∧S | ∼ S | A S ∧S S ∨ S A
p
A →p | q | r r
A A A A
q r p q
(a) (b
)
9
Eliminating Ambiguity Ambiguity
This ambiguity can be eliminated by parenthesizing the right side of those two
rules as shown in G2 below. The parentheses make the yields of the two parse trees
different as shown in figures (a) and (b).
S S
( ) )
S S ( S
∨ S
A ( S ∧S ) ( S S ) ∧ A
∨
p
r
A A A A
q r p q
(a): (p ∨(q ∧r)) (b): ((p ∨q ) ∧r)
10
Ambiguity
Eliminating Ambiguity
S S
b b
S S
G3 : S → bS | Sb | c
S b b
S
c c
11
Ambiguity
Eliminating Ambiguity
We can simply modify the grammar G3 to G4 as shown below such that left side
b’s, if any, are always generated first. Figure (b) is the only parse tree for string
bcb. Grammar G4 is unambiguous.
Ambiguous G3 : S → bS | Sb | c
Unambiguous G4 : S → bS | A A → Ab | c
S
b
S S
S
b b
S S A
S b b b
S A
c c c
(a) (b)
12
Ambiguity
Eliminating Ambiguity
Example 2. Using the technique of fixing the order of derivation, the ambiguous
CFG G1 that we have examined can be converted to an unambiguous grammar G5
shown in figure (a). Notice that this grammar generates the operators left to right in
the order they appear in the string.
S
Ambiguous:
∼ S
G1 : S → S∨S | S∧S | ∼ S | A
A ∨ S
A →p | q | r
A ∧ S
Unambiguous: p
q A ∨ S
G5 : S → A∨S | A∧S | ∼ S | A
q A
A →p | q | r
p
(a) (b): ∼ p∨q∧q∨p
13
Eliminating Ambiguity Ambiguity
14
Ambiguity
Eliminating Ambiguity
(a) (b)
15
Ambiguity
Eliminating Ambiguity
Example. Assume that the logical operators ∼ , ∧, and ∨are right associative with
precedence given in that order (i.e., ∼ is at the top followed by ∧, and ∨at the bottom).
The ambiguous CFG G1 (repeated below) can be modified into an unambiguous G10
by implementing the precedence and associativity into the production rules.
Notice that every OR (∨) operator in the string should be generated by S → D∨S
before generating others. Then AND (∧) operators, if any, are generated by D → C∧D,
and finally NOT (∼ ) operators. Also notice that with D, there is no way to derive ∨,
and with C neither ∨nor ∧can be derived.
S
Ambiguous D ∨ S
G1: S → S∨SS∧S∼ SA ∨ S
A → pqr C D D
~ C C C ∧ D
Unambiguous
G10 : S → D∨SD D → C∧DC A A C
A
C → ∼ CA A → pqr r A
p q
(a) (b) p
16
Eliminating Ambiguity Ambiguity
This fixed order of rule applications facilitates the compiler generating an object
code to evaluate the string (i.e., a logical expression) according to the operator
precedence and associativity.
For example, because NOT (∼ ) operators are generated last, they appear on a
subtree of the parent node of a leaf node labeled with ∧or ∨. Hence, the compiler,
traversing the tree bottom up, left to right, generates the instructions which will
execute the NOT (∼ ) operators before the instructions executing other operators.
Similarly, we can see how the compiler generates the instructions to execute AND
(∧) operators before OR (∨) operators. S
D ∨ S
G10 : S → D∨SD D → C∧DC ∨ S
C D D
C → ∼ CA A → pqr
~ C C C ∧ D
A A C
∼ p ∨q ∨r ∧p ⇒ ((∼ p) ∨(q ∨(r ∧p))) A
(1) (4) (3) (2) r A
p q
(a) (b) p
17
Eliminating Ambiguity Ambiguity
Now, for the associativity, notice that identical operators are generated left to
right in the order they appear in the string. For example, all OR (∨) operators are
derived by applying S → D∨S recursively. Thus, the compiler, traversing the parse
tree bottom up, left to right, will generate an object code to evaluate the logic
expression following the order of right associativity.
If we want to implement left associativity for an operator used in the language,
we can simply reverse the right side of the rule which derives it. For example, we
use S → S∨D instead to make operator ∨left associative.
S
D ∨ S
∨ S
G10 : S → D∨SD D → C∧DC C D D
C → ∼ CA A → pqr ~ C C C ∧ D
A A C
∼ p ∨q ∨r ∧p ⇒ ((∼ p) ∨(q ∨(r ∧p))) A
(1) (4) (3) (2) p q r A
(a) (b) p
18
Ambiguity
• There are some languages that can only be generated by ambiguous context-free grammars. Such languages are call
inherently ambiguous. The following language L is an example.
L = {anbncm | m, n ≥ 1 } ∪ {anbmcm | m, n ≥ 1}
• It is an unsolvable problem to tell whether an arbitrary CFG is ambiguous or not.
• It is an unsolvable problem to convert an arbitrary ambiguous CFG, whose language is not inherently ambiguous, to an
unambiguous CFG.
19
Ambiguity
Exercises
11.1 Show that each of the following CFG’s is ambiguous, and convert it to an unambiguous CFG.
(a) S → Sa | A | a A → a | bA
(b) S → AB | ε A→ a|ε B→ b|ε
G: S → S + S | S * S | T T→ a|b
(b) In the CFG G above, let + and * be the addition and multiplication operators, respectively, and a and b be integer
variables. Convert G to a CFG G’ that satisfies the following conditions.
(i) L(G) = L(G’)
(ii) Operator * has higher precedence than operator +.
(iii) Both operators are left associative.
You should also present a parse tree showing that your grammar meets the required order of operator precedence and
associativity.
20
Exercises Ambiguity
The following problem is concerned with the ambiguity of the if-statement, which appears often in the text. Question (a)
is easy, but question (b) is challenging.
11.3 The syntax flow graph below defines a simplified <If-statement> of the Pascal programming language. Following
the convention, symbols in a circle or oval are terminals, and the words in rectangles correspond to nonterminal symbols.
(a) Transform the flow graph to a CFG G and show that G is ambiguous.
(b) Convert grammar G to an unambiguous CFG and explain in detail how you got your answer.
<IF-statement>
a-b
<statement> <bool>
c
<IF-statement>
d
a+b
21