SlideShare a Scribd company logo
Context Free
Grammars and
Languages
-Sampath Kumar S,
AP/CSE, SECE
Definition of CFG
A context-free grammar (CFG) consisting of a finite
set of grammar rules is a quadruple G= (N, T, P,
S) where
 N is a set of Non-Terminal symbols.
 T is a set of Terminals where N ∩ T = NULL.
 P is a set of Production rules, P: N → (N ∪ T)*
 S is the Start symbol.
11/21/2017
Sampath Kumar S, AP/CSE, SECE
2
Example
 The grammar
G= ({A}, {a, b, c}, P, A), P : A → aA, A → abc.
 The grammar
G=({S}, {a, b}, P, S), P: S → aSa, S → bSb, S → ε
 The grammar
G=({S, F}, {0, 1}, P, S), P: S → 00S | 11F, F → 00F | ε
11/21/20173
Sampath Kumar S, AP/CSE, SECE
Problems to discuss:
52. What is language of CFG G=({A,B}, {a,b}, P, A)
where P: A → Ba, B → b.
53. Construct the CFG for Regular Expression (0+1)*.
54. Construct the CFG for defining a palindrome over
{a, b}.
55. Construct the CFG for the set of strings with equal
number of a’s and b’s.
56. Construct the CFG for the language L(G)={anb2n
where n>1}.
11/21/20174
Sampath Kumar S, AP/CSE, SECE
Problems to discuss:
57. Construct the CFG for the language containing
all string of different 1st and last symbol over Σ =
{0,1}.
58. Give CFG for R.E (a+b)*cc(a+b)*.
59. What is language of CFG G=({S,C}, {a,b},P,S)
where P: S → aCa, C → aCa|b.
60. What is language of CFG G=({S}, {0,1},P,S)
where P: S → 0S1 | ε.
11/21/2017
Sampath Kumar S, AP/CSE, SECE
5
Answers:
52. L(G) = {ab}
53. P = { S → 0S | 1S |ε }
54. P = { S → aSb | bSa | a | b |ε }
55. P = { S → SaSbS | SbSaS |ε }
56. P = { S → aSbb |abb }
57. P = { S → 0A1 | 1A0 , A → 0A | 1A |ε }
58. P = { S → AccA, A → aA | bA |ε }
59. L(G) = {anban where n>1}
60. L(G) = {0n1n where n>1}
11/21/2017
Sampath Kumar S, AP/CSE, SECE
6
11/21/2017
Sampath Kumar S, AP/CSE, SECE
9
நன்றி
11/21/2017
Sampath Kumar S, AP/CSE, SECE
10

More Related Content

PPTX
2.4 derivations and languages
PPTX
2.5 ambiguity in context free grammars
PPTX
2.8 normal forms gnf & problems
PPTX
Grammar generated by language iv
PPTX
Grammar generated by language example
PPTX
Grammar generated by language ii
PPTX
Grammar generated by language iii
PPTX
Grammar generated by language
2.4 derivations and languages
2.5 ambiguity in context free grammars
2.8 normal forms gnf & problems
Grammar generated by language iv
Grammar generated by language example
Grammar generated by language ii
Grammar generated by language iii
Grammar generated by language

What's hot (20)

PPT
7.7 Transforming Formulas
PPTX
CNF & Leftmost Derivation - Theory of Computation
PPTX
MATEMÁTICA | SEMANA 35 | 3ª SÉRIE | FUNÇÃO LOGARÍTMICA
PPT
Chapter 2 Review
DOCX
CS2303 Theory of computation April may 2015
PPTX
Graph Coloring
PPT
Chapter1.5
PPTX
Chomsky Normal Form
PDF
Media,265106,en
PPT
Simplifies and normal forms - Theory of Computation
DOCX
Cs6503 theory of computation april may 2017
PPT
Knowledge representationm
PDF
Color Coding
PPTX
Edge Coloring & K-tuple coloring
PDF
Cs6503 theory of computation november december 2015 be cse anna university q...
PPTX
Graph coloring using backtracking
DOC
Model toc
PPTX
Divisibility rules (Properties of Divisibility)
PPT
Graph coloring problem
PPT
chapter22.ppt
7.7 Transforming Formulas
CNF & Leftmost Derivation - Theory of Computation
MATEMÁTICA | SEMANA 35 | 3ª SÉRIE | FUNÇÃO LOGARÍTMICA
Chapter 2 Review
CS2303 Theory of computation April may 2015
Graph Coloring
Chapter1.5
Chomsky Normal Form
Media,265106,en
Simplifies and normal forms - Theory of Computation
Cs6503 theory of computation april may 2017
Knowledge representationm
Color Coding
Edge Coloring & K-tuple coloring
Cs6503 theory of computation november december 2015 be cse anna university q...
Graph coloring using backtracking
Model toc
Divisibility rules (Properties of Divisibility)
Graph coloring problem
chapter22.ppt
Ad

Similar to 2.3 context free grammars and languages (20)

PPTX
Conteext-free Grammer
PPTX
ContextFreeGrammars (1).pptx
PPTX
ContextFreeGrammars.pptx
PPTX
AUTOMATA AUTOMATA AUTOMATA Automata6Chapter5.pptx
PPT
Cfg part ii
PDF
Lecture5 syntax analysis_1
PPTX
CS911-Lecture-21_43709.pptx
PPTX
TOA_WEEK 14.pptx
PPT
context free language
PDF
TOC 6 | CFG Design
PPT
2. context free langauages
DOCX
Context Free Grammer .docx
PPTX
Context free grammar
PPTX
LECTURE 1 ALPHABET,STRINGS, LANGUAGE CHOMSKY TYPES OF GRAMMAR.pptx
PPTX
2.7 normal forms cnf & problems
PPT
Cfg part i
PPTX
Lecture 4 final FINITE AUTOMATA FOR THEORY OF COMPUTATION
PPTX
Lecture 4diuedhiuhiudhiqwuhiuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu fin...
PPTX
Lecture 4 final FINITE AUTOMATA FOR THEORY OF COMPUTATION
Conteext-free Grammer
ContextFreeGrammars (1).pptx
ContextFreeGrammars.pptx
AUTOMATA AUTOMATA AUTOMATA Automata6Chapter5.pptx
Cfg part ii
Lecture5 syntax analysis_1
CS911-Lecture-21_43709.pptx
TOA_WEEK 14.pptx
context free language
TOC 6 | CFG Design
2. context free langauages
Context Free Grammer .docx
Context free grammar
LECTURE 1 ALPHABET,STRINGS, LANGUAGE CHOMSKY TYPES OF GRAMMAR.pptx
2.7 normal forms cnf & problems
Cfg part i
Lecture 4 final FINITE AUTOMATA FOR THEORY OF COMPUTATION
Lecture 4diuedhiuhiudhiqwuhiuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu fin...
Lecture 4 final FINITE AUTOMATA FOR THEORY OF COMPUTATION
Ad

More from Sampath Kumar S (20)

PPTX
5.2 primitive recursive functions
PPTX
4.7. chomskian hierarchy of languages
PPTX
4.6 halting problem
PPTX
4.3 techniques for turing machines construction
PPTX
4.2 variantsof turing machines (types of tm)
PPTX
4.1 turing machines
PPTX
3.6 & 7. pumping lemma for cfl & problems based on pl
PPTX
3.5 equivalence of pushdown automata and cfl
PPTX
3.4 deterministic pda
PPTX
3.1,2,3 pushdown automata definition, moves & id
PPTX
2.1 & 2.2 grammar introduction – types of grammar
PPTX
1.10. pumping lemma for regular sets
PPTX
1.9. minimization of dfa
PPTX
1.8. equivalence of finite automaton and regular expressions
PPTX
1.7. eqivalence of nfa and dfa
PPTX
1.5 & 1.6 regular languages & regular expression
PPTX
1.4. finite automaton with ε moves
PPTX
1.3.2 non deterministic finite automaton
PPTX
1.3.1 deterministic finite automaton
PPTX
1.2. introduction to automata theory
5.2 primitive recursive functions
4.7. chomskian hierarchy of languages
4.6 halting problem
4.3 techniques for turing machines construction
4.2 variantsof turing machines (types of tm)
4.1 turing machines
3.6 & 7. pumping lemma for cfl & problems based on pl
3.5 equivalence of pushdown automata and cfl
3.4 deterministic pda
3.1,2,3 pushdown automata definition, moves & id
2.1 & 2.2 grammar introduction – types of grammar
1.10. pumping lemma for regular sets
1.9. minimization of dfa
1.8. equivalence of finite automaton and regular expressions
1.7. eqivalence of nfa and dfa
1.5 & 1.6 regular languages & regular expression
1.4. finite automaton with ε moves
1.3.2 non deterministic finite automaton
1.3.1 deterministic finite automaton
1.2. introduction to automata theory

Recently uploaded (20)

PPTX
History, Philosophy and sociology of education (1).pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PDF
Yogi Goddess Pres Conference Studio Updates
PDF
RMMM.pdf make it easy to upload and study
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
UNIT III MENTAL HEALTH NURSING ASSESSMENT
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PDF
What if we spent less time fighting change, and more time building what’s rig...
PPTX
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
PPTX
Cell Structure & Organelles in detailed.
PPTX
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
History, Philosophy and sociology of education (1).pptx
Final Presentation General Medicine 03-08-2024.pptx
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Chinmaya Tiranga quiz Grand Finale.pdf
Cell Types and Its function , kingdom of life
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
Yogi Goddess Pres Conference Studio Updates
RMMM.pdf make it easy to upload and study
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
UNIT III MENTAL HEALTH NURSING ASSESSMENT
01-Introduction-to-Information-Management.pdf
Orientation - ARALprogram of Deped to the Parents.pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
Practical Manual AGRO-233 Principles and Practices of Natural Farming
What if we spent less time fighting change, and more time building what’s rig...
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
Cell Structure & Organelles in detailed.
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...

2.3 context free grammars and languages

  • 2. Definition of CFG A context-free grammar (CFG) consisting of a finite set of grammar rules is a quadruple G= (N, T, P, S) where  N is a set of Non-Terminal symbols.  T is a set of Terminals where N ∩ T = NULL.  P is a set of Production rules, P: N → (N ∪ T)*  S is the Start symbol. 11/21/2017 Sampath Kumar S, AP/CSE, SECE 2
  • 3. Example  The grammar G= ({A}, {a, b, c}, P, A), P : A → aA, A → abc.  The grammar G=({S}, {a, b}, P, S), P: S → aSa, S → bSb, S → ε  The grammar G=({S, F}, {0, 1}, P, S), P: S → 00S | 11F, F → 00F | ε 11/21/20173 Sampath Kumar S, AP/CSE, SECE
  • 4. Problems to discuss: 52. What is language of CFG G=({A,B}, {a,b}, P, A) where P: A → Ba, B → b. 53. Construct the CFG for Regular Expression (0+1)*. 54. Construct the CFG for defining a palindrome over {a, b}. 55. Construct the CFG for the set of strings with equal number of a’s and b’s. 56. Construct the CFG for the language L(G)={anb2n where n>1}. 11/21/20174 Sampath Kumar S, AP/CSE, SECE
  • 5. Problems to discuss: 57. Construct the CFG for the language containing all string of different 1st and last symbol over Σ = {0,1}. 58. Give CFG for R.E (a+b)*cc(a+b)*. 59. What is language of CFG G=({S,C}, {a,b},P,S) where P: S → aCa, C → aCa|b. 60. What is language of CFG G=({S}, {0,1},P,S) where P: S → 0S1 | ε. 11/21/2017 Sampath Kumar S, AP/CSE, SECE 5
  • 6. Answers: 52. L(G) = {ab} 53. P = { S → 0S | 1S |ε } 54. P = { S → aSb | bSa | a | b |ε } 55. P = { S → SaSbS | SbSaS |ε } 56. P = { S → aSbb |abb } 57. P = { S → 0A1 | 1A0 , A → 0A | 1A |ε } 58. P = { S → AccA, A → aA | bA |ε } 59. L(G) = {anban where n>1} 60. L(G) = {0n1n where n>1} 11/21/2017 Sampath Kumar S, AP/CSE, SECE 6

Editor's Notes

  • #2: School of EECS, WSU