SlideShare a Scribd company logo
1 of 24
CONTEXT FREE LANGUAGES
Munib Ahmad
School of Information Technology, Minhaj University,
Lahore
CONTEXT FREE GRAMMAR (CFG)
Context Free Grammar (CFG) is a collection of
three things:
1. An alphabet of letters  called terminals from which
we are going to make strings that will be the words
of a language.
2. A set of symbols called nonterminals, one of which
is the symbol S, standing for "start here“ known as
“Start Symbol”.
3. A finite set of productions of the form one
nonterminal -) finite string of terminals and/or
nonterminals where the strings of terminals and
nonterminals can consist of only terminals or of
only nonterminals, or any mixture of terminals and
nonterminals or even the empty string.
CONTEXT FREE LANGUAGE (CFL)
 The language generated by a CFG is the set
of all strings of terminals that can be
produced from the start symbol S using the
productions as substitutions.
 A language generated by a CFG is called a
context-free language, abbreviated CFL.
EXAMPLES
S  aS can be written as S  aS |A
S  A
L = {A a aa aaa aaaa ……}
S  SS | a|A
L = {A a aa aaa aaaa ……}
S  SS | a
L = {a aa aaa aaaa ……}
S  aS | bS |a|b|A
L={A a b aa ab ba bb …….}
TOTAL LANGUAGE TREE (TLT)
S  aa | bX | aXX
X  ab | b
L = ?
TOTAL LANGUAGE TREE (TLT) …..
S  aSb I bSI a
L={a ba aab bba aaabb ......}
PARSE TREE
S--> aSa | bSb | a | b | A
L={A a b aa bb aaa aba bab bbb
......}
Parse tree uses for membership
verification
word aabaa  L
S  aS | Sa | a
L = {a aa aaa aaaa aaaaa ……..}
If there are multiple parse trees generated for
same input then grammar will be ambiguous
AMBIGUOUS GRAMMAR
REGULAR, NONREGULAR AND CONTEXT FREE
LANGUAGES
REGULAR AND CONTEXT FREE LANGUAGES
Theorem
All regular languages are also context free
languages but not vise versa.
Proof
Method 1
 Regular languages are defined by regular
expression.
 Regular expression can be converted into context
free grammar using following rules:
 Decompose regular expression of language L into sub-
regular expressions and assign them names as
nonterminals in CFG
 Convert all + with either rules option as
 a+b with S  a| b
EXAMPLE
(a + b)*bbb(a + b)*
X Y X
Context Free Grammar
S  XYX
X  aX | bX | A
Y  bbb
ANOTHER EXAMPLE
Regular Expression
((bb+ab+bbb)+(bb+aba)*)*
M N
Context Free Grammar (CFG)
S  OS | A
O  M | N
M  bb | ab | bbb
N  PN | A
P  bb | aba
ANOTHER EXAMPLE
Regular Expression
((aa+ab+ba)*(a+baa+aba)*+aba+(b+ab)*)*
M N O P
Context Free Grammar (CFG)
S  QS | A
Q  MN | O | P
M  RM | A
R  aa | ab | ba
N  TN | A
T  a | baa | aba
O  aba
P  WP | A
W  b | ab
Method 2
 Regular language can be defined with FA.
 FA can be converted into CFG using following
method:
 Assign names to all states of FA; normally S for start
state.
 Rules for CFG will be defined as:
Sourse state name  (transition contents) (target state name)
 For final state, one additional rule will be defined as:
Final state name  A
 Ignore all those states which involve in those paths
which do not converge towards final state.
REGULAR AND CONTEXT FREE LANGUAGES
EXAMPLE
S  aM | bS | A
M  bS | A
ANOTHER EXAMPLE
= {a b c}
S  aM | bS |cS
M  aM | bN | cS
N  aM | bS | cO
O  aP | bO | cO | A
P  aP | bQ | cO | A
Q  aP | bO | cS |A
NONREGULAR AND CONTEXT FREE LANGUAGES
 Some of the nonregular languages can be
defined using Context Free Grammar, so
such nonregular languages which can be
define by CFG are also CFLs.
 For example
 Language of PALINDROME
S  aSa | bSb | a | b
S  aSb | A
UNIT AND LEMBDA PRODUCTIONS
 A production is of the form
Nonterminal  one nonterminal
is called unit production
 A production is of the form
Nonterminal  A
is called lembda production
CHOMSKY NORMAL FORM
If a CFG has only productions of the form
Nonterminal string of two Nonterminals
or of the form
Nonterminal  one terminal
it is said to be in Chomsky Normal Form,
CNF.
EXAMPLE
Language of PALINDROME
S aSa | bSb |a | b | aa | bb |A
Conversion of this CFG into Chomsky Normal
Form
ANOTHER EXAMPLE
S  bA | aB
A  bAA | aS | a
B  aBB | bS | b
Conversion of this CFG into Chomsky Normal Form
S  YA | XB
A  YR1 | XS | a
B  XR2 | YS | b
X  a
Y  b
R1  AA
R2  BB
LEFTMOST AND RIGHTMOST DERIVATIONS
 If a word w is generated by a CFG by a certain
derivation and at each step in the derivation a
rule of production is applied to the leftmost
nonterminal in the working string, then this
derivation is called a leftmost derivation.
 If a word w is generated by a CFG by a certain
derivation and at each step in the derivation a
rule of production is applied to the rightmost
nonterminal in the working string, then this
derivation is called a rightmost derivation.
EXAMPLE
Consider the CFG:
S  aSX | b
X  Xb | a
Leftmost derivation: Rightmost derivation:
S  aSX S  aSX
 aaSXX  aSa
 aabXX  aaSXa
 aabXbX  aaSXba
 aababX  aaSaba
 aababa  aababa
SUMMARY
 Chapter 13, 14, 15, and 16 have been
covered in these slides.

More Related Content

Similar to CS911-Lecture-21_43709.pptx

Conteext-free Grammer
Conteext-free GrammerConteext-free Grammer
Conteext-free GrammerHASHIR RAZA
 
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 ProblemsRushabh2428
 
Chomsky hierarchy
Chomsky hierarchyChomsky hierarchy
Chomsky hierarchySANUC2
 
Syntax Analyzer.pdf
Syntax Analyzer.pdfSyntax Analyzer.pdf
Syntax Analyzer.pdfkenilpatel65
 
Context Free Grammar
Context Free GrammarContext Free Grammar
Context Free GrammarAkhil Kaushik
 
Regular Expression in Compiler design
Regular Expression in Compiler designRegular Expression in Compiler design
Regular Expression in Compiler designRiazul Islam
 
Deterministic context free grammars &non-deterministic
Deterministic context free grammars &non-deterministicDeterministic context free grammars &non-deterministic
Deterministic context free grammars &non-deterministicLeyo Stephen
 
RegularLanguageProperties [Autosaved].pptx
RegularLanguageProperties [Autosaved].pptxRegularLanguageProperties [Autosaved].pptx
RegularLanguageProperties [Autosaved].pptxRaviAr5
 
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.pptxJisock
 
Chapter Three(2)
Chapter Three(2)Chapter Three(2)
Chapter Three(2)bolovv
 
Context free grammars
Context free grammarsContext free grammars
Context free grammarsRonak Thakkar
 
Automata theory - CFG and normal forms
Automata theory - CFG and normal formsAutomata theory - CFG and normal forms
Automata theory - CFG and normal formsAkila Krishnamoorthy
 
Context free languages
Context free languagesContext free languages
Context free languagesJahurul Islam
 
Context free languages
Context free languagesContext free languages
Context free languagesJahurul Islam
 

Similar to CS911-Lecture-21_43709.pptx (20)

Conteext-free Grammer
Conteext-free GrammerConteext-free Grammer
Conteext-free Grammer
 
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
 
Chomsky hierarchy
Chomsky hierarchyChomsky hierarchy
Chomsky hierarchy
 
Syntax Analyzer.pdf
Syntax Analyzer.pdfSyntax Analyzer.pdf
Syntax Analyzer.pdf
 
Context Free Grammar
Context Free GrammarContext Free Grammar
Context Free Grammar
 
Regular Expression in Compiler design
Regular Expression in Compiler designRegular Expression in Compiler design
Regular Expression in Compiler design
 
Deterministic context free grammars &non-deterministic
Deterministic context free grammars &non-deterministicDeterministic context free grammars &non-deterministic
Deterministic context free grammars &non-deterministic
 
Lecture2 B
Lecture2 BLecture2 B
Lecture2 B
 
Context free grammar
Context free grammarContext free grammar
Context free grammar
 
RegularLanguageProperties [Autosaved].pptx
RegularLanguageProperties [Autosaved].pptxRegularLanguageProperties [Autosaved].pptx
RegularLanguageProperties [Autosaved].pptx
 
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
 
Chapter Three(2)
Chapter Three(2)Chapter Three(2)
Chapter Three(2)
 
Context free grammars
Context free grammarsContext free grammars
Context free grammars
 
TOA_WEEK 14.pptx
TOA_WEEK 14.pptxTOA_WEEK 14.pptx
TOA_WEEK 14.pptx
 
TOC question bank.pdf
TOC question bank.pdfTOC question bank.pdf
TOC question bank.pdf
 
Automata theory - CFG and normal forms
Automata theory - CFG and normal formsAutomata theory - CFG and normal forms
Automata theory - CFG and normal forms
 
CFG to CNF
CFG to CNFCFG to CNF
CFG to CNF
 
Cfg part ii
Cfg   part iiCfg   part ii
Cfg part ii
 
Context free languages
Context free languagesContext free languages
Context free languages
 
Context free languages
Context free languagesContext free languages
Context free languages
 

More from AliZaib71

Lecture-31.pptx
Lecture-31.pptxLecture-31.pptx
Lecture-31.pptxAliZaib71
 
Presentation1.pptx
Presentation1.pptxPresentation1.pptx
Presentation1.pptxAliZaib71
 
Computer_Graphics_circle_drawing_techniq.ppt
Computer_Graphics_circle_drawing_techniq.pptComputer_Graphics_circle_drawing_techniq.ppt
Computer_Graphics_circle_drawing_techniq.pptAliZaib71
 
Wireless Networks - CS718 Power Point Slides Lecture 02.ppt
Wireless Networks - CS718 Power Point Slides Lecture 02.pptWireless Networks - CS718 Power Point Slides Lecture 02.ppt
Wireless Networks - CS718 Power Point Slides Lecture 02.pptAliZaib71
 
Web-01-HTTP.pptx
Web-01-HTTP.pptxWeb-01-HTTP.pptx
Web-01-HTTP.pptxAliZaib71
 

More from AliZaib71 (6)

Lecture-31.pptx
Lecture-31.pptxLecture-31.pptx
Lecture-31.pptx
 
Presentation1.pptx
Presentation1.pptxPresentation1.pptx
Presentation1.pptx
 
Computer_Graphics_circle_drawing_techniq.ppt
Computer_Graphics_circle_drawing_techniq.pptComputer_Graphics_circle_drawing_techniq.ppt
Computer_Graphics_circle_drawing_techniq.ppt
 
SE UML.ppt
SE UML.pptSE UML.ppt
SE UML.ppt
 
Wireless Networks - CS718 Power Point Slides Lecture 02.ppt
Wireless Networks - CS718 Power Point Slides Lecture 02.pptWireless Networks - CS718 Power Point Slides Lecture 02.ppt
Wireless Networks - CS718 Power Point Slides Lecture 02.ppt
 
Web-01-HTTP.pptx
Web-01-HTTP.pptxWeb-01-HTTP.pptx
Web-01-HTTP.pptx
 

Recently uploaded

hybrid Seed Production In Chilli & Capsicum.pptx
hybrid Seed Production In Chilli & Capsicum.pptxhybrid Seed Production In Chilli & Capsicum.pptx
hybrid Seed Production In Chilli & Capsicum.pptx9to5mart
 
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangaloreamitlee9823
 
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...amitlee9823
 
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night StandCall Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
➥🔝 7737669865 🔝▻ Bangalore Call-girls in Women Seeking Men 🔝Bangalore🔝 Esc...
➥🔝 7737669865 🔝▻ Bangalore Call-girls in Women Seeking Men  🔝Bangalore🔝   Esc...➥🔝 7737669865 🔝▻ Bangalore Call-girls in Women Seeking Men  🔝Bangalore🔝   Esc...
➥🔝 7737669865 🔝▻ Bangalore Call-girls in Women Seeking Men 🔝Bangalore🔝 Esc...amitlee9823
 
Capstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics ProgramCapstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics ProgramMoniSankarHazra
 
Probability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter LessonsProbability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter LessonsJoseMangaJr1
 
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteedamy56318795
 
👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...
👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...
👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...karishmasinghjnh
 
Midocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxMidocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxolyaivanovalion
 
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...amitlee9823
 
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...amitlee9823
 
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightCheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightDelhi Call girls
 
Detecting Credit Card Fraud: A Machine Learning Approach
Detecting Credit Card Fraud: A Machine Learning ApproachDetecting Credit Card Fraud: A Machine Learning Approach
Detecting Credit Card Fraud: A Machine Learning ApproachBoston Institute of Analytics
 
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24  Building Real-Time Pipelines With FLaNKDATA SUMMIT 24  Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNKTimothy Spann
 
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...only4webmaster01
 
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night StandCall Girls In Attibele ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 

Recently uploaded (20)

hybrid Seed Production In Chilli & Capsicum.pptx
hybrid Seed Production In Chilli & Capsicum.pptxhybrid Seed Production In Chilli & Capsicum.pptx
hybrid Seed Production In Chilli & Capsicum.pptx
 
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
 
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
 
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night StandCall Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
 
➥🔝 7737669865 🔝▻ Bangalore Call-girls in Women Seeking Men 🔝Bangalore🔝 Esc...
➥🔝 7737669865 🔝▻ Bangalore Call-girls in Women Seeking Men  🔝Bangalore🔝   Esc...➥🔝 7737669865 🔝▻ Bangalore Call-girls in Women Seeking Men  🔝Bangalore🔝   Esc...
➥🔝 7737669865 🔝▻ Bangalore Call-girls in Women Seeking Men 🔝Bangalore🔝 Esc...
 
Capstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics ProgramCapstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics Program
 
Probability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter LessonsProbability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter Lessons
 
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
 
👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...
👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...
👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...
 
Midocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxMidocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFx
 
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
 
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get CytotecAbortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
 
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
 
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightCheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
 
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
 
Detecting Credit Card Fraud: A Machine Learning Approach
Detecting Credit Card Fraud: A Machine Learning ApproachDetecting Credit Card Fraud: A Machine Learning Approach
Detecting Credit Card Fraud: A Machine Learning Approach
 
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24  Building Real-Time Pipelines With FLaNKDATA SUMMIT 24  Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNK
 
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
 
Anomaly detection and data imputation within time series
Anomaly detection and data imputation within time seriesAnomaly detection and data imputation within time series
Anomaly detection and data imputation within time series
 
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night StandCall Girls In Attibele ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night Stand
 

CS911-Lecture-21_43709.pptx

  • 1. CONTEXT FREE LANGUAGES Munib Ahmad School of Information Technology, Minhaj University, Lahore
  • 2. CONTEXT FREE GRAMMAR (CFG) Context Free Grammar (CFG) is a collection of three things: 1. An alphabet of letters  called terminals from which we are going to make strings that will be the words of a language. 2. A set of symbols called nonterminals, one of which is the symbol S, standing for "start here“ known as “Start Symbol”. 3. A finite set of productions of the form one nonterminal -) finite string of terminals and/or nonterminals where the strings of terminals and nonterminals can consist of only terminals or of only nonterminals, or any mixture of terminals and nonterminals or even the empty string.
  • 3. CONTEXT FREE LANGUAGE (CFL)  The language generated by a CFG is the set of all strings of terminals that can be produced from the start symbol S using the productions as substitutions.  A language generated by a CFG is called a context-free language, abbreviated CFL.
  • 4. EXAMPLES S  aS can be written as S  aS |A S  A L = {A a aa aaa aaaa ……} S  SS | a|A L = {A a aa aaa aaaa ……} S  SS | a L = {a aa aaa aaaa ……} S  aS | bS |a|b|A L={A a b aa ab ba bb …….}
  • 5. TOTAL LANGUAGE TREE (TLT) S  aa | bX | aXX X  ab | b L = ?
  • 6. TOTAL LANGUAGE TREE (TLT) ….. S  aSb I bSI a L={a ba aab bba aaabb ......}
  • 7. PARSE TREE S--> aSa | bSb | a | b | A L={A a b aa bb aaa aba bab bbb ......} Parse tree uses for membership verification word aabaa  L
  • 8. S  aS | Sa | a L = {a aa aaa aaaa aaaaa ……..} If there are multiple parse trees generated for same input then grammar will be ambiguous AMBIGUOUS GRAMMAR
  • 9. REGULAR, NONREGULAR AND CONTEXT FREE LANGUAGES
  • 10. REGULAR AND CONTEXT FREE LANGUAGES Theorem All regular languages are also context free languages but not vise versa. Proof Method 1  Regular languages are defined by regular expression.  Regular expression can be converted into context free grammar using following rules:  Decompose regular expression of language L into sub- regular expressions and assign them names as nonterminals in CFG  Convert all + with either rules option as  a+b with S  a| b
  • 11. EXAMPLE (a + b)*bbb(a + b)* X Y X Context Free Grammar S  XYX X  aX | bX | A Y  bbb
  • 12. ANOTHER EXAMPLE Regular Expression ((bb+ab+bbb)+(bb+aba)*)* M N Context Free Grammar (CFG) S  OS | A O  M | N M  bb | ab | bbb N  PN | A P  bb | aba
  • 13. ANOTHER EXAMPLE Regular Expression ((aa+ab+ba)*(a+baa+aba)*+aba+(b+ab)*)* M N O P Context Free Grammar (CFG) S  QS | A Q  MN | O | P M  RM | A R  aa | ab | ba N  TN | A T  a | baa | aba O  aba P  WP | A W  b | ab
  • 14. Method 2  Regular language can be defined with FA.  FA can be converted into CFG using following method:  Assign names to all states of FA; normally S for start state.  Rules for CFG will be defined as: Sourse state name  (transition contents) (target state name)  For final state, one additional rule will be defined as: Final state name  A  Ignore all those states which involve in those paths which do not converge towards final state. REGULAR AND CONTEXT FREE LANGUAGES
  • 15. EXAMPLE S  aM | bS | A M  bS | A
  • 16. ANOTHER EXAMPLE = {a b c} S  aM | bS |cS M  aM | bN | cS N  aM | bS | cO O  aP | bO | cO | A P  aP | bQ | cO | A Q  aP | bO | cS |A
  • 17. NONREGULAR AND CONTEXT FREE LANGUAGES  Some of the nonregular languages can be defined using Context Free Grammar, so such nonregular languages which can be define by CFG are also CFLs.  For example  Language of PALINDROME S  aSa | bSb | a | b S  aSb | A
  • 18. UNIT AND LEMBDA PRODUCTIONS  A production is of the form Nonterminal  one nonterminal is called unit production  A production is of the form Nonterminal  A is called lembda production
  • 19. CHOMSKY NORMAL FORM If a CFG has only productions of the form Nonterminal string of two Nonterminals or of the form Nonterminal  one terminal it is said to be in Chomsky Normal Form, CNF.
  • 20. EXAMPLE Language of PALINDROME S aSa | bSb |a | b | aa | bb |A Conversion of this CFG into Chomsky Normal Form
  • 21. ANOTHER EXAMPLE S  bA | aB A  bAA | aS | a B  aBB | bS | b Conversion of this CFG into Chomsky Normal Form S  YA | XB A  YR1 | XS | a B  XR2 | YS | b X  a Y  b R1  AA R2  BB
  • 22. LEFTMOST AND RIGHTMOST DERIVATIONS  If a word w is generated by a CFG by a certain derivation and at each step in the derivation a rule of production is applied to the leftmost nonterminal in the working string, then this derivation is called a leftmost derivation.  If a word w is generated by a CFG by a certain derivation and at each step in the derivation a rule of production is applied to the rightmost nonterminal in the working string, then this derivation is called a rightmost derivation.
  • 23. EXAMPLE Consider the CFG: S  aSX | b X  Xb | a Leftmost derivation: Rightmost derivation: S  aSX S  aSX  aaSXX  aSa  aabXX  aaSXa  aabXbX  aaSXba  aababX  aaSaba  aababa  aababa
  • 24. SUMMARY  Chapter 13, 14, 15, and 16 have been covered in these slides.