SlideShare uma empresa Scribd logo
1 de 26
1
Properties of Context-free
Languages
Chapter 7
2
Topics
1) Simplifying CFGs, Normal forms
2) Pumping lemma for CFLs
3) Closure and decision properties of
CFLs
3
How to “simplify” CFGs?
4
Three ways to simplify/clean a CFG
(clean)
1. Eliminate useless symbols
(simplify)
2. Eliminate ε-productions
3. Eliminate unit productions
A => ε
A => B
5
Eliminating useless symbols
Grammar cleanup
6
Eliminating useless symbols
A symbol X is reachable if there exists:
 S *
α X β
A symbol X is generating if there exists:
 X *
w,

for some w ∈ T*
For a symbol X to be “useful”, it has to be both
reachable and generating

S *
α X β *
w’, for some w’ ∈ T*
reachable generating
7
Algorithm to detect useless
symbols
1. First, eliminate all symbols that are not
generating
2. Next, eliminate all symbols that are
not reachable
Is the order of these steps important,
or can we switch?
8
Example: Useless symbols
 SAB | a
 A b
1. A, S are generating
2. B is not generating (and therefore B is useless)
3. ==> Eliminating B… (i.e., remove all productions that involve B)
1. S a
2. A  b
4. Now, A is not reachable and therefore is useless
5. Simplified G:
1. S  a
What would happen if you reverse the order:
i.e., test reachability before generating?
Will fail to remove:
A  b
9
Eliminating ε-productions
A => ε
10
Eliminating ε-productions
It is not possible to eliminate ε-productions for
languages which include ε in their word set
Theorem: If G=(V,T,P,S) is a CFG for a language L,
then L {ε} has a CFG without ε-productions
Definition: A is “nullable” if A* ε
 If A is nullable, then any production of the form
“B CAD” can be simulated by:

B  CD | CAD
 This can allow us to remove ε transitions for A
A  ε
So we will target the grammar for the rest of the language
What’s the point of removing ε-productions?
11
Algorithm to detect all nullable
variables
 Basis:
 If A ε is a production in G, then A is
nullable
(note: A can still have other productions)
 Induction:
 If there is a production B C1C2…Ck, where
every Ci is nullable, then B is also nullable
12
Example: Eliminating ε-
productions
 Let L be the language represented by the following CFG G:
i. SAB
ii. AaAA | ε
iii. BbBB | ε
Goal: To construct G1, which is the grammar for L-{ε}
 Nullable symbols: {A, B}
 G1 can be constructed from G as follows:
 B  b | bB | bB | bBB
 ==> B  b | bB | bBB
 Similarly, A  a | aA | aAA
 Similarly, S  A | B | AB
 Note: L(G) = L(G1) U {ε}
G1:
• S  A | B | AB
• A  a | aA | aAA
• B  b | bB | bBB
Simplified
grammar
13
Eliminating unit productions
A => B B has to be a variable
What’s the point of removing unit transitions ?
A=>B | …
B=>C | …
C=>D | …
D=>xxx | yyy | zzz
A=>xxx | yyy | zzz | …
B=> xxx | yyy | zzz | …
C=> xxx | yyy | zzz | …
D=>xxx | yyy | zzz
Will save #substitutions
E.g.,
before after
14
Eliminating unit productions
 Unit production is one which is of the form A B, where both A & B
are variables
 E.g.,
1. E  T | E+T
2. T  F | T*F
3. F  I | (E)
4. I  a | b | Ia | Ib | I0 | I1
 How to eliminate unit productions?
A  B
15
The Unit Pair Algorithm:
to remove unit productions
 Suppose AB1 B2  …  Bn  α
 Action: Replace all intermediate productions to produce α
directly
 i.e., A α; B1 α; … Bn  α;
Definition: (A,B) to be a “unit pair” if A*
B
 We can find all unit pairs inductively:
 Basis: Every pair (A,A) is a unit pair (by definition). Similarly, if AB
is a production, then (A,B) is a unit pair.
 Induction: If (A,B) and (B,C) are unit pairs, and AC is also a unit
pair.
16
Example: eliminating unit
productions
G:
1. E  T | E+T
2. T  F | T*F
3. F  I | (E)
4. I  a | b | Ia | Ib | I0 | I1
Unit pairs Only non-unit
productions to be
added to P1
(E,E) E  E+T
(E,T) E  T*F
(E,F) E  (E)
(E,I) E  a|b|Ia | Ib | I0 | I1
(T,T) T  T*F
(T,F) T  (E)
(T,I) T  a|b| Ia | Ib | I0 | I1
(F,F) F  (E)
(F,I) F  a| b| Ia | Ib | I0 |
I1
(I,I) I  a| b | Ia | Ib | I0 |
I1
G1:
1. E  E+T | T*F | (E) | a| b | Ia | Ib | I0 | I1
2. T  T*F | (E) | a| b | Ia | Ib | I0 | I1
3. F  (E) | a| b | Ia | Ib | I0 | I1
4. I  a | b | Ia | Ib | I0 | I1
17
Putting all this together…
 Theorem: If G is a CFG for a language that
contains at least one string other than ε, then there
is another CFG G1, such that L(G1)=L(G) - ε, and G1
has:
 no ε -productions
 no unit productions
 no useless symbols
 Algorithm:
Step 1) eliminate ε -productions
Step 2) eliminate unit productions
Step 3) eliminate useless symbols
Again,
the order is
important!
Why?
18
Normal Forms
19
Why normal forms?
 If all productions of the grammar could be
expressed in the same form(s), then:
a. It becomes easy to design algorithms that use
the grammar
b. It becomes easy to show proofs and properties
20
Chomsky Normal Form (CNF)
Let G be a CFG for some L-{ε}
Definition:
G is said to be in Chomsky Normal Form if all
its productions are in one of the following
two forms:
i. A  BC where A,B,C are variables, or
ii. A  a where a is a terminal
 G has no useless symbols
 G has no unit productions
 G has no ε-productions
21
CNF checklist
G1:
1. E  E+T | T*F | (E) | Ia | Ib | I0 | I1
2. T  T*F | (E) | Ia | Ib | I0 | I1
3. F  (E) | Ia | Ib | I0 | I1
4. I  a | b | Ia | Ib | I0 | I1
Checklist:
• G has no ε-productions
• G has no unit productions
• G has no useless symbols
• But…
• the normal form for productions is violated
Is this grammar in CNF?
So, the grammar is not in CNF
22
How to convert a G into CNF?
 Assumption: G has no ε-productions, unit productions or useless
symbols
1) For every terminal a that appears in the body of a production:
i. create a unique variable, say Xa, with a production Xa  a, and
ii. replace all other instances of a in G by Xa
2) Now, all productions will be in one of the following
two forms:
 A  B1B2… Bk (k≥3) or Aa
1) Replace each production of the form A  B1B2B3… Bk by:
 AB1C1 C1B2C2 … Ck-3Bk-2Ck-2 Ck-2Bk-1Bk
B1 C1
B2 C2
and so on…
Example #1
23
G:
S => AS | BABC
A => A1 | 0A1 | 01
B => 0B | 0
C => 1C | 1
X0 => 0
X1 => 1
S => AS | BY1
Y1 => AY2
Y2 => BC
A => AX1 | X0Y3 | X0X1
Y3 => AX1
B => X0B | 0
C => X1C | 1
G in CNF:
All productions are of the form: A=>BC or A=>a
24
Example #2
G:
1. E  E+T | T*F | (E) | Ia | Ib | I0 | I1
2. T  T*F | (E) | Ia | Ib | I0 | I1
3. F  (E) | Ia | Ib | I0 | I1
4. I  a | b | Ia | Ib | I0 | I1
1. E  EX+T | TX*F | X(EX) | IXa | IXb | IX0 | IX1
2. T  TX*F | X(EX) | IXa | IXb | IX0 | IX1
3. F  X(EX) | IXa | IXb | IX0 | IX1
4. I  Xa | Xb | IXa | IXb | IX0 | IX1
5. X+  +
6. X*  *
7. X+  +
8. X(  (
9. …….
Step (1)
1. E  EC1 | TC2 | X(C3 | IXa | IXb | IX0 | IX1
2. C1  X+T
3. C2  X*F
4. C3  EX)
5. T  ..…….
6. ….
Step
(2)
25
CFL Closure Properties
26
Closure Property Results
 CFLs are closed under:
 Union
 Concatenation
 Substitution
 Homomorphism, inverse homomorphism
 reversal
 CFLs are not closed under:
 Intersection
 Difference
 Complementation
Note: Reg languages
are closed
under
these
operators

Mais conteúdo relacionado

Mais procurados

Binary logic
Binary logicBinary logic
Binary logic
wardjo
 
Mcs 10 104 compiler design dec 2014
Mcs 10 104 compiler design dec 2014Mcs 10 104 compiler design dec 2014
Mcs 10 104 compiler design dec 2014
Sreeju Sree
 
Data structures question paper anna university
Data structures question paper anna universityData structures question paper anna university
Data structures question paper anna university
sangeethajames07
 
Functions & Procedures [7]
Functions & Procedures [7]Functions & Procedures [7]
Functions & Procedures [7]
ecko_disasterz
 

Mais procurados (19)

Boothmultiplication
BoothmultiplicationBoothmultiplication
Boothmultiplication
 
[Question Paper] Fundamentals of Digital Computing (Revised Course) [April / ...
[Question Paper] Fundamentals of Digital Computing (Revised Course) [April / ...[Question Paper] Fundamentals of Digital Computing (Revised Course) [April / ...
[Question Paper] Fundamentals of Digital Computing (Revised Course) [April / ...
 
C Programming
C ProgrammingC Programming
C Programming
 
Binary logic
Binary logicBinary logic
Binary logic
 
Assignment6
Assignment6Assignment6
Assignment6
 
A109211002 switchingtheoryandlogicdesign1
A109211002 switchingtheoryandlogicdesign1A109211002 switchingtheoryandlogicdesign1
A109211002 switchingtheoryandlogicdesign1
 
[Question Paper] Introduction To C++ Programming (Revised Course) [January / ...
[Question Paper] Introduction To C++ Programming (Revised Course) [January / ...[Question Paper] Introduction To C++ Programming (Revised Course) [January / ...
[Question Paper] Introduction To C++ Programming (Revised Course) [January / ...
 
CSC Millionaire
CSC MillionaireCSC Millionaire
CSC Millionaire
 
Solve the problem showing all steps. Thoroughly explain how and why you perfo...
Solve the problem showing all steps. Thoroughly explain how and why you perfo...Solve the problem showing all steps. Thoroughly explain how and why you perfo...
Solve the problem showing all steps. Thoroughly explain how and why you perfo...
 
Assignment8
Assignment8Assignment8
Assignment8
 
Ec2203 digital electronics questions anna university by www.annaunivedu.org
Ec2203 digital electronics questions anna university by www.annaunivedu.orgEc2203 digital electronics questions anna university by www.annaunivedu.org
Ec2203 digital electronics questions anna university by www.annaunivedu.org
 
Mcs 10 104 compiler design dec 2014
Mcs 10 104 compiler design dec 2014Mcs 10 104 compiler design dec 2014
Mcs 10 104 compiler design dec 2014
 
EC202 SIGNALS & SYSTEMS PREVIOUS QUESTION PAPER
EC202 SIGNALS & SYSTEMS PREVIOUS QUESTION PAPEREC202 SIGNALS & SYSTEMS PREVIOUS QUESTION PAPER
EC202 SIGNALS & SYSTEMS PREVIOUS QUESTION PAPER
 
Assignment12
Assignment12Assignment12
Assignment12
 
C __paper.docx_final
C __paper.docx_finalC __paper.docx_final
C __paper.docx_final
 
B.Sc.IT: Semester - VI (April - 2015) [IDOL - Revised Course | Question Paper]
B.Sc.IT: Semester - VI (April - 2015) [IDOL - Revised Course | Question Paper]B.Sc.IT: Semester - VI (April - 2015) [IDOL - Revised Course | Question Paper]
B.Sc.IT: Semester - VI (April - 2015) [IDOL - Revised Course | Question Paper]
 
Data structures question paper anna university
Data structures question paper anna universityData structures question paper anna university
Data structures question paper anna university
 
Functions & Procedures [7]
Functions & Procedures [7]Functions & Procedures [7]
Functions & Procedures [7]
 
Context free grammar
Context free grammar Context free grammar
Context free grammar
 

Semelhante a Toc CFG cfl properties (15)

NORMAL-FORMS.ppt
NORMAL-FORMS.pptNORMAL-FORMS.ppt
NORMAL-FORMS.ppt
 
NORMAL-FORMS.ppt
NORMAL-FORMS.pptNORMAL-FORMS.ppt
NORMAL-FORMS.ppt
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
 
CHOMSKY AND GREIBACH NORMAL FORM.ppt
CHOMSKY AND GREIBACH NORMAL FORM.pptCHOMSKY AND GREIBACH NORMAL FORM.ppt
CHOMSKY AND GREIBACH NORMAL FORM.ppt
 
Chomsky_GreibachNF.ppt
Chomsky_GreibachNF.pptChomsky_GreibachNF.ppt
Chomsky_GreibachNF.ppt
 
Chomsky_Greibach-Hector-Chavez.ppt
Chomsky_Greibach-Hector-Chavez.pptChomsky_Greibach-Hector-Chavez.ppt
Chomsky_Greibach-Hector-Chavez.ppt
 
Context Free Languages by S.Mandal-1.ppt
Context Free Languages by S.Mandal-1.pptContext Free Languages by S.Mandal-1.ppt
Context Free Languages by S.Mandal-1.ppt
 
ContextFreeGrammars.pptx
ContextFreeGrammars.pptxContextFreeGrammars.pptx
ContextFreeGrammars.pptx
 
ContextFreeGrammars (1).pptx
ContextFreeGrammars (1).pptxContextFreeGrammars (1).pptx
ContextFreeGrammars (1).pptx
 
affTA09 - LampiranA
affTA09 - LampiranAaffTA09 - LampiranA
affTA09 - LampiranA
 
5.stack
5.stack5.stack
5.stack
 
Compiler Design Bottom Up Parsing Technique S
Compiler Design Bottom Up Parsing Technique SCompiler Design Bottom Up Parsing Technique S
Compiler Design Bottom Up Parsing Technique S
 
Lab 3 Multi-Function Gate
Lab 3   Multi-Function GateLab 3   Multi-Function Gate
Lab 3 Multi-Function Gate
 
Assigment1
Assigment1Assigment1
Assigment1
 
Digital VLSI - Unit 2.pptx
Digital VLSI - Unit 2.pptxDigital VLSI - Unit 2.pptx
Digital VLSI - Unit 2.pptx
 

Último

Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
Kamal Acharya
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
jaanualu31
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
MayuraD1
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
mphochane1998
 

Último (20)

Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 
Wadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptxWadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptx
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLEGEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 

Toc CFG cfl properties

  • 2. 2 Topics 1) Simplifying CFGs, Normal forms 2) Pumping lemma for CFLs 3) Closure and decision properties of CFLs
  • 4. 4 Three ways to simplify/clean a CFG (clean) 1. Eliminate useless symbols (simplify) 2. Eliminate ε-productions 3. Eliminate unit productions A => ε A => B
  • 6. 6 Eliminating useless symbols A symbol X is reachable if there exists:  S * α X β A symbol X is generating if there exists:  X * w,  for some w ∈ T* For a symbol X to be “useful”, it has to be both reachable and generating  S * α X β * w’, for some w’ ∈ T* reachable generating
  • 7. 7 Algorithm to detect useless symbols 1. First, eliminate all symbols that are not generating 2. Next, eliminate all symbols that are not reachable Is the order of these steps important, or can we switch?
  • 8. 8 Example: Useless symbols  SAB | a  A b 1. A, S are generating 2. B is not generating (and therefore B is useless) 3. ==> Eliminating B… (i.e., remove all productions that involve B) 1. S a 2. A  b 4. Now, A is not reachable and therefore is useless 5. Simplified G: 1. S  a What would happen if you reverse the order: i.e., test reachability before generating? Will fail to remove: A  b
  • 10. 10 Eliminating ε-productions It is not possible to eliminate ε-productions for languages which include ε in their word set Theorem: If G=(V,T,P,S) is a CFG for a language L, then L {ε} has a CFG without ε-productions Definition: A is “nullable” if A* ε  If A is nullable, then any production of the form “B CAD” can be simulated by:  B  CD | CAD  This can allow us to remove ε transitions for A A  ε So we will target the grammar for the rest of the language What’s the point of removing ε-productions?
  • 11. 11 Algorithm to detect all nullable variables  Basis:  If A ε is a production in G, then A is nullable (note: A can still have other productions)  Induction:  If there is a production B C1C2…Ck, where every Ci is nullable, then B is also nullable
  • 12. 12 Example: Eliminating ε- productions  Let L be the language represented by the following CFG G: i. SAB ii. AaAA | ε iii. BbBB | ε Goal: To construct G1, which is the grammar for L-{ε}  Nullable symbols: {A, B}  G1 can be constructed from G as follows:  B  b | bB | bB | bBB  ==> B  b | bB | bBB  Similarly, A  a | aA | aAA  Similarly, S  A | B | AB  Note: L(G) = L(G1) U {ε} G1: • S  A | B | AB • A  a | aA | aAA • B  b | bB | bBB Simplified grammar
  • 13. 13 Eliminating unit productions A => B B has to be a variable What’s the point of removing unit transitions ? A=>B | … B=>C | … C=>D | … D=>xxx | yyy | zzz A=>xxx | yyy | zzz | … B=> xxx | yyy | zzz | … C=> xxx | yyy | zzz | … D=>xxx | yyy | zzz Will save #substitutions E.g., before after
  • 14. 14 Eliminating unit productions  Unit production is one which is of the form A B, where both A & B are variables  E.g., 1. E  T | E+T 2. T  F | T*F 3. F  I | (E) 4. I  a | b | Ia | Ib | I0 | I1  How to eliminate unit productions? A  B
  • 15. 15 The Unit Pair Algorithm: to remove unit productions  Suppose AB1 B2  …  Bn  α  Action: Replace all intermediate productions to produce α directly  i.e., A α; B1 α; … Bn  α; Definition: (A,B) to be a “unit pair” if A* B  We can find all unit pairs inductively:  Basis: Every pair (A,A) is a unit pair (by definition). Similarly, if AB is a production, then (A,B) is a unit pair.  Induction: If (A,B) and (B,C) are unit pairs, and AC is also a unit pair.
  • 16. 16 Example: eliminating unit productions G: 1. E  T | E+T 2. T  F | T*F 3. F  I | (E) 4. I  a | b | Ia | Ib | I0 | I1 Unit pairs Only non-unit productions to be added to P1 (E,E) E  E+T (E,T) E  T*F (E,F) E  (E) (E,I) E  a|b|Ia | Ib | I0 | I1 (T,T) T  T*F (T,F) T  (E) (T,I) T  a|b| Ia | Ib | I0 | I1 (F,F) F  (E) (F,I) F  a| b| Ia | Ib | I0 | I1 (I,I) I  a| b | Ia | Ib | I0 | I1 G1: 1. E  E+T | T*F | (E) | a| b | Ia | Ib | I0 | I1 2. T  T*F | (E) | a| b | Ia | Ib | I0 | I1 3. F  (E) | a| b | Ia | Ib | I0 | I1 4. I  a | b | Ia | Ib | I0 | I1
  • 17. 17 Putting all this together…  Theorem: If G is a CFG for a language that contains at least one string other than ε, then there is another CFG G1, such that L(G1)=L(G) - ε, and G1 has:  no ε -productions  no unit productions  no useless symbols  Algorithm: Step 1) eliminate ε -productions Step 2) eliminate unit productions Step 3) eliminate useless symbols Again, the order is important! Why?
  • 19. 19 Why normal forms?  If all productions of the grammar could be expressed in the same form(s), then: a. It becomes easy to design algorithms that use the grammar b. It becomes easy to show proofs and properties
  • 20. 20 Chomsky Normal Form (CNF) Let G be a CFG for some L-{ε} Definition: G is said to be in Chomsky Normal Form if all its productions are in one of the following two forms: i. A  BC where A,B,C are variables, or ii. A  a where a is a terminal  G has no useless symbols  G has no unit productions  G has no ε-productions
  • 21. 21 CNF checklist G1: 1. E  E+T | T*F | (E) | Ia | Ib | I0 | I1 2. T  T*F | (E) | Ia | Ib | I0 | I1 3. F  (E) | Ia | Ib | I0 | I1 4. I  a | b | Ia | Ib | I0 | I1 Checklist: • G has no ε-productions • G has no unit productions • G has no useless symbols • But… • the normal form for productions is violated Is this grammar in CNF? So, the grammar is not in CNF
  • 22. 22 How to convert a G into CNF?  Assumption: G has no ε-productions, unit productions or useless symbols 1) For every terminal a that appears in the body of a production: i. create a unique variable, say Xa, with a production Xa  a, and ii. replace all other instances of a in G by Xa 2) Now, all productions will be in one of the following two forms:  A  B1B2… Bk (k≥3) or Aa 1) Replace each production of the form A  B1B2B3… Bk by:  AB1C1 C1B2C2 … Ck-3Bk-2Ck-2 Ck-2Bk-1Bk B1 C1 B2 C2 and so on…
  • 23. Example #1 23 G: S => AS | BABC A => A1 | 0A1 | 01 B => 0B | 0 C => 1C | 1 X0 => 0 X1 => 1 S => AS | BY1 Y1 => AY2 Y2 => BC A => AX1 | X0Y3 | X0X1 Y3 => AX1 B => X0B | 0 C => X1C | 1 G in CNF: All productions are of the form: A=>BC or A=>a
  • 24. 24 Example #2 G: 1. E  E+T | T*F | (E) | Ia | Ib | I0 | I1 2. T  T*F | (E) | Ia | Ib | I0 | I1 3. F  (E) | Ia | Ib | I0 | I1 4. I  a | b | Ia | Ib | I0 | I1 1. E  EX+T | TX*F | X(EX) | IXa | IXb | IX0 | IX1 2. T  TX*F | X(EX) | IXa | IXb | IX0 | IX1 3. F  X(EX) | IXa | IXb | IX0 | IX1 4. I  Xa | Xb | IXa | IXb | IX0 | IX1 5. X+  + 6. X*  * 7. X+  + 8. X(  ( 9. ……. Step (1) 1. E  EC1 | TC2 | X(C3 | IXa | IXb | IX0 | IX1 2. C1  X+T 3. C2  X*F 4. C3  EX) 5. T  ..……. 6. …. Step (2)
  • 26. 26 Closure Property Results  CFLs are closed under:  Union  Concatenation  Substitution  Homomorphism, inverse homomorphism  reversal  CFLs are not closed under:  Intersection  Difference  Complementation Note: Reg languages are closed under these operators