discrete math. and logic ii. normal forms - sfwr eng 2fa3se2fa3/notes/ln19new.pdf · 2014-03-27 ·...
TRANSCRIPT
Discrete Math. and Logic II. Normal FormsSFWR ENG 2FA3
Ryszard Janicki
Winter 2014
Acknowledgments: Material partially based on Automata and Computability by Dexter C. Kozen (Chapter 21).
Ryszard Janicki Discrete Math. and Logic II. Normal Forms 1 / 12
Normal Forms
Very often, it is helpful to assume that CFGs are in one oranother special restricted form
Two of the most useful such forms are
Chomsky normal form (CNF)
Greibach normal form (GNF)
De�nition
A CFG is in Chomsky normal form (CNF) if all productions are ofthe 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
De�nition
A CFG is in Greibach normal form (GNF) if all productions are ofthe form
A −→ aB1B2 · · ·Bk
for some k ≥ 0, where A,B1, · · · ,Bk ∈ N and a ∈ Σ.
Note that k = 0 is allowed, giving productions of the formA −→ a
No grammar in Chomsky or Greibach form can generate thenull string (No production can generate ε which does notbelong 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 | SS , C −→ SB,
A −→ [, B −→]
Greibach normal form
S −→ [B | [SB | [BS | [SBS , B −→]
Ryszard Janicki Discrete Math. and Logic II. Normal Forms 4 / 16
Normal Forms
Theorem
For any CFG G, there is a CFG G ′ in Chomsky normal form and a
CFG G” in Greibach normal form such that
L(G”) = L(G ′) = L(G )− {ε}.
To prove this theorem, we must �rst show how to get rid ofall ε-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 whetherapplying a production makes any progress toward deriving astring 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 thelanguage)
Ryszard Janicki Discrete Math. and Logic II. Normal Forms 6 / 12
Getting Rid of ε- and Unit Productions
Lemma
For any CFG G = (N,Σ,P,S), there is a CFG G ′ with no ε- orunit productions such that L(G ′) = L(G )− {ε}.
Proof Idea.
Use the following rules to transform the productions of G to getrid of ε- and unit productions
If A −→ αBβ and B −→ ε is in the set of production, thenwe add A −→ αβ
If A −→ B and B −→ γ is in the set of production, then weadd A −→ γ
At the end we remove ε- and unit productions and thegrammer 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 asimple matter to put the resulting grammar into Chomskynormal form
For each terminal a ∈ Σ, introduce a new nonterminal Aa andproduction Aa −→ a
After that, replace all occurrences of a on the right-hand sidesof old productions (except productions of the form B −→ a)with Aa
Then all productions are of the form
A −→ a or A −→ B1B2 · · ·Bk , k ≥ 2
where Bi are nonterminals.
Ryszard Janicki Discrete Math. and Logic II. Normal Forms 8 / 12
Chomsky Normal Form (CNF)
For any production
A −→ B1B2 · · ·Bk
with k ≥ 3, introduce a new nonterminal C and replace thisproduction with the two productions
A −→ B1C and C −→ B2B3 · · ·B3
Keep doing this until all right-hand sides are of length at most2
Ryszard Janicki Discrete Math. and Logic II. Normal Forms 9 / 12
Chomsky Normal Form (CNF)
Example
Derive a CNF grammar for the set {anbn | n ≥ 1}. We start withthe grammar
S −→ aSb | ε
Transformation:
1 Remove ε- and unit productions
S −→ aSb | ab
2 Adding nonterminal A,B and replace these productions with
S −→ ASB | AB, A −→ a, B −→ b
Ryszard Janicki Discrete Math. and Logic II. Normal Forms 10 / 12
Chomsky Normal Form (CNF)
Example (Continued)
1 Finally, we add a nonterminal C and replace S −→ ASB with
S −→ AC C −→ SB
2 The �nal grammar (Under Chomsky Normal Form) isS −→ AB | AC , C −→ SB, A −→ a, B −→ b
Example (Home Work)
Derive a CNF grammar for the set of nonnull strings of balancedparentheses [ ]. 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 notgenerate ε) into appropriate Graibach Normal Form is rathercomplicated (Carefully read pages 144�147 from textbook).
The construction is rather easy if for any production X → Ax ,where x ∈ Σ∗, the productions starting from A have aterminal as the �rst symbol, i.e. A→ x implies x = ay wherea ∈ 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 anyproduction of the form A→ ax can easily be transformed intothe set of equivalent productions in Graibach Normal Form.
For example A→ aBbCa can be transformed into
A→ XaBXbCXa
Xa → a
Xa → a
where Xa,Xb are new variables/nonterminals.
If we have X → Ax and, for example, A→ ay | bz | ct, wecan replace X → Ax withX → ayx | bzx | ctx
Ryszard Janicki Discrete Math. and Logic II. Normal Forms 13 / 16
Greibach Normal Form (GNF)
Example
S → ABb | BaA→ aAa | CBB → bAb | ABB | aC → aCb |c
C → aCb is replaced with C → aCXb | c and Xb → b.
A→ aAa is replaced with A→ aAXa | c and Xa → a.
A→ CB is replaced with A→ aCXbB | cBNow all productions starting with A,C ,Xa and Xb are inGraibach Normal Form as:A→ aAXa | aCXbB | cB, andC → aCXb | cXb → b
Xa → a
Ryszard Janicki Discrete Math. and Logic II. Normal Forms 14 / 16
Example (continued)
B → bAb is replaced with B → bAXb (we already haveXb → b)
B → ABB is replaced with B → aAaBB | aCXbBBB | cBBBNow all productions starting with A,B,Xa and Xb are inGraibach Normal Form as:A→ aAXa | aCXbB | cB,B → bAXb | aAaBB | aCXbBBB | cBBB | aC → aCXb | cXb → b
Xa → a
The only productions that are not in Graibach Normal Formare S → ABb | BaS → BA is replaced withS → bAXbXa | aAaBBXa | aCXbBBBXa | cBBBXa | aXa
S → ABb is replaced withS → aAXaBXb | aCXbBBXb | 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
S → ABb | BaA→ aAa | CBB → bAb | ABB | aC → aCb |c
mS → aAXaBXb | aCXbBBXb | cBBXb
S → bAXbXa | aAaBBXa | aCXbBBBXa | cBBBXa | aXa
A→ aAXa | aCXbB | cB,B → bAXb | aAaBB | aCXbBBB | cBBB | aC → aCXb | cXb → b
Xa → a
Ryszard Janicki Discrete Math. and Logic II. Normal Forms 16 / 16