SlideShare a Scribd company logo
CONTEXT FREE
GRAMMAR
M RATNAKAR BABU
Asst.Prof. CSE
Prerequisite
1. At very first session we have seen that the Finite Automata and
importance of Finite State Machines and the construction of
Deterministic Finite Automata and some examples on it.
2. In previous session we have seen that the construction of Regular
Expressions for the given problems and how to generate a Regular
expression from the DFA and vice versa.
3. The above two concepts are required to understand the Context
Free Grammar and the applications of CFG.
DEFINITION
• The Context Free Grammar can be defined as a set denoted by G=(V,T,P,S)
Where
V- set of non-terminals Ex: { P,Q,R…}
T- set of Terminals Ex: {p,q,r …}
P- set of Production rules
Where non-terminal -> non-terminal
non-terminal -> terminal
S- is start symbol.
EXAMPLE
• G=({S},{0,1},P,S)
Where P = { S->0S
S->1S
S->Ꜫ }
Example1 : Construct CFG which consists of all the strings having atleast one
occurrence of ‘000’ over input {0,1}.
• Solution:
We need to construct the equivalent R.E for the given problem
first.
The R.E=(any number of 0’s or 1’s) 000 (any number of 0’s or 1’s)
Thus R.E = (0+1)*000(0+1)*
Now let us build the production rules to satisfy the above R.E
{ S->ATA
A->0A|1A|Ꜫ
T->000 }
Excercise1: Find the CFG for the odd length of strings in{a,b}*
with middle symbol a
Excercise2: Generate the a CFG to obtain balanced set of
parenthesis(i.e. every left parenthesis should match with
corresponding right parenthesis)
Derivation Trees
• Definition: Derivation tree is a graphical representation for the
derivation of the given production rules for a given CFG.
• It is the simple way to show how the derivation can be done to obtain
some string from given set of production rules.
• The derivation tree is also called as Parse tree.
Properties of Derivation Tree:-
1. The root node is always a node indicating start symbol.
2. The derivation is read from left to right.
3. The leaf nodes are always terminal nodes.
4. The interior nodes are always the non-terminal nodes.
Example2: The CFG is given as S->bSb | a | b is a production rule. The S is
a start symbol.
• Solution :
The Derivation tree for deriving a string bbabb as follows.
S
b S b
b S b
a
By Simply reading the leaf nodes we can obtain the desired string.
Exercise : Construct the derivation tree for the string ‘aabbabba’ from
the CFG given by
S->aB|bA
A->a|aS|bAA
B->b|bS|aBB
Difference between Regular Grammar and Context Free Grammar
S.No. REGULAR GRAMMAR CONTEXT FREE GRAMMAR
1 Regular grammar is Defined as G=(V,T,P,S)
where V- set of Non-Terminals,
T- set of Terminals, P- set of Production rules, S-
start Symbol
Hence G be a regular Grammar.
The Context free grammar is defined as follows G=(V,T,P,S)
where V- set of Non-Terminals,
T- set of Terminals, P- set of Production rules, S- start Symbol
Hence G be a CFG.
2 Using this grammar we can draw DFA Using this we can construct Push Down Automata(PDA)
3 Compiler can not make use of this for Parsing. Compiler makes use of this for Parsing.
4 The grammar accepts small set of languages.
Hence it is type-3 grammar.
The grammar accepts a large set of languages. Hence it is
type-2 grammar.
5 This form of grammar can be represented by left
linear or right linear grammar
This form of grammar can be represented as nonterminal->
set of terminals and non terminals.
6 Every regular grammar is context free grammar. Every Context Free Grammar can not be Regular
7 For Ex: S->aA|B
A->aA|a
B->bB|b
For Example:
S->aSb|bSb|Ꜫ
8 Syntax of any programming language can not be
represented completely by regular grammar
Syntax of any programming language can be represented
completely by regular grammar
MINIMIZATION OF CFG
• The properties of reduced grammar are as follows.
1. Each variable(i.e. non terminal) and each terminal of G appears in the
derivation of some word in L.
2. There should not be any production as X->Y where X and Y are non
terminal.
3. If Ꜫ is not in the language L then there need not be the production X-> Ꜫ.
The Reduction of Grammar as follows
Removal of Useless
symbols
Elimination of
Ꜫ Productions
Removal of unit
production
Removal of useless symbols
• Any symbol is not useful when it appears on R.H.S. in the production rule. If no
such derivation exists then it is supposed to be a useless symbol.
For Example:
G= (V,T,P,S) where V={S,T,X} T={0,1}
S->0T|1T|X|0|1
T->00
In the above grammar S->X there is no further rule as a definition to X. Hence we
can declare X is a useless symbol. Therefore we can remove the S-> X production
from the given grammar.
After removal of useless symbol form the given grammar the Grammar should be
as follows.
G= (V,T,P,S) where V={S,T} T={0,1}
S->0T|1T|0|1
T->00
Excercise1: Eliminate the useless symbols from the following grammar
S->aS|A|C
A->a
B->aa
C->aCb
Exercise 2: Eliminate the useless symbols from the given grammar.
S->aA|a|Bb|cC
A->aB
B->a|Aa
C->cCD
D->ddd
Elimination of Ꜫ production
• In CFG if there is Ꜫ production we can remove it ,without changing the
meaning of the grammar.
Example:
S->0S|1S| Ꜫ
from the above Grammar we can remove Ꜫ production without
changing the meaning of it.
Simply if we place S-> Ꜫ in other rules.
we get S->0 and S->1 Hence we can rewrite the productions as
follows
S->0S|1S|0|1
Thus we can remove Ꜫ production from the given grammar
Example2: For the CFG given below remove Ꜫ production
S->aSa
S->bSb
S-> Ꜫ
• Solution:
According to the replacement procedure we will place Ꜫ at S in
the sentential form.
S->aSa if S= Ꜫ
S->aa
Similarly if
S->bSb if S= Ꜫ
S->bb
Thus finally rules are
S->aSa|bSb|aa|bb
Thus we can remove Ꜫ production from the given grammar.
Excercise1: Eliminate Ꜫ productions from the CFG given below
A->0B1|1B1
B->0B|1B|Ꜫ
Exercise 2: Construct the CFG without Ꜫ productions form the given
Grammar.
S->a|Ab|aBa
A->b|Ꜫ
B->b|A
Removal of Unit Productions
• The unit productions are the productions in which one non terminal
gives another non terminal.
For example X->Y, Y->Z, Z->X are the unit productions.
Example 1: if CFG is as below
S->0A|1B|C
A->0S|00
B->1|A
C->01 then remove the unit productions.
Solution:
Clearly S->C is a unit production. But while removing it we have
to consider what C gives. So, we add a rule to S.
S->0A|1B|01
Example 1(2):
• Similarly B->A is also unit production so we can modify it as
B->1|0S|00
Thus finally we can write CFG without unit productions as
S->0A|1B|01
A->0S|00
B->1|0S|00
C->01
Exercise: Eliminate the unit productions fro the Grammar
S->AB
A->a
B->C|b
C->D
D->E|bC
E->d|Ab
Next Class:
•We will discuss about the Push Down Automata.
•Conversion of CFG to PDA and some exercises.
•Construction of CFG from PDA some exercises.
End For Now
Continue in Next Class.
Solution:
Back
a or b a or ba
Length n Length n
Thus the R.E = (a+b)n a (a+b)n Where n>=0
As the above given expression doesn’t represent regular language, The DFA can
not be designed for representing such language.
The CFG G= (V,T,P,S)
Where
V={S}
T={a,b}
P={ S->aSa|bSb|aSb|bSa|a}
S is Start Symbol.
Solution:
Back
The well formed parenthesis means whenever opening parentheses is
present there should be a closing parentheses associated with it.
Any expression must begin with opening bracket .We will consider
T={(,[,),]} as set of brackets.
The CFG G=(V,T,P,S)
Where V={S}
T={(,[,),]}
P={ S->SS|()|[]|(S)|[S]
Where S is Start Symbol.
Solution :
• To draw a tree we will first try to
obtain derivation for the string
aabbabba.
S
aB S->aB
a aBB B->aBB
aa bS B B->bS
aab bA B S->bA
aabba B A->a
aabba bS B->bS
aabbab bA S->bA
aabbabba A->a
• Now let us draw a tree
S
a B
a B B
b S b S
b A b A
a a
Back
Solution:
• We will first write all the productions that are giving terminal symbols.
A->a
B->aa
But if we look at start symbol S then
S->aS|A|C
There is no production for B from start symbol. Thus B become a useless
symbol.
Similarly, S->C->aCb->aaCbb->….
There is no terminating symbol for C, Therefore we will eliminate C.
Then the set of rules are
S->aS|A
A->a
Back
Solution:
• Consider all the productions that are giving terminal symbols
S->a
B->a
D->ddd
Now Consider
S->cC
C->cCD
D->ddd.
We will try to derive the string using production rule C we get ,
S->cC->ccCddd->cccCDddd->….
We will not get any terminal symbol for C. Thus we can eliminate C production . To reach D , Cis the only
production, As C is eliminated there is no point of D production. Hence , D is also eliminated.
Therefore,
S->aA|a|Bb
A->aB
B->a|Aa
Is the reduced Grammar.
Back
Solution:
• Now the Ꜫ production is B->Ꜫ we will delete this production, And then add
the production having B replaced by Ꜫ
A->0B1 if B=Ꜫ
A->01
Similarly, A->1B1->11
A->0B1|1B1|01|11
Similarly,
B->0B->0
B->1B->1
B->0B|1B |0|1
Collectively we can write
A->0B1|1B1|01|11
B->0B|1B|0|1
Back
Solution:
• As S->AB and A->a
There is only one rule with A which is giving terminal symbol. Hence there is no Unit production with A.
Now Consider,
B->C
C->D
D->E
It is clear that B,C and D are Unit Productions. As E->d|Ab, we will replace value of D. Then
D->d|Ab|bC
Similarly, a C->D we can write
C->d|Ab|bC
Hence B->d|Ab|bC|b
Thus the grammar after removing unit productions
S->AB
A->a
B->d|Ab|bC|b
C->d|Ab|bC
Back
Solution:

More Related Content

What's hot

Introduction TO Finite Automata
Introduction TO Finite AutomataIntroduction TO Finite Automata
Introduction TO Finite Automata
Ratnakar Mikkili
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
Mahbubur Rahman
 
Strassen's matrix multiplication
Strassen's matrix multiplicationStrassen's matrix multiplication
Strassen's matrix multiplication
Megha V
 
Chomsky classification of Language
Chomsky classification of LanguageChomsky classification of Language
Chomsky classification of Language
Dipankar Boruah
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
Muhammed Afsal Villan
 
Finite Automata
Finite AutomataFinite Automata
Finite Automata
Mukesh Tekwani
 
Theory of Automata
Theory of AutomataTheory of Automata
Theory of Automata
Farooq Mian
 
Context free grammars
Context free grammarsContext free grammars
Context free grammarsRonak Thakkar
 
Recurrence relations
Recurrence relationsRecurrence relations
Recurrence relations
IIUM
 
Chomsky Normal Form
Chomsky Normal FormChomsky Normal Form
Chomsky Normal Form
Jasmine Peniel
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
Ratnakar Mikkili
 
Chomsky Normal Form
Chomsky Normal FormChomsky Normal Form
Chomsky Normal Form
Dhrumil Panchal
 
3.5 equivalence of pushdown automata and cfl
3.5 equivalence of pushdown automata and cfl3.5 equivalence of pushdown automata and cfl
3.5 equivalence of pushdown automata and cfl
Sampath Kumar S
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
MAHASREEM
 
Kleene's theorem
Kleene's theoremKleene's theorem
Kleene's theorem
Samita Mukesh
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
Protap Mondal
 
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
 
Context free grammar
Context free grammarContext free grammar
Context free grammar
Radhakrishnan Chinnusamy
 
Formal Languages and Automata Theory Unit 1
Formal Languages and Automata Theory Unit 1Formal Languages and Automata Theory Unit 1
Formal Languages and Automata Theory Unit 1
Srimatre K
 
Regular expressions-Theory of computation
Regular expressions-Theory of computationRegular expressions-Theory of computation
Regular expressions-Theory of computation
Bipul Roy Bpl
 

What's hot (20)

Introduction TO Finite Automata
Introduction TO Finite AutomataIntroduction TO Finite Automata
Introduction TO Finite Automata
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
 
Strassen's matrix multiplication
Strassen's matrix multiplicationStrassen's matrix multiplication
Strassen's matrix multiplication
 
Chomsky classification of Language
Chomsky classification of LanguageChomsky classification of Language
Chomsky classification of Language
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
 
Finite Automata
Finite AutomataFinite Automata
Finite Automata
 
Theory of Automata
Theory of AutomataTheory of Automata
Theory of Automata
 
Context free grammars
Context free grammarsContext free grammars
Context free grammars
 
Recurrence relations
Recurrence relationsRecurrence relations
Recurrence relations
 
Chomsky Normal Form
Chomsky Normal FormChomsky Normal Form
Chomsky Normal Form
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Chomsky Normal Form
Chomsky Normal FormChomsky Normal Form
Chomsky Normal Form
 
3.5 equivalence of pushdown automata and cfl
3.5 equivalence of pushdown automata and cfl3.5 equivalence of pushdown automata and cfl
3.5 equivalence of pushdown automata and cfl
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
 
Kleene's theorem
Kleene's theoremKleene's theorem
Kleene's theorem
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
TOC 3 | Different Operations on DFA
TOC 3 | Different Operations on DFATOC 3 | Different Operations on DFA
TOC 3 | Different Operations on DFA
 
Context free grammar
Context free grammarContext free grammar
Context free grammar
 
Formal Languages and Automata Theory Unit 1
Formal Languages and Automata Theory Unit 1Formal Languages and Automata Theory Unit 1
Formal Languages and Automata Theory Unit 1
 
Regular expressions-Theory of computation
Regular expressions-Theory of computationRegular expressions-Theory of computation
Regular expressions-Theory of computation
 

Similar to Context free grammar

Theory of competition topic simplification of cfg, normal form of FG.pptx
Theory of competition topic simplification of cfg, normal form of FG.pptxTheory of competition topic simplification of cfg, normal form of FG.pptx
Theory of competition topic simplification of cfg, normal form of FG.pptx
Jisock
 
CNF & Leftmost Derivation - Theory of Computation
CNF & Leftmost Derivation - Theory of ComputationCNF & Leftmost Derivation - Theory of Computation
CNF & Leftmost Derivation - Theory of Computation
Drishti Bhalla
 
CFG to CNF
CFG to CNFCFG to CNF
CFG to CNF
Zain Ul Abiden
 
Instaduction to instaparse
Instaduction to instaparseInstaduction to instaparse
Instaduction to instaparse
Alex Schoof
 
Chomsky & Greibach Normal Forms
Chomsky & Greibach Normal FormsChomsky & Greibach Normal Forms
Chomsky & Greibach Normal Forms
Rajendran
 
Automata theory - CFG and normal forms
Automata theory - CFG and normal formsAutomata theory - CFG and normal forms
Automata theory - CFG and normal forms
Akila Krishnamoorthy
 
Theory of Computation Basic Concepts and Grammar
Theory of Computation Basic Concepts and GrammarTheory of Computation Basic Concepts and Grammar
Theory of Computation Basic Concepts and Grammar
Rushabh2428
 
Theory of computation Lecture Slide(Chomsky Normal Form).pptx
Theory of computation Lecture Slide(Chomsky Normal Form).pptxTheory of computation Lecture Slide(Chomsky Normal Form).pptx
Theory of computation Lecture Slide(Chomsky Normal Form).pptx
customersupport14
 
Chomsky by zeeshan khan and Raheel Khan
Chomsky by zeeshan khan and Raheel KhanChomsky by zeeshan khan and Raheel Khan
Chomsky by zeeshan khan and Raheel Khan
M Khan
 
Lefmost rightmost TOC.pptx
Lefmost rightmost TOC.pptxLefmost rightmost TOC.pptx
Lefmost rightmost TOC.pptx
Jisock
 
Lex analysis
Lex analysisLex analysis
Lex analysis
Suhit Kulkarni
 
PARSING.ppt
PARSING.pptPARSING.ppt
PARSING.ppt
ayyankhanna6480086
 
Lecture 3 RE NFA DFA
Lecture 3   RE NFA DFA Lecture 3   RE NFA DFA
Lecture 3 RE NFA DFA
Rebaz Najeeb
 
CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR
Zahid Parvez
 
Theory of Computation FSM Grammar Minimisation and Normal Forms
Theory of Computation FSM Grammar Minimisation and Normal FormsTheory of Computation FSM Grammar Minimisation and Normal Forms
Theory of Computation FSM Grammar Minimisation and Normal Forms
Rushabh2428
 
NORMAL-FORMS.ppt
NORMAL-FORMS.pptNORMAL-FORMS.ppt
NORMAL-FORMS.ppt
SadagopanS
 
NORMAL-FORMS.ppt
NORMAL-FORMS.pptNORMAL-FORMS.ppt
NORMAL-FORMS.ppt
ssuser47f7f2
 
Simplifies and normal forms - Theory of Computation
Simplifies and normal forms - Theory of ComputationSimplifies and normal forms - Theory of Computation
Simplifies and normal forms - Theory of Computation
Nikhil Pandit
 
Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLP
kartikaVashisht
 
TOC_Solutions-Adi.pdf
TOC_Solutions-Adi.pdfTOC_Solutions-Adi.pdf
TOC_Solutions-Adi.pdf
Prof. Dr. K. Adisesha
 

Similar to Context free grammar (20)

Theory of competition topic simplification of cfg, normal form of FG.pptx
Theory of competition topic simplification of cfg, normal form of FG.pptxTheory of competition topic simplification of cfg, normal form of FG.pptx
Theory of competition topic simplification of cfg, normal form of FG.pptx
 
CNF & Leftmost Derivation - Theory of Computation
CNF & Leftmost Derivation - Theory of ComputationCNF & Leftmost Derivation - Theory of Computation
CNF & Leftmost Derivation - Theory of Computation
 
CFG to CNF
CFG to CNFCFG to CNF
CFG to CNF
 
Instaduction to instaparse
Instaduction to instaparseInstaduction to instaparse
Instaduction to instaparse
 
Chomsky & Greibach Normal Forms
Chomsky & Greibach Normal FormsChomsky & Greibach Normal Forms
Chomsky & Greibach Normal Forms
 
Automata theory - CFG and normal forms
Automata theory - CFG and normal formsAutomata theory - CFG and normal forms
Automata theory - CFG and normal forms
 
Theory of Computation Basic Concepts and Grammar
Theory of Computation Basic Concepts and GrammarTheory of Computation Basic Concepts and Grammar
Theory of Computation Basic Concepts and Grammar
 
Theory of computation Lecture Slide(Chomsky Normal Form).pptx
Theory of computation Lecture Slide(Chomsky Normal Form).pptxTheory of computation Lecture Slide(Chomsky Normal Form).pptx
Theory of computation Lecture Slide(Chomsky Normal Form).pptx
 
Chomsky by zeeshan khan and Raheel Khan
Chomsky by zeeshan khan and Raheel KhanChomsky by zeeshan khan and Raheel Khan
Chomsky by zeeshan khan and Raheel Khan
 
Lefmost rightmost TOC.pptx
Lefmost rightmost TOC.pptxLefmost rightmost TOC.pptx
Lefmost rightmost TOC.pptx
 
Lex analysis
Lex analysisLex analysis
Lex analysis
 
PARSING.ppt
PARSING.pptPARSING.ppt
PARSING.ppt
 
Lecture 3 RE NFA DFA
Lecture 3   RE NFA DFA Lecture 3   RE NFA DFA
Lecture 3 RE NFA DFA
 
CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR
 
Theory of Computation FSM Grammar Minimisation and Normal Forms
Theory of Computation FSM Grammar Minimisation and Normal FormsTheory of Computation FSM Grammar Minimisation and Normal Forms
Theory of Computation FSM Grammar Minimisation and Normal Forms
 
NORMAL-FORMS.ppt
NORMAL-FORMS.pptNORMAL-FORMS.ppt
NORMAL-FORMS.ppt
 
NORMAL-FORMS.ppt
NORMAL-FORMS.pptNORMAL-FORMS.ppt
NORMAL-FORMS.ppt
 
Simplifies and normal forms - Theory of Computation
Simplifies and normal forms - Theory of ComputationSimplifies and normal forms - Theory of Computation
Simplifies and normal forms - Theory of Computation
 
Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLP
 
TOC_Solutions-Adi.pdf
TOC_Solutions-Adi.pdfTOC_Solutions-Adi.pdf
TOC_Solutions-Adi.pdf
 

More from Ratnakar Mikkili

Exception Handling notes in java exception
Exception Handling notes in java exceptionException Handling notes in java exception
Exception Handling notes in java exception
Ratnakar Mikkili
 
AI-State Space Representation.pptx
AI-State Space Representation.pptxAI-State Space Representation.pptx
AI-State Space Representation.pptx
Ratnakar Mikkili
 
AI-State Space Representation.pptx
AI-State Space Representation.pptxAI-State Space Representation.pptx
AI-State Space Representation.pptx
Ratnakar Mikkili
 
Artificial Intelligence_Searching.pptx
Artificial Intelligence_Searching.pptxArtificial Intelligence_Searching.pptx
Artificial Intelligence_Searching.pptx
Ratnakar Mikkili
 
Artificial Intelligence_Environment.pptx
Artificial Intelligence_Environment.pptxArtificial Intelligence_Environment.pptx
Artificial Intelligence_Environment.pptx
Ratnakar Mikkili
 
2_2Specification of Tokens.ppt
2_2Specification of Tokens.ppt2_2Specification of Tokens.ppt
2_2Specification of Tokens.ppt
Ratnakar Mikkili
 
2_4 Finite Automata.ppt
2_4 Finite Automata.ppt2_4 Finite Automata.ppt
2_4 Finite Automata.ppt
Ratnakar Mikkili
 
Push down automata
Push down automataPush down automata
Push down automata
Ratnakar Mikkili
 

More from Ratnakar Mikkili (8)

Exception Handling notes in java exception
Exception Handling notes in java exceptionException Handling notes in java exception
Exception Handling notes in java exception
 
AI-State Space Representation.pptx
AI-State Space Representation.pptxAI-State Space Representation.pptx
AI-State Space Representation.pptx
 
AI-State Space Representation.pptx
AI-State Space Representation.pptxAI-State Space Representation.pptx
AI-State Space Representation.pptx
 
Artificial Intelligence_Searching.pptx
Artificial Intelligence_Searching.pptxArtificial Intelligence_Searching.pptx
Artificial Intelligence_Searching.pptx
 
Artificial Intelligence_Environment.pptx
Artificial Intelligence_Environment.pptxArtificial Intelligence_Environment.pptx
Artificial Intelligence_Environment.pptx
 
2_2Specification of Tokens.ppt
2_2Specification of Tokens.ppt2_2Specification of Tokens.ppt
2_2Specification of Tokens.ppt
 
2_4 Finite Automata.ppt
2_4 Finite Automata.ppt2_4 Finite Automata.ppt
2_4 Finite Automata.ppt
 
Push down automata
Push down automataPush down automata
Push down automata
 

Recently uploaded

一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
AmarGB2
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
Jayaprasanna4
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
Pipe Restoration Solutions
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
Runway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptxRunway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptx
SupreethSP4
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
thanhdowork
 

Recently uploaded (20)

一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
Runway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptxRunway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptx
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
 

Context free grammar

  • 1. CONTEXT FREE GRAMMAR M RATNAKAR BABU Asst.Prof. CSE
  • 2. Prerequisite 1. At very first session we have seen that the Finite Automata and importance of Finite State Machines and the construction of Deterministic Finite Automata and some examples on it. 2. In previous session we have seen that the construction of Regular Expressions for the given problems and how to generate a Regular expression from the DFA and vice versa. 3. The above two concepts are required to understand the Context Free Grammar and the applications of CFG.
  • 3. DEFINITION • The Context Free Grammar can be defined as a set denoted by G=(V,T,P,S) Where V- set of non-terminals Ex: { P,Q,R…} T- set of Terminals Ex: {p,q,r …} P- set of Production rules Where non-terminal -> non-terminal non-terminal -> terminal S- is start symbol.
  • 4. EXAMPLE • G=({S},{0,1},P,S) Where P = { S->0S S->1S S->Ꜫ }
  • 5. Example1 : Construct CFG which consists of all the strings having atleast one occurrence of ‘000’ over input {0,1}. • Solution: We need to construct the equivalent R.E for the given problem first. The R.E=(any number of 0’s or 1’s) 000 (any number of 0’s or 1’s) Thus R.E = (0+1)*000(0+1)* Now let us build the production rules to satisfy the above R.E { S->ATA A->0A|1A|Ꜫ T->000 }
  • 6. Excercise1: Find the CFG for the odd length of strings in{a,b}* with middle symbol a Excercise2: Generate the a CFG to obtain balanced set of parenthesis(i.e. every left parenthesis should match with corresponding right parenthesis)
  • 7. Derivation Trees • Definition: Derivation tree is a graphical representation for the derivation of the given production rules for a given CFG. • It is the simple way to show how the derivation can be done to obtain some string from given set of production rules. • The derivation tree is also called as Parse tree. Properties of Derivation Tree:- 1. The root node is always a node indicating start symbol. 2. The derivation is read from left to right. 3. The leaf nodes are always terminal nodes. 4. The interior nodes are always the non-terminal nodes.
  • 8. Example2: The CFG is given as S->bSb | a | b is a production rule. The S is a start symbol. • Solution : The Derivation tree for deriving a string bbabb as follows. S b S b b S b a By Simply reading the leaf nodes we can obtain the desired string.
  • 9. Exercise : Construct the derivation tree for the string ‘aabbabba’ from the CFG given by S->aB|bA A->a|aS|bAA B->b|bS|aBB
  • 10. Difference between Regular Grammar and Context Free Grammar S.No. REGULAR GRAMMAR CONTEXT FREE GRAMMAR 1 Regular grammar is Defined as G=(V,T,P,S) where V- set of Non-Terminals, T- set of Terminals, P- set of Production rules, S- start Symbol Hence G be a regular Grammar. The Context free grammar is defined as follows G=(V,T,P,S) where V- set of Non-Terminals, T- set of Terminals, P- set of Production rules, S- start Symbol Hence G be a CFG. 2 Using this grammar we can draw DFA Using this we can construct Push Down Automata(PDA) 3 Compiler can not make use of this for Parsing. Compiler makes use of this for Parsing. 4 The grammar accepts small set of languages. Hence it is type-3 grammar. The grammar accepts a large set of languages. Hence it is type-2 grammar. 5 This form of grammar can be represented by left linear or right linear grammar This form of grammar can be represented as nonterminal-> set of terminals and non terminals. 6 Every regular grammar is context free grammar. Every Context Free Grammar can not be Regular 7 For Ex: S->aA|B A->aA|a B->bB|b For Example: S->aSb|bSb|Ꜫ 8 Syntax of any programming language can not be represented completely by regular grammar Syntax of any programming language can be represented completely by regular grammar
  • 11. MINIMIZATION OF CFG • The properties of reduced grammar are as follows. 1. Each variable(i.e. non terminal) and each terminal of G appears in the derivation of some word in L. 2. There should not be any production as X->Y where X and Y are non terminal. 3. If Ꜫ is not in the language L then there need not be the production X-> Ꜫ.
  • 12. The Reduction of Grammar as follows Removal of Useless symbols Elimination of Ꜫ Productions Removal of unit production
  • 13. Removal of useless symbols • Any symbol is not useful when it appears on R.H.S. in the production rule. If no such derivation exists then it is supposed to be a useless symbol. For Example: G= (V,T,P,S) where V={S,T,X} T={0,1} S->0T|1T|X|0|1 T->00 In the above grammar S->X there is no further rule as a definition to X. Hence we can declare X is a useless symbol. Therefore we can remove the S-> X production from the given grammar. After removal of useless symbol form the given grammar the Grammar should be as follows. G= (V,T,P,S) where V={S,T} T={0,1} S->0T|1T|0|1 T->00
  • 14. Excercise1: Eliminate the useless symbols from the following grammar S->aS|A|C A->a B->aa C->aCb Exercise 2: Eliminate the useless symbols from the given grammar. S->aA|a|Bb|cC A->aB B->a|Aa C->cCD D->ddd
  • 15. Elimination of Ꜫ production • In CFG if there is Ꜫ production we can remove it ,without changing the meaning of the grammar. Example: S->0S|1S| Ꜫ from the above Grammar we can remove Ꜫ production without changing the meaning of it. Simply if we place S-> Ꜫ in other rules. we get S->0 and S->1 Hence we can rewrite the productions as follows S->0S|1S|0|1 Thus we can remove Ꜫ production from the given grammar
  • 16. Example2: For the CFG given below remove Ꜫ production S->aSa S->bSb S-> Ꜫ • Solution: According to the replacement procedure we will place Ꜫ at S in the sentential form. S->aSa if S= Ꜫ S->aa Similarly if S->bSb if S= Ꜫ S->bb Thus finally rules are S->aSa|bSb|aa|bb Thus we can remove Ꜫ production from the given grammar.
  • 17. Excercise1: Eliminate Ꜫ productions from the CFG given below A->0B1|1B1 B->0B|1B|Ꜫ Exercise 2: Construct the CFG without Ꜫ productions form the given Grammar. S->a|Ab|aBa A->b|Ꜫ B->b|A
  • 18. Removal of Unit Productions • The unit productions are the productions in which one non terminal gives another non terminal. For example X->Y, Y->Z, Z->X are the unit productions. Example 1: if CFG is as below S->0A|1B|C A->0S|00 B->1|A C->01 then remove the unit productions. Solution: Clearly S->C is a unit production. But while removing it we have to consider what C gives. So, we add a rule to S. S->0A|1B|01
  • 19. Example 1(2): • Similarly B->A is also unit production so we can modify it as B->1|0S|00 Thus finally we can write CFG without unit productions as S->0A|1B|01 A->0S|00 B->1|0S|00 C->01
  • 20. Exercise: Eliminate the unit productions fro the Grammar S->AB A->a B->C|b C->D D->E|bC E->d|Ab
  • 21. Next Class: •We will discuss about the Push Down Automata. •Conversion of CFG to PDA and some exercises. •Construction of CFG from PDA some exercises.
  • 22. End For Now Continue in Next Class.
  • 23. Solution: Back a or b a or ba Length n Length n Thus the R.E = (a+b)n a (a+b)n Where n>=0 As the above given expression doesn’t represent regular language, The DFA can not be designed for representing such language. The CFG G= (V,T,P,S) Where V={S} T={a,b} P={ S->aSa|bSb|aSb|bSa|a} S is Start Symbol.
  • 24. Solution: Back The well formed parenthesis means whenever opening parentheses is present there should be a closing parentheses associated with it. Any expression must begin with opening bracket .We will consider T={(,[,),]} as set of brackets. The CFG G=(V,T,P,S) Where V={S} T={(,[,),]} P={ S->SS|()|[]|(S)|[S] Where S is Start Symbol.
  • 25. Solution : • To draw a tree we will first try to obtain derivation for the string aabbabba. S aB S->aB a aBB B->aBB aa bS B B->bS aab bA B S->bA aabba B A->a aabba bS B->bS aabbab bA S->bA aabbabba A->a • Now let us draw a tree S a B a B B b S b S b A b A a a Back
  • 26. Solution: • We will first write all the productions that are giving terminal symbols. A->a B->aa But if we look at start symbol S then S->aS|A|C There is no production for B from start symbol. Thus B become a useless symbol. Similarly, S->C->aCb->aaCbb->…. There is no terminating symbol for C, Therefore we will eliminate C. Then the set of rules are S->aS|A A->a Back
  • 27. Solution: • Consider all the productions that are giving terminal symbols S->a B->a D->ddd Now Consider S->cC C->cCD D->ddd. We will try to derive the string using production rule C we get , S->cC->ccCddd->cccCDddd->…. We will not get any terminal symbol for C. Thus we can eliminate C production . To reach D , Cis the only production, As C is eliminated there is no point of D production. Hence , D is also eliminated. Therefore, S->aA|a|Bb A->aB B->a|Aa Is the reduced Grammar. Back
  • 28. Solution: • Now the Ꜫ production is B->Ꜫ we will delete this production, And then add the production having B replaced by Ꜫ A->0B1 if B=Ꜫ A->01 Similarly, A->1B1->11 A->0B1|1B1|01|11 Similarly, B->0B->0 B->1B->1 B->0B|1B |0|1 Collectively we can write A->0B1|1B1|01|11 B->0B|1B|0|1 Back
  • 29. Solution: • As S->AB and A->a There is only one rule with A which is giving terminal symbol. Hence there is no Unit production with A. Now Consider, B->C C->D D->E It is clear that B,C and D are Unit Productions. As E->d|Ab, we will replace value of D. Then D->d|Ab|bC Similarly, a C->D we can write C->d|Ab|bC Hence B->d|Ab|bC|b Thus the grammar after removing unit productions S->AB A->a B->d|Ab|bC|b C->d|Ab|bC Back