SlideShare a Scribd company logo
1 of 13
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

Chomsky & Greibach Normal Forms
Chomsky & Greibach Normal FormsChomsky & Greibach Normal Forms
Chomsky & Greibach Normal FormsRajendran
 
Deterministic Finite Automata
Deterministic Finite AutomataDeterministic Finite Automata
Deterministic Finite AutomataShiraz316
 
TOC 2 | Deterministic Finite Automata
TOC 2 | Deterministic Finite AutomataTOC 2 | Deterministic Finite Automata
TOC 2 | Deterministic Finite AutomataMohammad Imam Hossain
 
Finite automata(For college Seminars)
Finite automata(For college Seminars)Finite automata(For college Seminars)
Finite automata(For college Seminars)Naman Joshi
 
Intro automata theory
Intro automata theory Intro automata theory
Intro automata theory Rajendran
 
Pumping lemma Theory Of Automata
Pumping lemma Theory Of AutomataPumping lemma Theory Of Automata
Pumping lemma Theory Of Automatahafizhamza0322
 
Types of grammer - TOC
Types of grammer - TOCTypes of grammer - TOC
Types of grammer - TOCAbhayDhupar
 
TOC 4 | Non-deterministic Finite Automata
TOC 4 | Non-deterministic Finite AutomataTOC 4 | Non-deterministic Finite Automata
TOC 4 | Non-deterministic Finite AutomataMohammad 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 ComputationMohammad Imam Hossain
 
Lecture 1,2
Lecture 1,2Lecture 1,2
Lecture 1,2shah zeb
 
CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR Zahid Parvez
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal languageRabia Khalid
 
1.3.2 non deterministic finite automaton
1.3.2 non deterministic finite automaton1.3.2 non deterministic finite automaton
1.3.2 non deterministic finite automatonSampath Kumar S
 
Context Free Grammar
Context Free GrammarContext Free Grammar
Context Free GrammarAkhil Kaushik
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal languageRabia Khalid
 

What's hot (20)

Chomsky & Greibach Normal Forms
Chomsky & Greibach Normal FormsChomsky & Greibach Normal Forms
Chomsky & Greibach Normal Forms
 
CFG to CNF
CFG to CNFCFG to CNF
CFG to CNF
 
Deterministic Finite Automata
Deterministic Finite AutomataDeterministic Finite Automata
Deterministic Finite Automata
 
TOC 2 | Deterministic Finite Automata
TOC 2 | Deterministic Finite AutomataTOC 2 | Deterministic Finite Automata
TOC 2 | Deterministic Finite Automata
 
Finite automata(For college Seminars)
Finite automata(For college Seminars)Finite automata(For college Seminars)
Finite automata(For college Seminars)
 
Intro automata theory
Intro automata theory Intro automata theory
Intro automata theory
 
Pumping lemma Theory Of Automata
Pumping lemma Theory Of AutomataPumping lemma Theory Of Automata
Pumping lemma Theory Of Automata
 
Types of grammer - TOC
Types of grammer - TOCTypes of grammer - TOC
Types of grammer - TOC
 
TOC 4 | Non-deterministic Finite Automata
TOC 4 | Non-deterministic Finite AutomataTOC 4 | Non-deterministic Finite Automata
TOC 4 | Non-deterministic Finite Automata
 
TOC 9 | Pushdown Automata
TOC 9 | Pushdown AutomataTOC 9 | Pushdown Automata
TOC 9 | Pushdown Automata
 
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
 
Lecture 1,2
Lecture 1,2Lecture 1,2
Lecture 1,2
 
CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal language
 
1.3.2 non deterministic finite automaton
1.3.2 non deterministic finite automaton1.3.2 non deterministic finite automaton
1.3.2 non deterministic finite automaton
 
TOC 6 | CFG Design
TOC 6 | CFG DesignTOC 6 | CFG Design
TOC 6 | CFG Design
 
Context free grammar
Context free grammarContext free grammar
Context free grammar
 
Context Free Grammar
Context Free GrammarContext Free Grammar
Context Free Grammar
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal language
 
pushdown automata
pushdown automatapushdown automata
pushdown automata
 

More from 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 SearchMohammad 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 IntroductionMohammad 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 SchemaMohammad 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 SchemaMohammad 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 QueriesMohammad 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 10 | Turing Machine
TOC 10 | Turing MachineTOC 10 | Turing Machine
TOC 10 | Turing Machine
 
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 3 | Different Operations on DFA
TOC 3 | Different Operations on DFATOC 3 | Different Operations on DFA
TOC 3 | Different Operations on DFA
 

Recently uploaded

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 

Recently uploaded (20)

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 

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