SlideShare a Scribd company logo
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 Grammer
HASHIR 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 Problems
Rushabh2428
 
Chomsky hierarchy
Chomsky hierarchyChomsky hierarchy
Chomsky hierarchy
SANUC2
 
Syntax Analyzer.pdf
Syntax Analyzer.pdfSyntax Analyzer.pdf
Syntax Analyzer.pdf
kenilpatel65
 
Context Free Grammar
Context Free GrammarContext Free Grammar
Context Free Grammar
Akhil Kaushik
 
Regular Expression in Compiler design
Regular Expression in Compiler designRegular Expression in Compiler design
Regular Expression in Compiler design
Riazul Islam
 
Deterministic context free grammars &non-deterministic
Deterministic context free grammars &non-deterministicDeterministic context free grammars &non-deterministic
Deterministic context free grammars &non-deterministic
Leyo Stephen
 
Context free grammar
Context free grammarContext free grammar
Context free grammar
Radhakrishnan Chinnusamy
 
RegularLanguageProperties [Autosaved].pptx
RegularLanguageProperties [Autosaved].pptxRegularLanguageProperties [Autosaved].pptx
RegularLanguageProperties [Autosaved].pptx
RaviAr5
 
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
 
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
 
TOA_WEEK 14.pptx
TOA_WEEK 14.pptxTOA_WEEK 14.pptx
TOA_WEEK 14.pptx
Kamran Zafar
 
TOC question bank.pdf
TOC question bank.pdfTOC question bank.pdf
TOC question bank.pdf
MrRRajasekarCSE
 
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
 
CFG to CNF
CFG to CNFCFG to CNF
CFG to CNF
Zain Ul Abiden
 
Cfg part ii
Cfg   part iiCfg   part ii
Cfg part ii
Kashif Ali
 
Context free languages
Context free languagesContext free languages
Context free languages
Jahurul Islam
 
Context free languages
Context free languagesContext free languages
Context free languages
Jahurul 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.pptx
AliZaib71
 
Presentation1.pptx
Presentation1.pptxPresentation1.pptx
Presentation1.pptx
AliZaib71
 
Computer_Graphics_circle_drawing_techniq.ppt
Computer_Graphics_circle_drawing_techniq.pptComputer_Graphics_circle_drawing_techniq.ppt
Computer_Graphics_circle_drawing_techniq.ppt
AliZaib71
 
SE UML.ppt
SE UML.pptSE UML.ppt
SE UML.ppt
AliZaib71
 
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
AliZaib71
 
Web-01-HTTP.pptx
Web-01-HTTP.pptxWeb-01-HTTP.pptx
Web-01-HTTP.pptx
AliZaib71
 

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

一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
slg6lamcq
 
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
u86oixdj
 
Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)
TravisMalana
 
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
ahzuo
 
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
NABLAS株式会社
 
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
vcaxypu
 
社内勉強会資料_LLM Agents                              .
社内勉強会資料_LLM Agents                              .社内勉強会資料_LLM Agents                              .
社内勉強会資料_LLM Agents                              .
NABLAS株式会社
 
Empowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptxEmpowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptx
benishzehra469
 
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
Tiktokethiodaily
 
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
mbawufebxi
 
standardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghhstandardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghh
ArpitMalhotra16
 
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
nscud
 
Opendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptxOpendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptx
Opendatabay
 
FP Growth Algorithm and its Applications
FP Growth Algorithm and its ApplicationsFP Growth Algorithm and its Applications
FP Growth Algorithm and its Applications
MaleehaSheikh2
 
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Subhajit Sahu
 
Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...
AbhimanyuSinha9
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
jerlynmaetalle
 
Q1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year ReboundQ1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year Rebound
Oppotus
 
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project PresentationPredicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Boston Institute of Analytics
 
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdfCh03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
haila53
 

Recently uploaded (20)

一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
 
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
 
Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)
 
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
 
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
 
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
 
社内勉強会資料_LLM Agents                              .
社内勉強会資料_LLM Agents                              .社内勉強会資料_LLM Agents                              .
社内勉強会資料_LLM Agents                              .
 
Empowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptxEmpowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptx
 
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
 
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
 
standardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghhstandardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghh
 
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
 
Opendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptxOpendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptx
 
FP Growth Algorithm and its Applications
FP Growth Algorithm and its ApplicationsFP Growth Algorithm and its Applications
FP Growth Algorithm and its Applications
 
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
 
Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
 
Q1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year ReboundQ1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year Rebound
 
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project PresentationPredicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
 
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdfCh03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
 

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.