0% found this document useful (0 votes)
20 views46 pages

Lec 8 To C

The document outlines the course content for a Theory of Computation class, focusing on context-free grammars (CFGs) and their simplification methods. It details processes for reducing CFGs, removing unit and null productions, and converting CFGs into Chomsky Normal Form. The lecture aims to provide students with essential techniques for grammar simplification and understanding the structure of formal languages.

Uploaded by

tavako7168
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views46 pages

Lec 8 To C

The document outlines the course content for a Theory of Computation class, focusing on context-free grammars (CFGs) and their simplification methods. It details processes for reducing CFGs, removing unit and null productions, and converting CFGs into Chomsky Normal Form. The lecture aims to provide students with essential techniques for grammar simplification and understanding the structure of formal languages.

Uploaded by

tavako7168
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

Theory of Computation

Fourth Year Students


Department of Computer Science
T.A. Rehab Emad El-Dein Sayed

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.

 Simplification consists of the following steps −


1. Reduction of CFG
2. Removal of Unit Productions
3. Removal of Null Productions
FCI-Minia University 4
CFG Simplification:
Reduction of CFG
1. Useless productions –

 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

CFGs are reduced in two phases −


 Phase 1 − Derivation of an equivalent grammar, G’, from the CFG, G, such that each variable derives some
terminal string.
Derivation Procedure −
Step 1 − Include all symbols, W1, that derive some terminal and initialize i=1.
Step 2 − Include all symbols, Wi+1, that derive Wi.
Step 3 − Increment i and repeat Step 2, until Wi+1 = Wi.
Step 4 − Include all production rules that have Wi in it.

 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

Problem#1 Find a reduced grammar equivalent to the grammar G, having


production rules, P: S → AC | B,
A → a, C → c | BC,
E → aA | e
Phase 1 −
T = { a, c, e }
W1 = { A, C, E } from rules A → a, C → c and E → aA
W2 = { A, C, E } U { S } from rule S → AC
W3 = { A, C, E, S } U ∅
Since W2 = W3, we can derive G’ as −
G’ = { { A, C, E, S }, { a, c, e }, P, {S}}
where P: S → AC, A → a, C → c FCI-Minia, E →University
aA | e 7
Reduction of CFG: Problem#1

Problem#1 Find a reduced grammar equivalent to the grammar G, having


production rules, P: S → AC | B, A → a, C → c | BC, E → aA | e
G’: S → AC, A → a, C → c , E → aA | e
Phase 2 −
Y1 = { S }
Y2 = { S, A, C } from rule S → AC
Y3 = { S, A, C, a, c } from rules A → a and C → c
Y4 = { S, A, C, a, c }
Since Y3 = Y4, we can derive G” as −
G” = { { A, C, S }, { a, c }, P, {S}}
where P: S → AC, A → a, C → c
FCI-Minia University 8
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
Solution

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 1 − To remove A → B, add production A → x to the grammar rule


whenever B → x occurs in the grammar. [x ∈ Terminal, x can be Null]

Step 2 − Delete A → B from the grammar.

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

 At first, we will remove M → N:

As N → a, we add M → a, and M → N is removed.


The production set becomes
S → XY, X → a, Y → Z | b, Z → M, M → a, N → a

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

 Now we will remove Y → Z.


As Z → a, we add Y→ a, and Y → Z is removed.
The production set becomes
S → XY, X → a, Y → a | b, Z → a, M → a, N → a

Now Z, M, and N are unreachable, hence we can remove those.


The final CFG is unit production free −
S → XY, X → a, Y → a | b
FCI-Minia University 13
Removal of Unit Productions:
Problem#2
Problem#2 Remove unit production from the following −
S -> Aa | B, A -> b | B, B -> A | a
Solution
3
There are unit productions in the grammar −
S → B, A → B, and B → A

 At first, we will remove A→ B:

As B → a, we add A → a, and A→ B is removed.


The production set becomes
S -> Aa | B, A -> b | a, B -> A | 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

 Now we will remove S → B.


As B → a|b, we add S→ a|b, and S → B is removed.
The production set becomes
S -> Aa | a|b, A -> b | a, B -> b | a
The final CFG is unit production free −
S -> Aa | a| b,
A -> b | a,
FCI-Minia University 15
B -> b | a
CFG Simplification:
Removal of Null Productions
 In a CFG, a non-terminal symbol ‘A’ is a nullable variable if there is a
production A → ε or there is a derivation that starts at A and finally ends up with
ε: A → .......… → ε

 Removal Procedure
Step 1 − Find out nullable non-terminal variables which derive ε.

Step 2 − For each production A → a, construct all productions A → x where x is


obtained from ‘a’ by removing one or multiple non-terminals from Step 1.

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

 Now we will remove A → ε.


After removing A → ε, the production set becomes −
S→ASA | aB | b | a | SA | AS | S, A → B| b, B → b

This is the final production set without null transition. 17


Removal of Null Productions:
Problem#2
Problem#2 Remove null production from the following −
S -> ABCd A -> BC B -> bB | ∈ C -> cC | ∈
Solution −

 Lets first find all the nullable variables.


 Variables ‘B’ and ‘C’ are clearly nullable because they contain ‘λ’ on the RHS
of their production.
 Variable ‘A’ is also nullable because both variables (B, C) on the RHS are also
nullable.
 Similarly , variable ‘S’ is also nullable.
 So variables ‘S’ , ‘A’ , ‘B’ and ‘C’ are nullable variables.

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

 Now we will remove B → ε.


S -> ABCd|ABd|ACd|Ad A -> BC|B|C|∈ B -> bB | b C -> cC | c

 Now we will remove A → ε.


S -> ABCd|ABd|ACd|Ad|BCd|Bd|Cd|d A -> BC|B|C B -> bB | b C -> cC | c

This is the final production set without null transition. 19


CFG Simplification: Note

 To remove all kinds of productions mentioned above,

1. First, remove the null productions.


2. Second, remove the unit productions.
3. Finally , remove the useless productions.

Following this order is very important to get the correct result.

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

 For a given grammar, there can be more than one CNF.


 CNF produces the same language as generated by CFG.
 CNF is used as a preprocessing step for many algorithms for CFG like bottom-up parsers
etc.
 For generating string w of length ‘n’ requires ‘2n-1’ production or steps in CNF.
 Any Context free Grammar that do not have ε in it’s language has an equivalent CNF.

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.

 Step 5 − Replace each production A → B1…Bn where n > 2 with A → B1C


where C → B2 …Bn. Repeat this step for all productions having two or more symbols in the
right side. 24
Chomsky Normal Form: Problem #1
Convert the following CFG into CNF
S → ASA | aB, A → B | S, B→b|ε
Solution
(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→ ASA | aB, A → B | S, B → b | ∈
(2) Now we will remove the null productions −
B → ∈ and A → ∈
After removing B → ε, the production set becomes −
S0→S, S→ ASA | aB | a, A → B | S | ∈, B→b
After removing A → ∈, the production set becomes −
S0→S, S→ ASA | aB | a | AS | SA | S, A → B | S, B→b 25
Chomsky Normal Form: Problem#1
(3) Now we will remove the unit productions. S0→S,
After removing S → S, the production set becomes − S→ ASA | aB | a | AS | SA | S,
S0→S, A → B | S,
S→ ASA | aB | a | AS | SA,
A → B | S,
B→b
B→b
After removing S0→ S, the production set becomes −
S0→ ASA | aB | a | AS | SA,
S→ ASA | aB | a | AS | SA
A → B | S, B → b
After removing A→ B, the production set becomes −
S0 → ASA | aB | a | AS | SA, S→ ASA | aB | a | AS | SA
A→S|b
B→b
After removing A→ S, the production set becomes −
S0 → ASA | aB | a | AS | SA,
S→ ASA | aB | a | AS | SA
A → b |ASA | aB | a | AS | SA, FCI-Minia University 26
B→b
Chomsky Normal Form: Problem#1
S0 → ASA | aB | a | AS | SA,
(4) We have to change the productions S0→ aB, S→ aB, A→ aB S→ ASA | aB | a | AS | SA
And the final production set becomes − A → b |ASA | aB | a | AS | SA,
B→b
S0→ ASA | YB | a | AS | SA
S→ ASA | YB | a | AS | SA
A → b |ASA | YB | a | AS | SA
B→b
Y→a

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

Convert the following CFG into CNF


S → ASB A → aAS |a |ε B → SbS |A |bb
Solution

(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

(2) Now we will remove the null productions − A → ∈ and B → ∈

After removing A → ε, the production set becomes −


S0→S, S → ASB| SB A → aAS |a |aS B → SbS |A |bb|∈
After removing B → ε, the production set becomes −
S0→S, S → ASB| SB |AS|S A → aAS |a |aS B → SbS |A |bb 29
Chomsky Normal Form: Problem#2
(3) Now we will remove the unit productions. S0→S,
After removing S → S, the production set becomes − S → ASB| SB |AS|S
S0→S, A → aAS |a |aS
S → ASB| SB |AS B → SbS |A |bb
A → aAS |a |aS
B → SbS |A |bb
After removing S0 → S, the production set becomes −
S0→ ASB| SB |AS,
S → ASB| SB |AS
A → aAS |a |aS
B → SbS |A |bb
After removing B → A, the production set becomes −
S0→ ASB| SB |AS,
S → ASB| SB |AS
A → aAS |a |aS
B → SbS | aAS |a |aS |bb FCI-Minia University 30
Chomsky Normal Form: Problem#2

(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 –

 For a given grammar, there can be more than one GNF


 GNF produces the same language as generated by CFG

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

 Step 7 − Remove all direct and indirect left-recursion.


 Step 8 − Do proper substitutions of productions to convert it into the proper form of
GNF.

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

Replace A1 with its value A1 → A2A3 |A4A4


A4 → b| A2A3A4| A4A4A4

So the production set become:


A1 → A2A3|A4A4
A4 → b| A2A3A4| A4A4A4
A2→ b
A3 → a
38
Greibach Normal Form:Problem#1
A1 → A2A3|A4A4
4) Remove all direct and indirect left-recursion. A4 → b| A2A3A4| A4A4A4
A4 → b| A2A3A4| A4A4A4 A2→ b
Introduce a new variable to remove the left recursion A3 → a
Z → A4A4Z| A4A4
So, remove A4 → A4A4A4
A4 → b| A2A3A4| bZ|A2A3A4Z

So the production set become:


A1 → A2A3|A4A4
A4 → b| A2A3A4| bZ|A2A3A4Z
Z → A4A4Z| A4A4
A2→ b
A3 → a
FCI-Minia University 39
Greibach Normal Form:Problem#1
A1 → A2A3|A4A4
5) Do proper substitutions of productions to convert it into the A4 → b| A2A3A4| bZ|A2A3A4Z
proper form of GNF, such as Z → A4A4Z| A4A4
A1 → A2A3|A4A4 A2→ b
A4 → b| A2A3A4| bZ|A2A3A4Z A3 → a
Z → A4A4Z| A4A4
Must begin with terminal symbol, (e.g.; X->xX1X2…Xn)
Replace A2→ b

So the production set become:


A1 → bA3|A4A4
A4 → b| bA3A4| bZ|bA3A4Z
Z → A4A4Z| A4A4
A2→ b
A3 → a FCI-Minia University 40
Greibach Normal Form:Problem#1
Replace A4 → b| bA3A4| bZ|bA3A4Z A1 → bA3|A4A4
So the production set become: A4 → b| bA3A4| bZ|bA3A4Z
A1 → bA3|bA4| bA3A4A4| bZA4|bA3A4ZA4 Z → A4A4Z| A4A4
A4 → b| bA3A4| bZ|bA3A4Z A2→ b
Z → A4A4Z| A4A4 A3 → a
A2→ b
A3 → a

So the production set become:


A1 → bA3|bA4| bA3A4A4| bZA4|bA3A4ZA4
A4 → b| bA3A4| bZ|bA3A4Z
Z → bA4Z| bA3A4A4Z| bZA4Z|bA3A4ZA4Z | bA4| bA3A4A4| bZA4|bA3A4ZA4
A2→ b
A3 → a 41
Greibach Normal Form:Problem#1

So the production set become:


A1 → bA3|bA4| bA3A4A4| bZA4|bA3A4ZA4
A4 → b| bA3A4| bZ|bA3A4Z
Z → bA4Z| bA3A4A4Z| bZA4Z|bA3A4ZA4Z | bA4| bA3A4A4| bZA4|bA3A4ZA4
A2→ b
A3 → a

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

Convert the following CFG into GNF


S → XY | Xn | p X → mX | m Y → Xn | o
Solution
5) Do proper substitutions of productions to convert it into the proper form of GNF,
such as:
S → XY | Xn | p
Y → Xn | o
Must begin with terminal symbol, (e.g.; X->xX1X2…Xn)
Replace X → mX | m
So the production set become:
S → mXY | mY| Xn | p
X → mX | m
Y → Xn | o
43
Greibach Normal Form:Problem#2
add C → n S → mXY | mY| Xn | p
So the production set become: X → mX | m
S → mXY | mY| XC | p Y → Xn | o
X → mX | m
Y → XC | o
C→n

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

2. Convert the following CFG into GNF


S→A
A→aBa|a
B → bAb | b
45
Thanks…
Next:
 Pushdown Automata

FCI-Minia University 46

You might also like