Sei sulla pagina 1di 23

German University in Cairo, Winter 2013

Ambiguity + Chomsky Normal Form


Lecture 10
November 11, 2013

c
Haythem
O. Ismail

German University in Cairo, Winter 2013

Objectives
By the end of this lecture, you should be able to
Give a leftmost derivation of a string in some CFG.
Determine whether a CFG is ambiguous.
Determine whether a CFG is in Chomsky normal form.
Convert a CFG into one in Chomsky normal form.

c
Haythem
O. Ismail

German University in Cairo, Winter 2013

Ambiguity
A CFG G is ambiguous if it generates some string with more
than one parse tree.
Such a string will have different meanings.
Example: Consider a grammar G for arithmetic expressions.
hexpri

hexpri + hexpri | hexpri hexpri

(hexpri) | a

G is ambiguous.
Draw two different parse trees for the string a + a a

c
Haythem
O. Ismail

German University in Cairo, Winter 2013

An Ambiguous Grammar for English


hsentencei

hnoun-phraseihverb-phrasei

hnoun-phrasei

hcmplx-nouni | hcmplx-nounihprep-phrasei

hverb-phrasei

hcmplx-verbi | hcmplx-verbihprep-phrasei

hprep-phrasei

hprepihcmplx-nouni

hcmplx-nouni

harticleihnouni

hcmplx-verbi

hverbi | hverbihnoun-phrasei

harticlei

a | the

hnouni

boy | girl | flower

hverbi

touches | likes | sees

hprepi

with

c
Haythem
O. Ismail

German University in Cairo, Winter 2013

Derivations and Parse Trees


For every derivation there is a unique corresponding parse tree.
The same parse tree may correspond to two different
derivations, though.
Two derivations differ in two respects:
which variable is chosen for substitution at a given step, and
which rule is used to rewrite the chosen variable.
Two derivations that differ only in the first respect would have
the same parse tree.
To formally define ambiguities in terms of derivations, we fix a
strategy for choosing variables for substitution.
A derivation is a leftmost derivation if the variable chosen
for substitution, at any step, is the leftmost variable.
c
Haythem
O. Ismail

German University in Cairo, Winter 2013

Definition of Ambiguity
A string w is derived ambiguously in CFG G if it has two or
more leftmost derivations.
CFG G is ambiguous if it generates some string ambiguously.
Can you give a formal definition of ambiguity?
Prove that the grammar G for arithmetic expressions is
ambiguous.

c
Haythem
O. Ismail

German University in Cairo, Winter 2013

Chomsky Normal Form


A CFG (V, , R, S) is in Chomsky normal form (CNF) if
every rule is in one of the following forms.
1. S
2. A BC
Where A V , and B, C V {S}
3. A a
Where A V and a
Writing grammars in CNF simplify the design of algorithms
working with CFGs.
Example: The following grammar is in CNF.
S

| AB | BA

AA | a

BB | b
c
Haythem
O. Ismail

German University in Cairo, Winter 2013

Important Result
Theorem (Sipser 2.9) Any context-free language is generated by
a context-free grammar in Chomsky normal form.

c
Haythem
O. Ismail

German University in Cairo, Winter 2013

Important Result
Theorem (Sipser 2.9) Any context-free language is generated by
a context-free grammar in Chomsky normal form.
Proof Strategy
The conversion takes place in four stages.
1. Introduce a new start variable. (Why?)
2. Eliminate all -rules: rules of the form A .
3. Eliminate all unit-rules: rules of the form A B.
4. Change the remaining rules so that their right-hand sides
have either one terminal or two variables.
The above operations should be accompanied by appropriate
patching to ensure that the resulting grammar is equivalent to
the original one.

c
Haythem
O. Ismail

German University in Cairo, Winter 2013

10

The Proof
Let G = (V, , R, S) be a CFG.
We convert G in four stages into a CFG in CNF.

c
Haythem
O. Ismail

German University in Cairo, Winter 2013

11

1. A New Start Variable


Let S0 be a new symbol; S0 6 V and S0 6 .
Let V1 = V {S0 }.
Let R1 = R {S0 S}
Let the resulting grammar be G1 = (V1 , , R1 , S0 ).

c
Haythem
O. Ismail

German University in Cairo, Winter 2013

12

2. No -Rules
Let R2 = R1
For each rule, r R2 , of the form A do
1. Let R2 = R2 {r}.
2. If B uAv R2 , (where u, v (V1 ) and A is a
proper substring of uAv), let R2 = R2 {B uv}
Note: This should be done for each occurrence of A on
the right-hand side of a rule.That is, it should be
repeated for each possible choice of u and v.
3. If B A R2 , then unless B has already been
removed, let R2 = R2 {B }.
Let the resulting grammar be G2 = (V1 , , R2 , S0 ).

c
Haythem
O. Ismail

German University in Cairo, Winter 2013

13

3. No Unit-Rules
Let R3 = R2
For each rule, r R3 , of the form A B (where B V ) do
1. Let R3 = R3 {r}
2. For every rule of the form B u R3 (where
u (V1 )+ and u 6 V ), let R3 = R3 {A u}.
3. For every rule of the form B C R3 (where C V ),
then unless A C has already been removed, let
R3 = R3 {A C}.
Let the resulting grammar be G3 = (V1 , , R3 , S0 ).

c
Haythem
O. Ismail

German University in Cairo, Winter 2013

14

4a. Proper Form


Let R4 = R3
For each rule, r R3 , of the form A u1 u2 uk
(ui V and k 3) do
1. Let R4 = R4 {r}.
2. Let V4 = V1 {A1 , A2 , . . . , Ak2 } where Ai 6 (V1 ).
3. Let R4 = R4 {A u1 A1 , A1 u2 A2 , . . . , Ak2
uk1 uk }.

c
Haythem
O. Ismail

German University in Cairo, Winter 2013

15

4b. Proper Form


For each rule, r R3 , of the form A u1 u2 where u1 or
u2 do
1. Let R4 = R4 {r}.
2. If ui , then
(a) Let V4 = V4 {Ui }, where Ui 6 (V1 ).
(b) Let R4 = R4 {Ui ui }.
(c) Let R4 = R4 {ri }, where ri is the same as r but with ui
replaced by Ui .
Let the resulting grammar be G4 = (V4 , , R4 , S0 ).

c
Haythem
O. Ismail

German University in Cairo, Winter 2013

16

Example
Convert the following grammar G into CNF.
S

ASA | aB

B|S

b|

c
Haythem
O. Ismail

German University in Cairo, Winter 2013

17

Example
Convert the following grammar G into CNF.
S

ASA | aB

B|S

b|

S0

ASA | aB

B|S

b|

G1 :

c
Haythem
O. Ismail

German University in Cairo, Winter 2013

18

Example (Contd)
G2 :
S0

ASA | aB | a

B|S|

c
Haythem
O. Ismail

German University in Cairo, Winter 2013

19

Example (Contd)
G2 :
S0

ASA | aB | a

B|S|

G2 :
S0

ASA | aB | a | SA | AS | S

B|S

c
Haythem
O. Ismail

German University in Cairo, Winter 2013

20

Example (Contd)
G3 :
S0

ASA | aB | a | SA | AS

ASA | aB | a | SA | AS

b | ASA | aB | a | SA | AS

c
Haythem
O. Ismail

German University in Cairo, Winter 2013

21

Example (Contd)
G4 :
S0

AA1 | UB | a | SA | AS

AA1 | UB | a | SA | AS

b | AA1 | UB | a | SA | AS

A1

SA

c
Haythem
O. Ismail

German University in Cairo, Winter 2013

22

Next time
Pushdown Automata.

c
Haythem
O. Ismail

German University in Cairo, Winter 2013

23

Points to take home


Leftmost derivations.
Ambiguity.
Chmosky normal form.
From CFG to CFG in Chomsky normal form.

c
Haythem
O. Ismail

Potrebbero piacerti anche