Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
c
Haythem
O. Ismail
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
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) | a
G is ambiguous.
Draw two different parse trees for the string a + a a
c
Haythem
O. Ismail
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
hverbi
hprepi
with
c
Haythem
O. Ismail
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
| AB | BA
AA | a
BB | b
c
Haythem
O. Ismail
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
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
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
11
c
Haythem
O. Ismail
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
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
14
c
Haythem
O. Ismail
15
c
Haythem
O. Ismail
16
Example
Convert the following grammar G into CNF.
S
ASA | aB
B|S
b|
c
Haythem
O. Ismail
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
18
Example (Contd)
G2 :
S0
ASA | aB | a
B|S|
c
Haythem
O. Ismail
19
Example (Contd)
G2 :
S0
ASA | aB | a
B|S|
G2 :
S0
ASA | aB | a | SA | AS | S
B|S
c
Haythem
O. Ismail
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
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
22
Next time
Pushdown Automata.
c
Haythem
O. Ismail
23
c
Haythem
O. Ismail