SlideShare a Scribd company logo
Theory of Computation
CSE 2233
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
CFG – Context Free Grammar
2
A Context Free Grammar is a 4 tuple (V, 𝛴, R, S), where
▸ V is a finite set called the variables
▸ Σ is a finite set, disjoint from V, called the terminals
▸ R is a finite set of rules, with each rule being a variable and a string of variables and terminals
▸ S ϵ V is the start variable
Example:
Grammar, G1 = ( { S }, { a, b }, R, S )
V = { S }
Σ = { a, b }
R is,
S → aSb | SS | ε
Example:
Grammar, G2 = ( V, Σ, R, <EXPR> )
V = { <EXPR>, <TERM>, <FACTOR> }
Σ = { a, +, x, (, ) }
R is,
<EXPR> → <EXPR> + <TERM> | <TERM>
<TERM> → <TERM> x <FACTOR> | <FACTOR>
<FACTOR> → ( <EXPR> ) | a
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
CNF - Chomsky Normal Form
3
▸ When working with context-free grammar, it is often convenient to have them in simplified form.
▸ One of the simplest and most useful forms is called the Chomsky Normal Form.
▸ CNF is useful in giving algorithms for working with CFGs.
Chomsky Normal Form:
A context-free grammar is in Chomsky Normal Form if every rule is of the form,
A → BC
A → a
where a is any terminal and A, B, C are any variables – except that B and C may not be the start variable.
If S is the start variable, then the rule S → 𝜀 is valid.
[ i.e. if language contains 𝜀, then we allow S -> 𝜀 where S is start symbol, and forbid S on RHS]
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
CFG in CNF
4
Theorem:
Any Context Free Language is generated by a Context-Free Grammar in Chomsky Normal Form.
The conversion to Chomsky Normal Form has 4 main steps:
▸ Get rid of all 𝜀 productions.
▸ Get rid of all productions where RHS is a single variable.
▸ Replace every production that is too long by shorter productions.
▸ Move all terminals to productions where RHS is one terminal.
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
CFG in CNF – Step 1
5
CFG to CNF conversion principle:
Rules that violate the CNF conditions are replaced with equivalent ones that are satisfactory.
Step 1:
Add a new start variable S0 and the rule S0 → S where S was the original start variable.
▸It guarantees that the start variable doesn’t occur on the right-hand side of a rule.
Example:
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
S → ASA | aB
A → B | S
B → b | ε
S0 → S
S → ASA | aB
A → B | S
B → b | 𝜀
Step 1
CFG in CNF – Step 2
6
Step 2:
Eliminate all 𝜀 rules of the form A → 𝜀, where A is not the start variable.
▸For each occurrence of an A on the right-hand side of a rule, add a new rule with that occurrence deleted.
– If R → uAv is a rule in which u and v are strings of variables and terminals, we add new rule,
R → uv
– Do this for each occurrence of A. If the rule is R → uAvAw then we will add rules,
R → uvAw, R → uAvw, and R → uvw
– If the rule is R → A, then the new rule becomes R → 𝜀 unless we had previously removed the rule R → 𝜀
▸Repeat this step until all 𝜀-rules are eliminated.
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Example:
CFG in CNF – Step 2
7
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Removing 𝜀-rule B → 𝜀 ,
S0 → S
S → ASA | aB | a
A → B | S | 𝜀
B → b | 𝜀
S → ASA | aB
A → B | S
B → b | ε
S0 → S
S → ASA | aB
A → B | S
B → b | 𝜀
Step 1
Step 2
Removing 𝜀-rule A → 𝜀 ,
S0 → S
S → ASA | aB | a | SA | AS | S
A → B | S | 𝜀
B → b
Step 2
CFG in CNF – Step 3
8
Step 3: Eliminate all the unit rules of the form A → B.
▸ Whenever a rule B → u appears, add a new rule A → u unless this was a unit rule previously removed.
▸ Repeat this step until all unit rules are eliminated.
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Example:
CFG in CNF – Step 3
9
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Removing 𝜀-rule B → 𝜀 ,
S0 → S
S → ASA | aB | a
A → B | S | 𝜀
B → b | 𝜀
S → ASA | aB
A → B | S
B → b | ε
S0 → S
S → ASA | aB
A → B | S
B → b | 𝜀
Step 1 Step 2
Removing 𝜀-rule A → 𝜀 ,
S0 → S
S → ASA | aB | a | SA | AS | S
A → B | S | 𝜀
B → b
Step 2
Removing unit rule S → S,
S0 → S
S → ASA | aB | a | SA | AS | S
A → B | S
B → b
Step 3
Removing unit rule S0 → S,
S0 → S | ASA | aB | a | SA | AS
S → ASA | aB | a | SA | AS
A → B | S
B → b
Step 3
Removing unit rule A → B,
S0 → ASA | aB | a | SA | AS
S → ASA | aB | a | SA | AS
A → B | S | b
B → b
Step 3
Removing unit rule A → S,
S0 → ASA | aB | a | SA | AS
S → ASA | aB | a | SA | AS
A → S | b | ASA | aB | a | SA | AS
B → b
Step 3
CFG in CNF – Step 4
10
Step 4:
Convert all the remaining rules in the proper form.
▸ Replace each rule of the form, A → u1 u2 … … uk where k>=3 and each ui is a variable or terminal symbol with these new
rules,
A → u1 A1, A1 → u2 A2, A2 → u3 A3, … … , Ak-2 → uk-1 uk
▸Replace each terminal ui in the preceding rules with the new variable Ui and add the rule,
Ui → ui
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Example:
CFG in CNF – Step 4
11
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Removing 𝜀-rule B → 𝜀 ,
S0 → S
S → ASA | aB | a
A → B | S | 𝜀
B → b | 𝜀
S → ASA | aB
A → B | S
B → b | ε
S0 → S
S → ASA | aB
A → B | S
B → b | 𝜀
Step 1 Step 2
Removing 𝜀-rule A → 𝜀 ,
S0 → S
S → ASA | aB | a | SA | AS | S
A → B | S | 𝜀
B → b
Step 2
Removing unit rule S → S,
S0 → S
S → ASA | aB | a | SA | AS | S
A → B | S
B → b
Step 3
Removing unit rule S0 → S,
S0 → S | ASA | aB | a | SA | AS
S → ASA | aB | a | SA | AS
A → B | S
B → b
Step 3
Removing unit rule A → B,
S0 → ASA | aB | a | SA | AS
S → ASA | aB | a | SA | AS
A → B | S | b
B → b
Step 3
Removing unit rule A → S,
S0 → ASA | aB | a | SA | AS
S → ASA | aB | a | SA | AS
A → S | b | ASA | aB | a | SA | AS
B → b
Step 3
Proper form conversion,
S0 → AA1 | UB | a | SA | AS
A1 → SA
U → a
S → AA1 | UB | a | SA | AS
A → b | AA1 | UB | a | SA | AS
B → b
Step 4
CFG in CNF – Practice Set
12
1.
S → ASB
A → aAS | a | ε
B → SbS | A | bb
2.
S → XY
X → abb | aXb | ε
Y → c | cY
3.
S → aXbY
X → aX | 𝜀
Y → bY | 𝜀
4.
S → aXbX
X → aY | bY | 𝜀
Y → X | c
5.
S → S1 | S2
S1 → S1b | Ab | 𝜀
A → aAb | ab
S2 → S2a | Ba | 𝜀
B → bBa | ba
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
6.
S → DBC | Ba
B → 0B1 | 01 | 𝜀
C → aCb | aC | Bb
D → bD | D
7.
S → aX | bY | b | ZZc
X → Yaa | abZ | 𝜀
Y → bXXb | ab | cZ
Z → a | b | XZ | 𝜀
8.
A -> BAB | B | 𝜀
B -> 00 | 𝜀
13
References:
Chapter 2(section 2.1), Introduction to the Theory of Computation, 3rd Edition by Michael Sipser
THANKS!
Any questions?
You can find me at imam@cse.uiu.ac.bd

More Related Content

What's hot

Chapter1 Formal Language and Automata Theory
Chapter1 Formal Language and Automata TheoryChapter1 Formal Language and Automata Theory
Chapter1 Formal Language and Automata Theory
Tsegazeab Asgedom
 
CFG to CNF
CFG to CNFCFG to CNF
CFG to CNF
Zain Ul Abiden
 
Chomsky & Greibach Normal Forms
Chomsky & Greibach Normal FormsChomsky & Greibach Normal Forms
Chomsky & Greibach Normal Forms
Rajendran
 
Theory of Automata
Theory of AutomataTheory of Automata
Theory of Automata
Farooq Mian
 
TOC 4 | Non-deterministic Finite Automata
TOC 4 | Non-deterministic Finite AutomataTOC 4 | Non-deterministic Finite Automata
TOC 4 | Non-deterministic Finite Automata
Mohammad Imam Hossain
 
Context free grammar
Context free grammar Context free grammar
Context free grammar
Mohammad Ilyas Malik
 
Lecture 3,4
Lecture 3,4Lecture 3,4
Lecture 3,4
shah zeb
 
TOC 3 | Different Operations on DFA
TOC 3 | Different Operations on DFATOC 3 | Different Operations on DFA
TOC 3 | Different Operations on DFA
Mohammad Imam Hossain
 
Lecture: Automata
Lecture: AutomataLecture: Automata
Lecture: Automata
Marina Santini
 
CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR
Zahid Parvez
 
Theory of Computation Grammar Concepts and Problems
Theory of Computation Grammar Concepts and ProblemsTheory of Computation Grammar Concepts and Problems
Theory of Computation Grammar Concepts and Problems
Rushabh2428
 
Automata theory
Automata theoryAutomata theory
Automata theory
colleges
 
Pumping lemma Theory Of Automata
Pumping lemma Theory Of AutomataPumping lemma Theory Of Automata
Pumping lemma Theory Of Automata
hafizhamza0322
 
NFA & DFA
NFA & DFANFA & DFA
NFA & DFA
Akhil Kaushik
 
AUTOMATA THEORY - SHORT NOTES
AUTOMATA THEORY - SHORT NOTESAUTOMATA THEORY - SHORT NOTES
AUTOMATA THEORY - SHORT NOTES
suthi
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal language
Rabia Khalid
 
Finite automata intro
Finite automata introFinite automata intro
Finite automata introlavishka_anuj
 
Context free grammar
Context free grammarContext free grammar
Context free grammar
Radhakrishnan Chinnusamy
 
TOC 2 | Deterministic Finite Automata
TOC 2 | Deterministic Finite AutomataTOC 2 | Deterministic Finite Automata
TOC 2 | Deterministic Finite Automata
Mohammad Imam Hossain
 
2.8 normal forms gnf &amp; problems
2.8 normal forms   gnf &amp; problems2.8 normal forms   gnf &amp; problems
2.8 normal forms gnf &amp; problems
Sampath Kumar S
 

What's hot (20)

Chapter1 Formal Language and Automata Theory
Chapter1 Formal Language and Automata TheoryChapter1 Formal Language and Automata Theory
Chapter1 Formal Language and Automata Theory
 
CFG to CNF
CFG to CNFCFG to CNF
CFG to CNF
 
Chomsky & Greibach Normal Forms
Chomsky & Greibach Normal FormsChomsky & Greibach Normal Forms
Chomsky & Greibach Normal Forms
 
Theory of Automata
Theory of AutomataTheory of Automata
Theory of Automata
 
TOC 4 | Non-deterministic Finite Automata
TOC 4 | Non-deterministic Finite AutomataTOC 4 | Non-deterministic Finite Automata
TOC 4 | Non-deterministic Finite Automata
 
Context free grammar
Context free grammar Context free grammar
Context free grammar
 
Lecture 3,4
Lecture 3,4Lecture 3,4
Lecture 3,4
 
TOC 3 | Different Operations on DFA
TOC 3 | Different Operations on DFATOC 3 | Different Operations on DFA
TOC 3 | Different Operations on DFA
 
Lecture: Automata
Lecture: AutomataLecture: Automata
Lecture: Automata
 
CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR
 
Theory of Computation Grammar Concepts and Problems
Theory of Computation Grammar Concepts and ProblemsTheory of Computation Grammar Concepts and Problems
Theory of Computation Grammar Concepts and Problems
 
Automata theory
Automata theoryAutomata theory
Automata theory
 
Pumping lemma Theory Of Automata
Pumping lemma Theory Of AutomataPumping lemma Theory Of Automata
Pumping lemma Theory Of Automata
 
NFA & DFA
NFA & DFANFA & DFA
NFA & DFA
 
AUTOMATA THEORY - SHORT NOTES
AUTOMATA THEORY - SHORT NOTESAUTOMATA THEORY - SHORT NOTES
AUTOMATA THEORY - SHORT NOTES
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal language
 
Finite automata intro
Finite automata introFinite automata intro
Finite automata intro
 
Context free grammar
Context free grammarContext free grammar
Context free grammar
 
TOC 2 | Deterministic Finite Automata
TOC 2 | Deterministic Finite AutomataTOC 2 | Deterministic Finite Automata
TOC 2 | Deterministic Finite Automata
 
2.8 normal forms gnf &amp; problems
2.8 normal forms   gnf &amp; problems2.8 normal forms   gnf &amp; problems
2.8 normal forms gnf &amp; problems
 

More from Mohammad Imam Hossain

DS & Algo 6 - Offline Assignment 6
DS & Algo 6 - Offline Assignment 6DS & Algo 6 - Offline Assignment 6
DS & Algo 6 - Offline Assignment 6
Mohammad Imam Hossain
 
DS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic ProgrammingDS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic Programming
Mohammad Imam Hossain
 
DS & Algo 5 - Disjoint Set and MST
DS & Algo 5 - Disjoint Set and MSTDS & Algo 5 - Disjoint Set and MST
DS & Algo 5 - Disjoint Set and MST
Mohammad Imam Hossain
 
DS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path SearchDS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path Search
Mohammad Imam Hossain
 
DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3
Mohammad Imam Hossain
 
DS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and ConquerDS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and Conquer
Mohammad Imam Hossain
 
DS & Algo 2 - Offline Assignment 2
DS & Algo 2 - Offline Assignment 2DS & Algo 2 - Offline Assignment 2
DS & Algo 2 - Offline Assignment 2
Mohammad Imam Hossain
 
DS & Algo 2 - Recursion
DS & Algo 2 - RecursionDS & Algo 2 - Recursion
DS & Algo 2 - Recursion
Mohammad Imam Hossain
 
DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1
Mohammad Imam Hossain
 
DS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL IntroductionDS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL Introduction
Mohammad Imam Hossain
 
DBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMSDBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMS
Mohammad Imam Hossain
 
DBMS 10 | Database Transactions
DBMS 10 | Database TransactionsDBMS 10 | Database Transactions
DBMS 10 | Database Transactions
Mohammad Imam Hossain
 
DBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational SchemaDBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational Schema
Mohammad Imam Hossain
 
DBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship ModelDBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship Model
Mohammad Imam Hossain
 
DBMS 7 | Relational Query Language
DBMS 7 | Relational Query LanguageDBMS 7 | Relational Query Language
DBMS 7 | Relational Query Language
Mohammad Imam Hossain
 
DBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML CommandsDBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML Commands
Mohammad Imam Hossain
 
DBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR SchemaDBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR Schema
Mohammad Imam Hossain
 
TOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity CheckTOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity Check
Mohammad Imam Hossain
 
DBMS 6 | MySQL Practice List - Rank Related Queries
DBMS 6 | MySQL Practice List - Rank Related QueriesDBMS 6 | MySQL Practice List - Rank Related Queries
DBMS 6 | MySQL Practice List - Rank Related Queries
Mohammad Imam Hossain
 
TOC 1 | Introduction to Theory of Computation
TOC 1 | Introduction to Theory of ComputationTOC 1 | Introduction to Theory of Computation
TOC 1 | Introduction to Theory of Computation
Mohammad Imam Hossain
 

More from Mohammad Imam Hossain (20)

DS & Algo 6 - Offline Assignment 6
DS & Algo 6 - Offline Assignment 6DS & Algo 6 - Offline Assignment 6
DS & Algo 6 - Offline Assignment 6
 
DS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic ProgrammingDS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic Programming
 
DS & Algo 5 - Disjoint Set and MST
DS & Algo 5 - Disjoint Set and MSTDS & Algo 5 - Disjoint Set and MST
DS & Algo 5 - Disjoint Set and MST
 
DS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path SearchDS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path Search
 
DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3
 
DS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and ConquerDS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and Conquer
 
DS & Algo 2 - Offline Assignment 2
DS & Algo 2 - Offline Assignment 2DS & Algo 2 - Offline Assignment 2
DS & Algo 2 - Offline Assignment 2
 
DS & Algo 2 - Recursion
DS & Algo 2 - RecursionDS & Algo 2 - Recursion
DS & Algo 2 - Recursion
 
DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1
 
DS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL IntroductionDS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL Introduction
 
DBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMSDBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMS
 
DBMS 10 | Database Transactions
DBMS 10 | Database TransactionsDBMS 10 | Database Transactions
DBMS 10 | Database Transactions
 
DBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational SchemaDBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational Schema
 
DBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship ModelDBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship Model
 
DBMS 7 | Relational Query Language
DBMS 7 | Relational Query LanguageDBMS 7 | Relational Query Language
DBMS 7 | Relational Query Language
 
DBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML CommandsDBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML Commands
 
DBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR SchemaDBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR Schema
 
TOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity CheckTOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity Check
 
DBMS 6 | MySQL Practice List - Rank Related Queries
DBMS 6 | MySQL Practice List - Rank Related QueriesDBMS 6 | MySQL Practice List - Rank Related Queries
DBMS 6 | MySQL Practice List - Rank Related Queries
 
TOC 1 | Introduction to Theory of Computation
TOC 1 | Introduction to Theory of ComputationTOC 1 | Introduction to Theory of Computation
TOC 1 | Introduction to Theory of Computation
 

Recently uploaded

Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
PedroFerreira53928
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
Vivekanand Anglo Vedic Academy
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
Steve Thomason
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 

Recently uploaded (20)

Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 

TOC 7 | CFG in Chomsky Normal Form

  • 1. Theory of Computation CSE 2233 Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 2. CFG – Context Free Grammar 2 A Context Free Grammar is a 4 tuple (V, 𝛴, R, S), where ▸ V is a finite set called the variables ▸ Σ is a finite set, disjoint from V, called the terminals ▸ R is a finite set of rules, with each rule being a variable and a string of variables and terminals ▸ S ϵ V is the start variable Example: Grammar, G1 = ( { S }, { a, b }, R, S ) V = { S } Σ = { a, b } R is, S → aSb | SS | ε Example: Grammar, G2 = ( V, Σ, R, <EXPR> ) V = { <EXPR>, <TERM>, <FACTOR> } Σ = { a, +, x, (, ) } R is, <EXPR> → <EXPR> + <TERM> | <TERM> <TERM> → <TERM> x <FACTOR> | <FACTOR> <FACTOR> → ( <EXPR> ) | a Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 3. CNF - Chomsky Normal Form 3 ▸ When working with context-free grammar, it is often convenient to have them in simplified form. ▸ One of the simplest and most useful forms is called the Chomsky Normal Form. ▸ CNF is useful in giving algorithms for working with CFGs. Chomsky Normal Form: A context-free grammar is in Chomsky Normal Form if every rule is of the form, A → BC A → a where a is any terminal and A, B, C are any variables – except that B and C may not be the start variable. If S is the start variable, then the rule S → 𝜀 is valid. [ i.e. if language contains 𝜀, then we allow S -> 𝜀 where S is start symbol, and forbid S on RHS] Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 4. CFG in CNF 4 Theorem: Any Context Free Language is generated by a Context-Free Grammar in Chomsky Normal Form. The conversion to Chomsky Normal Form has 4 main steps: ▸ Get rid of all 𝜀 productions. ▸ Get rid of all productions where RHS is a single variable. ▸ Replace every production that is too long by shorter productions. ▸ Move all terminals to productions where RHS is one terminal. Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 5. CFG in CNF – Step 1 5 CFG to CNF conversion principle: Rules that violate the CNF conditions are replaced with equivalent ones that are satisfactory. Step 1: Add a new start variable S0 and the rule S0 → S where S was the original start variable. ▸It guarantees that the start variable doesn’t occur on the right-hand side of a rule. Example: Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU S → ASA | aB A → B | S B → b | ε S0 → S S → ASA | aB A → B | S B → b | 𝜀 Step 1
  • 6. CFG in CNF – Step 2 6 Step 2: Eliminate all 𝜀 rules of the form A → 𝜀, where A is not the start variable. ▸For each occurrence of an A on the right-hand side of a rule, add a new rule with that occurrence deleted. – If R → uAv is a rule in which u and v are strings of variables and terminals, we add new rule, R → uv – Do this for each occurrence of A. If the rule is R → uAvAw then we will add rules, R → uvAw, R → uAvw, and R → uvw – If the rule is R → A, then the new rule becomes R → 𝜀 unless we had previously removed the rule R → 𝜀 ▸Repeat this step until all 𝜀-rules are eliminated. Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 7. Example: CFG in CNF – Step 2 7 Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU Removing 𝜀-rule B → 𝜀 , S0 → S S → ASA | aB | a A → B | S | 𝜀 B → b | 𝜀 S → ASA | aB A → B | S B → b | ε S0 → S S → ASA | aB A → B | S B → b | 𝜀 Step 1 Step 2 Removing 𝜀-rule A → 𝜀 , S0 → S S → ASA | aB | a | SA | AS | S A → B | S | 𝜀 B → b Step 2
  • 8. CFG in CNF – Step 3 8 Step 3: Eliminate all the unit rules of the form A → B. ▸ Whenever a rule B → u appears, add a new rule A → u unless this was a unit rule previously removed. ▸ Repeat this step until all unit rules are eliminated. Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 9. Example: CFG in CNF – Step 3 9 Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU Removing 𝜀-rule B → 𝜀 , S0 → S S → ASA | aB | a A → B | S | 𝜀 B → b | 𝜀 S → ASA | aB A → B | S B → b | ε S0 → S S → ASA | aB A → B | S B → b | 𝜀 Step 1 Step 2 Removing 𝜀-rule A → 𝜀 , S0 → S S → ASA | aB | a | SA | AS | S A → B | S | 𝜀 B → b Step 2 Removing unit rule S → S, S0 → S S → ASA | aB | a | SA | AS | S A → B | S B → b Step 3 Removing unit rule S0 → S, S0 → S | ASA | aB | a | SA | AS S → ASA | aB | a | SA | AS A → B | S B → b Step 3 Removing unit rule A → B, S0 → ASA | aB | a | SA | AS S → ASA | aB | a | SA | AS A → B | S | b B → b Step 3 Removing unit rule A → S, S0 → ASA | aB | a | SA | AS S → ASA | aB | a | SA | AS A → S | b | ASA | aB | a | SA | AS B → b Step 3
  • 10. CFG in CNF – Step 4 10 Step 4: Convert all the remaining rules in the proper form. ▸ Replace each rule of the form, A → u1 u2 … … uk where k>=3 and each ui is a variable or terminal symbol with these new rules, A → u1 A1, A1 → u2 A2, A2 → u3 A3, … … , Ak-2 → uk-1 uk ▸Replace each terminal ui in the preceding rules with the new variable Ui and add the rule, Ui → ui Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 11. Example: CFG in CNF – Step 4 11 Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU Removing 𝜀-rule B → 𝜀 , S0 → S S → ASA | aB | a A → B | S | 𝜀 B → b | 𝜀 S → ASA | aB A → B | S B → b | ε S0 → S S → ASA | aB A → B | S B → b | 𝜀 Step 1 Step 2 Removing 𝜀-rule A → 𝜀 , S0 → S S → ASA | aB | a | SA | AS | S A → B | S | 𝜀 B → b Step 2 Removing unit rule S → S, S0 → S S → ASA | aB | a | SA | AS | S A → B | S B → b Step 3 Removing unit rule S0 → S, S0 → S | ASA | aB | a | SA | AS S → ASA | aB | a | SA | AS A → B | S B → b Step 3 Removing unit rule A → B, S0 → ASA | aB | a | SA | AS S → ASA | aB | a | SA | AS A → B | S | b B → b Step 3 Removing unit rule A → S, S0 → ASA | aB | a | SA | AS S → ASA | aB | a | SA | AS A → S | b | ASA | aB | a | SA | AS B → b Step 3 Proper form conversion, S0 → AA1 | UB | a | SA | AS A1 → SA U → a S → AA1 | UB | a | SA | AS A → b | AA1 | UB | a | SA | AS B → b Step 4
  • 12. CFG in CNF – Practice Set 12 1. S → ASB A → aAS | a | ε B → SbS | A | bb 2. S → XY X → abb | aXb | ε Y → c | cY 3. S → aXbY X → aX | 𝜀 Y → bY | 𝜀 4. S → aXbX X → aY | bY | 𝜀 Y → X | c 5. S → S1 | S2 S1 → S1b | Ab | 𝜀 A → aAb | ab S2 → S2a | Ba | 𝜀 B → bBa | ba Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU 6. S → DBC | Ba B → 0B1 | 01 | 𝜀 C → aCb | aC | Bb D → bD | D 7. S → aX | bY | b | ZZc X → Yaa | abZ | 𝜀 Y → bXXb | ab | cZ Z → a | b | XZ | 𝜀 8. A -> BAB | B | 𝜀 B -> 00 | 𝜀
  • 13. 13 References: Chapter 2(section 2.1), Introduction to the Theory of Computation, 3rd Edition by Michael Sipser THANKS! Any questions? You can find me at imam@cse.uiu.ac.bd