Discrete Math. and Logic II. Normal Forms

Discrete Math. and Logic II. Normal Forms
SFWR ENG 2FA3
Ryszard Janicki
Winter 2014
Acknowledgments:
Material
partially
based on
Automata and Computability
Ryszard Janicki
by Dexter C. Kozen (Chapter 21).
Discrete Math. and Logic II. Normal Forms
1 / 12
Normal Forms
Very often, it is helpful to assume that CFGs are in one or
another special restricted form
Two of the most useful such forms are
Chomsky normal form (CNF)
Greibach normal form (GNF)
Denition
A CFG is in Chomsky normal form (CNF) if all productions are of
the form
A −→ BC or A −→ a
where
, ,
A B C
∈ N and
a
∈ Σ.
Ryszard Janicki
Discrete Math. and Logic II. Normal Forms
2 / 12
Normal Forms
Denition
A CFG is in Greibach normal form (GNF) if all productions are of
the form
A −→ aB1 B2 · · · Bk
for some
k
≥ 0, where
Note that
A −→ a
k
,
A B1
, · · · , Bk ∈ N and
a
∈ Σ.
= 0 is allowed, giving productions of the form
No grammar in Chomsky or Greibach form can generate the
null string (No production can generate which does not
belong to Σ)
Ryszard Janicki
Discrete Math. and Logic II. Normal Forms
3 / 12
Normal Forms
Example
Grammar for the set of nonnull strings of balanced parentheses
Most obvious grammar:
S
−→ [S ] |
SS
| Chomsky normal form
S
−→ AB |
|
AC
A
SS
,
−→ [,
C
B
−→ SB ,
−→]
Greibach normal form
S
−→ [B | [SB | [BS | [SBS ,
Ryszard Janicki
B
−→]
Discrete Math. and Logic II. Normal Forms
4 / 16
Normal Forms
Theorem
For any CFG G , there is a CFG G
CFG G
”
0
in Chomsky normal form and a
in Greibach normal form such that
( ”) = L(G 0 ) = L(G ) − {}.
L G
To prove this theorem, we must rst show how to get rid of
all -productions A −→ and unit productions A −→ B
Ryszard Janicki
Discrete Math. and Logic II. Normal Forms
5 / 12
Normal Forms
These productions make it hard to determine whether
applying a production makes any progress toward deriving a
string of terminals
One might apply unit production n times, then use
-production n times to erase them =⇒ waste time!!!!
There might be useless productions!!
We cannot simply throw out the - and unit productions,
because they may be needed to generate some strings in L(G )
We simply to replace them (with loosing only from the
language)
Ryszard Janicki
Discrete Math. and Logic II. Normal Forms
6 / 12
Getting Rid of - and Unit Productions
Lemma
= (N , Σ, P , S ), there is a CFG G 0
0
productions such that L(G ) = L(G ) − {}.
For any CFG G
unit
with no
Proof Idea.
Use the following rules to transform the productions of
rid of - and unit productions
If A −→ αB β and
we add A −→ αβ
If A −→ B and
add A −→ γ
B
B
G
-
or
to get
−→ is in the set of production, then
−→ γ is in the set of production, then we
At the end we remove - and unit productions and the
grammer obtained generates L(G ) − {}
Ryszard Janicki
Discrete Math. and Logic II. Normal Forms
7 / 12
Chomsky Normal Form (CNF)
Once we are rid of useless, - and unit productions, it is a
simple matter to put the resulting grammar into Chomsky
normal form
For each terminal a ∈ Σ, introduce a new nonterminal
production Aa −→ a
Aa
and
After that, replace all occurrences of a on the right-hand sides
of old productions (except productions of the form B −→ a)
with Aa
Then all productions are of the form
A
where
Bi
−→ a or
A
−→ B1 B2 · · · Bk , k ≥ 2
are nonterminals.
Ryszard Janicki
Discrete Math. and Logic II. Normal Forms
8 / 12
Chomsky Normal Form (CNF)
For any production
A
−→ B1 B2 · · · Bk
with k ≥ 3, introduce a new nonterminal
production with the two productions
A
−→ B1 C and
C
C
and replace this
−→ B2 B3 · · · B3
Keep doing this until all right-hand sides are of length at most
2
Ryszard Janicki
Discrete Math. and Logic II. Normal Forms
9 / 12
Chomsky Normal Form (CNF)
Example
Derive a CNF grammar for the set {an b n |
the grammar
S −→ aSb | n
≥ 1}. We start with
Transformation:
1
Remove - and unit productions
S
2
−→ aSb |
ab
Adding nonterminal A, B and replace these productions with
S
−→ ASB |
AB
,
Ryszard Janicki
A
−→ a,
B
−→ b
Discrete Math. and Logic II. Normal Forms
10 / 12
Chomsky Normal Form (CNF)
Example (Continued)
1
Finally, we add a nonterminal
S
2
−→ AC
C
and replace
C
S
−→ ASB with
−→ SB
The nal grammar (Under Chomsky Normal Form) is
S −→ AB | AC ,
C −→ SB ,
A −→ a,
B −→ b
Example (Home Work)
Derive a CNF grammar for the set of nonnull strings of balanced
parentheses [ ]. Start with the grammar S −→ [S ] | SS | Ryszard Janicki
Discrete Math. and Logic II. Normal Forms
11 / 12
Greibach Normal Form (GNF)
General transformation of any grammar (that does not
generate ε) into appropriate Graibach Normal Form is rather
complicated (Carefully read pages 144147 from textbook).
The construction is rather easy if for any production X → Ax ,
where x ∈ Σ∗ , the productions starting from A have a
terminal as the rst symbol, i.e. A → x implies x = ay where
∗
a ∈ Sigma, y ∈ Σ .
Ryszard Janicki
Discrete Math. and Logic II. Normal Forms
12 / 16
Greibach Normal Form (GNF)
The construction is based on the observation that any
production of the form A → ax can easily be transformed into
the set of equivalent productions in Graibach Normal Form.
For example
A
→ aBbCa can be transformed into
→ Xa BXb CXa
Xa → a
Xa → a
A
where
,
Xa Xb
are new variables/nonterminals.
If we have X → Ax and, for example,
can replace X → Ax with
X → ayx | bzx | ctx
Ryszard Janicki
A
→ ay | bz | ct , we
Discrete Math. and Logic II. Normal Forms
13 / 16
Greibach Normal Form (GNF)
Example
→ ABb | Ba
→ aAa | CB
B → bAb | ABB | a
C → aCb |c
S
A
C
→ aCb is replaced with
C
A
→ aAa is replaced with
A
A
→ CB is replaced with
A
→ aCXb | c and
→ aAXa | c and
Xb
Xa
→ a.
→ aCXb B | cB
Now all productions starting with A, C , Xa and
Graibach Normal Form as:
A → aAXa | aCXb B | cB , and
C → aCXb | c
Xb → b
Xa → a
Ryszard Janicki
→ b.
Xb
are in
Discrete Math. and Logic II. Normal Forms
14 / 16
Example (continued)
B
→ bAb is replaced with
→ b)
B
→ bAXb (we already have
Xb
B
→ ABB is replaced with
B
→ aAaBB | aCXb BBB | cBBB
Now all productions starting with A, B , Xa and
Graibach Normal Form as:
A → aAXa | aCXb B | cB ,
B → bAXb | aAaBB | aCXb BBB | cBBB | a
C → aCXb | c
Xb → b
Xa → a
Xb
are in
The only productions that are not in Graibach Normal Form
are S → ABb | Ba
S
S
S
S
→ BA is replaced with
→ bAXb Xa | aAaBBXa | aCXb BBBXa | cBBBXa | aXa
→ ABb is replaced with
→ aAXa BXb | aCXb BBXb | cBBXb
Ryszard Janicki
Discrete Math. and Logic II. Normal Forms
15 / 16
Greibach Normal Form (GNF)
Example (continued)
Now all new productions are in Graibach Normal Form so
→ ABb | Ba
→ aAa | CB
B → bAb | ABB | a
C → aCb |c
m
S → aAXa BXb | aCXb BBXb | cBBXb
S → bAXb Xa | aAaBBXa | aCXb BBBXa | cBBBXa | aXa
A → aAXa | aCXb B | cB ,
B → bAXb | aAaBB | aCXb BBB | cBBB | a
C → aCXb | c
Xb → b
Xa → a
S
A
Ryszard Janicki
Discrete Math. and Logic II. Normal Forms
16 / 16