SlideShare a Scribd company logo
1 of 17
Recap lecture 23
Context Free Grammar, Terminals, non-terminals,
productions, CFG, context Free language, examples.
Example
∑ = {a,b}
productions:
1. S → SS
2. S → XS
3. S → ^
4. S → YSY
5. X → aa
6. X → bb
7. Y → ab
8. Y → ba
This grammar generates EVEN-EVEN language.
Example
∑ = {a,b}
productions:
1. S → aB
2. S → bA
3. A → a
4. A → aS
5. A → bAA
6. B → b
7. B → bS
8. B → aBB
This grammar generates the language EQUAL(The
language of strings, with number of a’s equal to number of
b’s).
Note
It is to be noted that if the same non-
terminal have more than one productions,
it can be written in single line e.g.
S → aS, S → bS, S → ^ can be written as
S → aS|bS|^
It may also be noted that the productions
S → SS|^ always defines the language
which is closed w.r.t. concatenation i.e.the
language expressed by RE of type r*
. It may
also be noted that the production S → SS
defines the language expressed by r+
.
Example
Consider the following CFG ∑ = {a,b}
productions:
1. S → YXY
2. Y → aY|bY|^
3. X → bbb
It can be observed that, using prod.2, Y
generates ^. Y generates a. Y generates b. Y
also generates all the combinations of a and b.
thus Y generates the strings generated by (a+b)*
.
It may also be observed that the above CFG
generates the language expressed by
(a+b)*
bbb(a+b)*
. Following are four words
generated by the given CFG
Example continued …
S → YXY
→ aYbbb^
→ abYbbb
→ ab^bbb
= abbbb
S → YXY
→ ^bbbaY
→ bbbabY
→ bbbabaY
→ bbbaba^
S → YXY
→ bYbbbaY
→ b^bbbabY
→ bbbbabbY
→ bbbbabbaY
→ bbbbabba^
= bbbbabba
S → YXY
→ bYbbbaY
→ b^bbba^
= bbbba
Example
Consider the following CFG
1. S → SS|XaXaX|^
2. X → bX|^
It can be observed that, using prod.2, X
generates ^. X generates any number of b’s.
Thus X generates the strings generated by b*
.
It may also be observed that the above CFG
generates the language expressed by
(b*
ab*
ab*
)*
.
Example
Consider the following CFG
∑ = {a,b}
productions:
S → aSa|bSb|a|b|^
The above CFG generates the
language PALINDROME. It may be
noted that the CFG
S → aSa|bSb|a|b generates the
language NON-NULLPALINDROME.
Example
Consider the following CFG
∑ = {a,b}
productions:
S → aSb|ab|^
It can be observed that the CFG
generates the language {an
bn
:
n=0,1,2,3, …}. It may also be noted
that the language {an
bn
: n=1,2,3, …}
can be generated by the following
CFG S → aSb|ab
Task
Construct CFG that generates the language L = {w 
{a,b}*
: length(w)  2 and second letter of w from right is
a}
Example
Consider the following CFG
(1) S → aXb|bXa (2) X → aX|
bX|^
The above CFG generates the
language of strings, defined over
∑={a,b}, beginning and ending
in different letters.
Task
Construct the CFG for the language of
strings, defined over ∑={a,b},
beginning and ending in same letters.
Trees
As in English language any sentence can be
expressed by parse tree, so any word generated
by the given CFG can also be expressed by the
parse tree, e.g.
consider the following CFG
S → AA
A → AAA|bA|Ab|a
Obviously, baab can be generated by the above
CFG. To express the word baab as a parse tree,
start with S. Replace S by the string AA, of
nonterminals, drawing the downward lines from S
to each character of this string as follows
Trees continued …
Now let the left A be replaced by bA and the right
one by Ab then the tree will be
S
A A
S
A A
b A A b
Trees continued …
Replacing both A’s by a, the above tree will be
S
A A
b A A b
a a
Trees continued …
Thus the word baab is generated. The above tree to
generate the word baab is called Syntax tree or
Generation tree or Derivation tree as well.
Summing Up
 Context Free Grammar, Trees, examples.

More Related Content

What's hot (20)

Lecture 6
Lecture 6Lecture 6
Lecture 6
 
Chapter1 Formal Language and Automata Theory
Chapter1 Formal Language and Automata TheoryChapter1 Formal Language and Automata Theory
Chapter1 Formal Language and Automata Theory
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 
Lesson 12
Lesson 12Lesson 12
Lesson 12
 
Lesson 03
Lesson 03Lesson 03
Lesson 03
 
Lesson 02
Lesson 02Lesson 02
Lesson 02
 
Lesson 05
Lesson 05Lesson 05
Lesson 05
 
Lesson 04
Lesson 04Lesson 04
Lesson 04
 
2.7 normal forms cnf & problems
2.7 normal forms  cnf & problems2.7 normal forms  cnf & problems
2.7 normal forms cnf & problems
 
Lesson 09
Lesson 09Lesson 09
Lesson 09
 
Theory of computation Lec3 dfa
Theory of computation Lec3 dfaTheory of computation Lec3 dfa
Theory of computation Lec3 dfa
 
Chomsky & Greibach Normal Forms
Chomsky & Greibach Normal FormsChomsky & Greibach Normal Forms
Chomsky & Greibach Normal Forms
 
Lecture 3,4
Lecture 3,4Lecture 3,4
Lecture 3,4
 
2.8 normal forms gnf & problems
2.8 normal forms   gnf & problems2.8 normal forms   gnf & problems
2.8 normal forms gnf & problems
 
Chapter 1 : Balagurusamy_ Programming ANsI in C
Chapter 1  :  Balagurusamy_ Programming ANsI in C Chapter 1  :  Balagurusamy_ Programming ANsI in C
Chapter 1 : Balagurusamy_ Programming ANsI in C
 
Theory of Automata Lesson 02
Theory of Automata Lesson 02Theory of Automata Lesson 02
Theory of Automata Lesson 02
 
Lesson 03
Lesson 03Lesson 03
Lesson 03
 
1.9. minimization of dfa
1.9. minimization of dfa1.9. minimization of dfa
1.9. minimization of dfa
 
Chomsky Normal Form
Chomsky Normal FormChomsky Normal Form
Chomsky Normal Form
 
Language
LanguageLanguage
Language
 

Similar to Cfg part ii

Context Free Languages by S.Mandal-1.ppt
Context Free Languages by S.Mandal-1.pptContext Free Languages by S.Mandal-1.ppt
Context Free Languages by S.Mandal-1.ppt1sonalishipu
 
Context Free Grammar
Context Free GrammarContext Free Grammar
Context Free GrammarniveditJain
 
Theory of Automata Lesson 01
 Theory of Automata Lesson 01  Theory of Automata Lesson 01
Theory of Automata Lesson 01 hamzamughal39
 
Lesson-01-29092022-081117pm.ppt
Lesson-01-29092022-081117pm.pptLesson-01-29092022-081117pm.ppt
Lesson-01-29092022-081117pm.pptashja1
 
Simplifiaction of grammar
Simplifiaction of grammarSimplifiaction of grammar
Simplifiaction of grammarlavishka_anuj
 
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
 
Hw4 2017-spring
Hw4 2017-springHw4 2017-spring
Hw4 2017-spring奕安 陳
 
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 ComputationNikhil Pandit
 
Hw3 2017-spring
Hw3 2017-springHw3 2017-spring
Hw3 2017-spring奕安 陳
 
Conteext-free Grammer
Conteext-free GrammerConteext-free Grammer
Conteext-free GrammerHASHIR RAZA
 
Theory of Automata and formal languages Unit 3
Theory of Automata and formal languages Unit 3Theory of Automata and formal languages Unit 3
Theory of Automata and formal languages Unit 3Abhimanyu Mishra
 
163692498 grammar
163692498 grammar163692498 grammar
163692498 grammarcjsmann
 
CNF & Leftmost Derivation - Theory of Computation
CNF & Leftmost Derivation - Theory of ComputationCNF & Leftmost Derivation - Theory of Computation
CNF & Leftmost Derivation - Theory of ComputationDrishti Bhalla
 

Similar to Cfg part ii (20)

TOA_WEEK 14.pptx
TOA_WEEK 14.pptxTOA_WEEK 14.pptx
TOA_WEEK 14.pptx
 
Context free grammar
Context free grammarContext free grammar
Context free grammar
 
Context Free Languages by S.Mandal-1.ppt
Context Free Languages by S.Mandal-1.pptContext Free Languages by S.Mandal-1.ppt
Context Free Languages by S.Mandal-1.ppt
 
To lec 03
To lec 03To lec 03
To lec 03
 
Lec4
Lec4Lec4
Lec4
 
Context Free Grammar
Context Free GrammarContext Free Grammar
Context Free Grammar
 
Theory of Automata Lesson 01
 Theory of Automata Lesson 01  Theory of Automata Lesson 01
Theory of Automata Lesson 01
 
Lesson-01-29092022-081117pm.ppt
Lesson-01-29092022-081117pm.pptLesson-01-29092022-081117pm.ppt
Lesson-01-29092022-081117pm.ppt
 
Simplifiaction of grammar
Simplifiaction of grammarSimplifiaction of grammar
Simplifiaction of 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
 
Ch02
Ch02Ch02
Ch02
 
To lec 04
To lec 04To lec 04
To lec 04
 
Hw4 2017-spring
Hw4 2017-springHw4 2017-spring
Hw4 2017-spring
 
Unit iii
Unit iiiUnit iii
Unit iii
 
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
 
Hw3 2017-spring
Hw3 2017-springHw3 2017-spring
Hw3 2017-spring
 
Conteext-free Grammer
Conteext-free GrammerConteext-free Grammer
Conteext-free Grammer
 
Theory of Automata and formal languages Unit 3
Theory of Automata and formal languages Unit 3Theory of Automata and formal languages Unit 3
Theory of Automata and formal languages Unit 3
 
163692498 grammar
163692498 grammar163692498 grammar
163692498 grammar
 
CNF & Leftmost Derivation - Theory of Computation
CNF & Leftmost Derivation - Theory of ComputationCNF & Leftmost Derivation - Theory of Computation
CNF & Leftmost Derivation - Theory of Computation
 

More from Kashif Ali

What makes a leader great 1
What makes a leader great 1What makes a leader great 1
What makes a leader great 1Kashif Ali
 
Entrepreneurship
EntrepreneurshipEntrepreneurship
EntrepreneurshipKashif Ali
 
Leader vs boss
Leader vs bossLeader vs boss
Leader vs bossKashif Ali
 
Entrepreneurship
EntrepreneurshipEntrepreneurship
EntrepreneurshipKashif Ali
 
Mission vs vision statement
Mission vs vision statementMission vs vision statement
Mission vs vision statementKashif Ali
 

More from Kashif Ali (6)

What makes a leader great 1
What makes a leader great 1What makes a leader great 1
What makes a leader great 1
 
Entrepreneurship
EntrepreneurshipEntrepreneurship
Entrepreneurship
 
Leader vs boss
Leader vs bossLeader vs boss
Leader vs boss
 
Entrepreneurship
EntrepreneurshipEntrepreneurship
Entrepreneurship
 
Mission vs vision statement
Mission vs vision statementMission vs vision statement
Mission vs vision statement
 
Cfg part i
Cfg   part iCfg   part i
Cfg part i
 

Recently uploaded

Night 7k to 12k Top Call Girls Ahmedabad 👉 BOOK NOW 8617697112 👈 ♀️ night gir...
Night 7k to 12k Top Call Girls Ahmedabad 👉 BOOK NOW 8617697112 👈 ♀️ night gir...Night 7k to 12k Top Call Girls Ahmedabad 👉 BOOK NOW 8617697112 👈 ♀️ night gir...
Night 7k to 12k Top Call Girls Ahmedabad 👉 BOOK NOW 8617697112 👈 ♀️ night gir...Call girls in Ahmedabad High profile
 
Model Call Girl in Shalimar Bagh Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Shalimar Bagh Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Shalimar Bagh Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Shalimar Bagh Delhi reach out to us at 🔝8264348440🔝soniya singh
 
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual serviceCALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual serviceanilsa9823
 
9892124323 | Book Call Girls in Juhu and escort services 24x7
9892124323 | Book Call Girls in Juhu and escort services 24x79892124323 | Book Call Girls in Juhu and escort services 24x7
9892124323 | Book Call Girls in Juhu and escort services 24x7Pooja Nehwal
 
Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,
Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,
Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,Pooja Nehwal
 
哪里有卖的《俄亥俄大学学历证书+俄亥俄大学文凭证书+俄亥俄大学学位证书》Q微信741003700《俄亥俄大学学位证书复制》办理俄亥俄大学毕业证成绩单|购买...
哪里有卖的《俄亥俄大学学历证书+俄亥俄大学文凭证书+俄亥俄大学学位证书》Q微信741003700《俄亥俄大学学位证书复制》办理俄亥俄大学毕业证成绩单|购买...哪里有卖的《俄亥俄大学学历证书+俄亥俄大学文凭证书+俄亥俄大学学位证书》Q微信741003700《俄亥俄大学学位证书复制》办理俄亥俄大学毕业证成绩单|购买...
哪里有卖的《俄亥俄大学学历证书+俄亥俄大学文凭证书+俄亥俄大学学位证书》Q微信741003700《俄亥俄大学学位证书复制》办理俄亥俄大学毕业证成绩单|购买...wyqazy
 
Chandigarh Call Girls Service ❤️🍑 9115573837 👄🫦Independent Escort Service Cha...
Chandigarh Call Girls Service ❤️🍑 9115573837 👄🫦Independent Escort Service Cha...Chandigarh Call Girls Service ❤️🍑 9115573837 👄🫦Independent Escort Service Cha...
Chandigarh Call Girls Service ❤️🍑 9115573837 👄🫦Independent Escort Service Cha...Niamh verma
 
CALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun serviceCALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun serviceanilsa9823
 
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost LoverPowerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost LoverPsychicRuben LoveSpells
 

Recently uploaded (9)

Night 7k to 12k Top Call Girls Ahmedabad 👉 BOOK NOW 8617697112 👈 ♀️ night gir...
Night 7k to 12k Top Call Girls Ahmedabad 👉 BOOK NOW 8617697112 👈 ♀️ night gir...Night 7k to 12k Top Call Girls Ahmedabad 👉 BOOK NOW 8617697112 👈 ♀️ night gir...
Night 7k to 12k Top Call Girls Ahmedabad 👉 BOOK NOW 8617697112 👈 ♀️ night gir...
 
Model Call Girl in Shalimar Bagh Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Shalimar Bagh Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Shalimar Bagh Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Shalimar Bagh Delhi reach out to us at 🔝8264348440🔝
 
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual serviceCALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual service
 
9892124323 | Book Call Girls in Juhu and escort services 24x7
9892124323 | Book Call Girls in Juhu and escort services 24x79892124323 | Book Call Girls in Juhu and escort services 24x7
9892124323 | Book Call Girls in Juhu and escort services 24x7
 
Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,
Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,
Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,
 
哪里有卖的《俄亥俄大学学历证书+俄亥俄大学文凭证书+俄亥俄大学学位证书》Q微信741003700《俄亥俄大学学位证书复制》办理俄亥俄大学毕业证成绩单|购买...
哪里有卖的《俄亥俄大学学历证书+俄亥俄大学文凭证书+俄亥俄大学学位证书》Q微信741003700《俄亥俄大学学位证书复制》办理俄亥俄大学毕业证成绩单|购买...哪里有卖的《俄亥俄大学学历证书+俄亥俄大学文凭证书+俄亥俄大学学位证书》Q微信741003700《俄亥俄大学学位证书复制》办理俄亥俄大学毕业证成绩单|购买...
哪里有卖的《俄亥俄大学学历证书+俄亥俄大学文凭证书+俄亥俄大学学位证书》Q微信741003700《俄亥俄大学学位证书复制》办理俄亥俄大学毕业证成绩单|购买...
 
Chandigarh Call Girls Service ❤️🍑 9115573837 👄🫦Independent Escort Service Cha...
Chandigarh Call Girls Service ❤️🍑 9115573837 👄🫦Independent Escort Service Cha...Chandigarh Call Girls Service ❤️🍑 9115573837 👄🫦Independent Escort Service Cha...
Chandigarh Call Girls Service ❤️🍑 9115573837 👄🫦Independent Escort Service Cha...
 
CALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun serviceCALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun service
 
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost LoverPowerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
 

Cfg part ii

  • 1. Recap lecture 23 Context Free Grammar, Terminals, non-terminals, productions, CFG, context Free language, examples.
  • 2. Example ∑ = {a,b} productions: 1. S → SS 2. S → XS 3. S → ^ 4. S → YSY 5. X → aa 6. X → bb 7. Y → ab 8. Y → ba This grammar generates EVEN-EVEN language.
  • 3. Example ∑ = {a,b} productions: 1. S → aB 2. S → bA 3. A → a 4. A → aS 5. A → bAA 6. B → b 7. B → bS 8. B → aBB This grammar generates the language EQUAL(The language of strings, with number of a’s equal to number of b’s).
  • 4. Note It is to be noted that if the same non- terminal have more than one productions, it can be written in single line e.g. S → aS, S → bS, S → ^ can be written as S → aS|bS|^ It may also be noted that the productions S → SS|^ always defines the language which is closed w.r.t. concatenation i.e.the language expressed by RE of type r* . It may also be noted that the production S → SS defines the language expressed by r+ .
  • 5. Example Consider the following CFG ∑ = {a,b} productions: 1. S → YXY 2. Y → aY|bY|^ 3. X → bbb It can be observed that, using prod.2, Y generates ^. Y generates a. Y generates b. Y also generates all the combinations of a and b. thus Y generates the strings generated by (a+b)* . It may also be observed that the above CFG generates the language expressed by (a+b)* bbb(a+b)* . Following are four words generated by the given CFG
  • 6. Example continued … S → YXY → aYbbb^ → abYbbb → ab^bbb = abbbb S → YXY → ^bbbaY → bbbabY → bbbabaY → bbbaba^ S → YXY → bYbbbaY → b^bbbabY → bbbbabbY → bbbbabbaY → bbbbabba^ = bbbbabba S → YXY → bYbbbaY → b^bbba^ = bbbba
  • 7. Example Consider the following CFG 1. S → SS|XaXaX|^ 2. X → bX|^ It can be observed that, using prod.2, X generates ^. X generates any number of b’s. Thus X generates the strings generated by b* . It may also be observed that the above CFG generates the language expressed by (b* ab* ab* )* .
  • 8. Example Consider the following CFG ∑ = {a,b} productions: S → aSa|bSb|a|b|^ The above CFG generates the language PALINDROME. It may be noted that the CFG S → aSa|bSb|a|b generates the language NON-NULLPALINDROME.
  • 9. Example Consider the following CFG ∑ = {a,b} productions: S → aSb|ab|^ It can be observed that the CFG generates the language {an bn : n=0,1,2,3, …}. It may also be noted that the language {an bn : n=1,2,3, …} can be generated by the following CFG S → aSb|ab
  • 10. Task Construct CFG that generates the language L = {w  {a,b}* : length(w)  2 and second letter of w from right is a}
  • 11. Example Consider the following CFG (1) S → aXb|bXa (2) X → aX| bX|^ The above CFG generates the language of strings, defined over ∑={a,b}, beginning and ending in different letters.
  • 12. Task Construct the CFG for the language of strings, defined over ∑={a,b}, beginning and ending in same letters.
  • 13. Trees As in English language any sentence can be expressed by parse tree, so any word generated by the given CFG can also be expressed by the parse tree, e.g. consider the following CFG S → AA A → AAA|bA|Ab|a Obviously, baab can be generated by the above CFG. To express the word baab as a parse tree, start with S. Replace S by the string AA, of nonterminals, drawing the downward lines from S to each character of this string as follows
  • 14. Trees continued … Now let the left A be replaced by bA and the right one by Ab then the tree will be S A A S A A b A A b
  • 15. Trees continued … Replacing both A’s by a, the above tree will be S A A b A A b a a
  • 16. Trees continued … Thus the word baab is generated. The above tree to generate the word baab is called Syntax tree or Generation tree or Derivation tree as well.
  • 17. Summing Up  Context Free Grammar, Trees, examples.