Lec 8 To C
Lec 8 To C
FCI-Minia University 1
Course Contents
I. PART 1
Introduction to Automata and Languages
II. PART 2
Regular Expressions and Languages
Properties of Regular Languages
Context-Free Grammars and Languages
Applications of Context-Free Grammars
FCI-Minia University 2
Aim of The Lecture
CFG Simplification
Chomsky Normal Form
Greibach Normal Form
FCI-Minia University 3
CFG Simplification
The definition of context free grammars (CFGs) allows us to develop a wide variety of
grammars.
Most of the time, some of the productions of CFGs are not useful and are redundant. This
happens because the definition of CFGs does not restrict us from making these redundant
productions.
By simplifying CFGs we remove all these redundant productions (null productions and unit
productions) from a grammar , while keeping the transformed grammar equivalent to the
original grammar.
Two grammars are called equivalent if they produce the same language.
Simplifying CFGs is necessary to later convert them into Normal forms.
The productions that can never take part in derivation of any string , are
called useless productions.
Similarly , a variable that can never take part in derivation of any string is
called a useless variable.
5
Reduction of CFG: Procedure
Phase 2 − Derivation of an equivalent grammar, G”, from the CFG, G’, such that each symbol appears in a
sentential form.
Derivation Procedure −
Step 1 − Include the start symbol in Y1 and initialize i = 1.
Step 2 − Include all symbols, Yi+1, that can be derived from Yi and include all production rules that have been
applied.
Step 3 − Increment i and repeat Step 2, until Yi+1 = Yi.
FCI-Minia University 6
Reduction of CFG: Problem#1
Phase 1 −
T = { a, b, c, d }
W1 = { S, A } from rules S -> abS | abA | abB, A -> cd
W2 = {S, A } U ∅
Since W1 = W2, we can derive G’ as −
G’ = { { S, A }, {a, b, c, d }, P, {S}}
where P: S -> abS | abA , A -> cd
FCI-Minia University 9
Reduction of CFG: Problem#2
Problem#2 Find a reduced grammar equivalent to the grammar G, having
production rules, S -> abS | abA | abB, A -> cd, B -> aB, C -> dc
G’: S -> abS | abA , A -> cd
Phase 2 −
Y1 = { S }
Y2 = { S, A, a, b } from rule S -> abS | abA
Y3 = { S, A, a, b, c, d} from rules A -> cd
Y4 = { S, A, a, b, c, d}
Since Y3 = Y4, we can derive G” as −
G” = { { S, A}, { a, b, c, d }, P, {S}}
where P: S -> abS | abA , A -> cd
FCI-Minia University 10
CFG Simplification:
Removal of Unit Productions
The productions of type ‘A -> B’ are called unit productions.
Removal Procedure −
Step 3 − Repeat from step 1 until all unit productions are removed.
FCI-Minia University 11
Removal of Unit Productions:
Problem#1
Problem#1 Remove unit production from the following −
S → XY, X → a, Y → Z | b, Z → M, M → N, N → a
Solution
3
There are unit productions in the grammar −
Y → Z, Z → M, and M → N
FCI-Minia University 12
Removal of Unit Productions:
Problem#1
Now we will remove Z → M. S → XY, X → a, Y → Z | b, Z → M, M → a, N → a
As M → a, we add Z→ a, and Z → M is removed.
The production set becomes
S → XY, X → a, Y → Z | b, Z → a, M → a, N → a
FCI-Minia University 14
Removal of Unit Productions:
Problem#2
S -> Aa | B, A -> b | a, B -> A | a
Now we will remove B → A.
As A → b, we add B→ b, and B → A is removed.
The production set becomes
S -> Aa | B, A -> b | a, B -> b | a
Removal Procedure
Step 1 − Find out nullable non-terminal variables which derive ε.
Step 3 − Combine the original productions with the result of step 2 and remove ε -
productions.
FCI-Minia University 16
Removal of Null Productions:
Problem#1
Problem#1 Remove null production from the following −
S → ASA | aB | b, A → B, B → b | ∈
Solution −
There are two nullable variables − A and B
At first, we will remove B → ε.
After removing B → ε, the production set becomes −
S→ASA | aB | b | a, A → B|b|∈, B → b
18
Removal of Null Productions:
Problem#2
Problem#2 Remove null production from the following −
S -> ABCd A -> BC B -> bB | ∈ C -> cC | ∈
Solution −
At first, we will remove C → ε.
S -> ABCd|ABd A -> BC|B B -> bB |∈ C -> cC | c
FCI-Minia University 20
Aim of The Lecture
CFG Simplification
Chomsky Normal Form
Greibach Normal Form
FCI-Minia University 21
Chomsky Normal Form
A CFG is in Chomsky Normal Form if the Productions are in the following forms:
A→a
A → BC
S→ϵ
Where A, B, and C are non-terminals and a is terminal.
Note:
A non-terminal generating a terminal (e.g.; X->x)
A non-terminal generating two non-terminals (e.g.; X->YZ)
Start symbol generating ε. (e.g.; S-> ε)
FCI-Minia University 22
Chomsky Normal Form
Note
FCI-Minia University 23
Chomsky Normal Form:
Procedure
Step 1 − If the start symbol S occurs on some right side, create a new start symbol S’ and a
new production S’→ S.
Step 2 − Remove Null productions. (Using the Null production removal Procedure)
Step 3 − Remove unit productions. (Using the Unit production removal Procedure)
Step 4 − If the right side of any production is in the form A → aB where a is a terminal
and A, B are non-terminal, then the production is replaced by A → XB and X → a.
Repeat this step for every production which is in the form A → aB.
FCI-Minia University 27
Chomsky Normal Form: Problem#1
S0→ ASA | YB | a | AS | SA
S→ ASA | YB | a | AS | SA
(5) Now we will find out more than two variables in the R.H.S A → b |ASA | YB | a | AS | SA
Here, S0→ ASA, S → ASA, A→ ASA violates two Non-terminals in B→b
R.H.S. Y→a
Hence we will apply step 4 and step 5 to get the following final
production set which is in CNF −
S0→ AX | YB | a | AS | SA
S→ AX | YB | a | AS | SA
A → b |AX | YB | a | AS | SA
B→b
Y →a
X → SA
28
Chomsky Normal Form: Problem#2
(1) Since S appears in R.H.S, we add a new state S0 and S0→S is added to the
production set and it becomes −
S0→S, S → ASB A → aAS |a |ε B → SbS |A |bb
(4) We have to change the productions A->aAS |aS and B-> SbS|aAS|aS, terminals a and b
exist on RHS with non-terminates. Removing them from RHS:
S0→ ASB| SB |AS,
S0-> AS|ASB| SB
S → ASB| SB |AS
S → AS|ASB| SB A → aAS |a |aS
A → XAS|XS|a B → SbS | aAS |a |aS |bb
B → SYS|bb|XAS|XS|a
X →a
Y→b
Also, B->bb can’t be part of CNF, removing it from grammar yields:
S0-> AS|ASB| SB
S → AS|ASB| SB
A → XAS|XS|a
B → SYS|YY|XAS|XS|a
X →a FCI-Minia University 31
Chomsky Normal Form: Problem#2
(4) Now we will find out more than two variables in the R.H.S S0-> AS|ASB| SB
S → AS|ASB| SB
Here, S0→ ASB, S → ASB, A→ XAS, B → SYS,B → XAS violates two A → XAS|XS|a
Non-terminals in R.H.S. B → SYS|YY|XAS|XS|a
X →a
Y→b
S0-> AS |VB| SB
S → AS |VB |SB
A → XV |XS |a
B → SP| YY |XV |XS |a
X →a
Y→ b
V →AS
P →YS
FCI-Minia University 32
Aim of The Lecture
CFG Simplification
Chomsky Normal Form
Greibach Normal Form
FCI-Minia University 33
Greibach Normal Form
A CFG is in Greibach Normal Form if the Productions are in the following forms −
A→b
A → bD1…Dn
S→ε
where A, D1,....,Dn are non-terminals and b is a terminal.
Note:
A non-terminal generating a terminal (e.g.; X->x)
A non-terminal generating a terminal followed by any number of non-terminals
(e.g.; X->xX1X2…Xn)
Start symbol generating ε. (e.g.; S-> ε)
FCI-Minia University 34
Greibach Normal Form
Note –
FCI-Minia University 35
Greibach Normal Form: Procedure
Step 1 − If the start symbol S occurs on some right side, create a new start symbol S’ and
a new production S’ → S.
Step 2 − Remove Null productions.
Step 3 − Remove unit productions.
Step 4- Check whether the CFG is already in CNF, and convert it to CNF if it is not.
Step 5- Change the names of non-terminal symbols into some Ai in ascending order of i.
Step 6- Alter the rules, so that the non-terminals are in ascending order, such that,
If the production is of the form Ai →Ajx then i<j and should never be i>=j
36
Greibach Normal Form:Problem#1
Convert the following CFG into GNF
S → CA|BB B → b|SB C → b A → a
Solution
1) CFG is in CNF.
2) Change the names of non-terminal symbols into some Ai in ascending order of i.
Replace: S with A1
C with A2
A with A3
B with A4
So the production set become:
A1 → A2A3|A4A4
A4 → b|A1A4
A2→ b
A3 → a
FCI-Minia University 37
Greibach Normal Form:Problem#1
3) Alter the rules, so that the non-terminals are in ascending order, A1 → A2A3|A4A4
such that, A4 → b|A1A4
If the production is of the form Ai →Ajx then i<j and should never A2→ b
A3 → a
be i>=j
A4 → b|A1A4
So the final production set become after replace S with A1 C with A2 A with A3 B with A4:
S → bA| bB| bABB| bZB| bABZB
B → b| bAB| bZ| bABZ
Z → bBZ| bABBZ| bZBZ|bABZBZ | bB| bABB| bZB|bABZB
C→ b
A→ a
42
Greibach Normal Form:Problem#2
Replace X → mX | m
So the production set become:
S → mXY | mY| mXC| mC | p
X → mX | m
Y → mXC| mC | o
C→n
FCI-Minia University 44
Assignments
1. Convert the following CFG into CNF
E → E+E
E → E*E
E → (E)
E→7
The terminals are + * ( ) 7
FCI-Minia University 46