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

2.3 context free grammars and languages

  • 1.
  • 2.
    Definition of CFG Acontext-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
  • 7.
  • 8.

Editor's Notes

  • #2 School of EECS, WSU