MODULE 3
CONTEXT FREE GRAMMAR
MODULE III:
Context-Free Grammars(CFG): Introduction to Rewrite Systems and Grammars, CFGs
and languages, designing CFGs, simplifying CFGs, proving that a Grammar is correct,
Derivation and Parse trees, Ambiguity, Normal Forms.
Pushdown Automata (PDA): Definition of non-deterministic PDA, Deterministic and Non-
deterministic PDAs, Nondeterminism and Halting, alternative equivalent definitions of a
PDA, alternatives that are not equivalent to PDA.
Definition:
Context-Free Grammar (CFG) has 4-tuple: G = (V, T, P, S)
Where,
V - A finite set of variables or non-terminals
T - A finite set of terminals (V and T do not intersect)
P - A finite set of productions, each of the form A –> α,
Where A is in V and α is in (V U T)*
Note: that α may be ε.
S - A starting non-terminal (S is in V)
Example CFG:
S, SIGMA={0,1}
G = ({S}, {0, 1}, P, S)
P: S –> 0S1 or just simply S –> 0S1 | ε
S –> ε
L={01, 0011, 000111, 00001111, 0000011111, }
S –> 0S1
0e1
01
S –> 0S1
0 0S1 1
00 e 1 1
0011
Example :
2. CFG to generate 0 or more a’s L{e, a, aa, aaaa………}
A –> Aa | a | ε
A => Aa
=> aa ; A->a
derivation
3. CFG to generate strings of a’s and b’s of any length L={e, aba, baba,………}
S –> ε |a|b|Sa|Sb
aba
S->Sa ; S->Sa
->Sb a ; S->Sb
-> aba ; S ->a
4. Grammar for L ={anbn |n>=1}
L ={ab,aabb,aaabbb…………}
P: S –> ab|aSb
V ={S} T={a,b} P:set of productions S:{S}
5. Grammar for L ={anbn |n>=0}
L ={ε , ab,aabb,aaabbb…………}
S –> ab|aSb | ε
6. Grammar for L ={anb2n |n>=0} L={e, abb, aabbbb, ……. } aabbbb
L ={ε , abb,aabbbb,aaabbbbbb…………} S->aSbb
S –> abb|aSbb | ε ->aabb bb ; S-
>abb
7. Grammar for L ={anbn+1 |n>=0}
L ={b , abb,aabbb,aaabbbb…………}
S –> b|aSb
8 . Grammar for L ={anbn+2 |n>=0}
L ={bb , abbb,aabbbb,aaabbbbb…………}
anbn+2 = anbn b b
S –> Abb
A –> ε |aAb
abbb
S->Abb
->aAb bb ; A->aAb
->a e bbb ; A->e
abbb
9. Grammar for L ={ w : |w| mod 3 = 0} over Σ ={a}
L ={ε , aaa, aaaaaa…………}
S –> ε |Saaa
10. Grammar for L ={ w : |w| mod 3 > 0} over Σ ={a}
L ={a, aa, aaaa, aaaaa…………} aaaa
S –> a | aa|Saaa S –>Saaa
–> a aaa ; S –>a
11. Grammar for L ={anbm ck : m=n+k } over Σ ={a, b, c} aabbbbcc
L ={abbc, aabbbbcc, aaaacccccb,…………} S –> AB
anbm ck = an bn+k ck = an bn bk ck ->aAbB
S –> AB ->aab bB A->ab
A –> aAb | ab -> aabb bBc ; B->bBc
B –> bBc | bc -> aabbbbcc ; B->bc
12. Grammar for L ={an bm ck : n=m+k } over Σ ={a, b, c}
L ={aaabbc, aaaaaabbbbcc, aaaaaabccccc,…………}
an bm ck = am+kbm ck = am ak bm ck = ak am bm ck
S –> aAc S –> aAc
A –> aAb | aAc | ε a aAb c
aa aAb bc
aaa e bbc
aaabbc
13. Grammar for L ={WWR } over Σ ={a, b}
S –> aSa | bSb | a | b | ε
L={abba, abbbba…….} abbbba
S ->aSa
a bSb a ; S->bSb
ab bSb ba ; S->bSb
abb e bba ; S->e
abbbba
14. Grammar to generate arithmetic expression { id }
E -> E + E
E -> E – E
E -> E * E
E -> E / E
E -> id | (E)
Generate: id + id*id : Left Most Derivation
E→E+E
E → id + E
E → id + E * E Id+id
E → id + id * E Id*id-id/id
E → id + id * id A+b-(a-d)
For some strings there exist more than one parse tree
Or more than one leftmost derivation or more than one rightmost derivation
P:
E→E+E|E*E|E–E| E/E
E→ id
G=(V T P S) V={E} T={id,+,_,*,/} S =E
Example: id+id*id
Solution: The sentence id +id *id can be obtained from left most derivation in two ways as
shown below.
E E+E E E*E
id+E E+E*E
id+E*E id+E*E
id+ id *E id+id*E
id+ id *id id+ id *id
Is the grammar ambiguous?
S -> aS | X
X -> aX | a
L={a, aa, aaa, aaaa}
Consider aaa
S -> aS S -> X
-> a X -> aX
-> aa X -> aaX
-> aa a -> aaa
Is the grammar ambiguous?
S -> aB | bA
A -> aS | bAA | a
B -> bS | aBB | b
Consider string : aabbab
Obtain string aaabbabbba by applying left most and right most derivations
Show that the grammar is ambiguous
S -> aSbS | bSaS | ε
Soln : derive aababb
S -> aSbS
aaSbSbS
aabSaS
2) Obtain the left most derivation for the string aaabbabbba using the following
grammer
SaB/bA
Aas/bAA/a
Bbs/aBB/b
SaB
aaBB (applying BaBB)
aaaBBB (applying BaBB)
aaabBB (applying Bb)
aaabbB (applying Bb)
aaabbaBB (BaBB)
aaabbabB (Bb)
aaabbabbs (Bbs)
aaabbabbbA (SbA)
aaababbba (Aa)
3) check whether the given grammer is ambiguous or not
Sicts
Sictses
Sa
Cb
Consider string ibtibtibtaea
Draw the derivation tree
Der1 --ibtibtibtaea
S->iCtSeS
-> ibtSeS ; C->b
-> ibt iCtS eS ; C->b
->ibtibt SeS ; S->iCtS
-> ibtibt iCtS eS ; C->b
->ibtibtibt SeS ; S->a
->ibtibtibt aeS ; S->a
->ibtibtibtaea
4) consider grammer
G = {V,T,R,S}
V = {S,A}
T = {A,B}
R={ SAA
AAAA
Aa
ABa
AAb}
Such that the grammer is ambiguous
Solution: consider the string babbab
Exercise
1) The following grammer queries the grammer of the language consisting of all strings
of even length
SAS/ ε
Aaa/ab/ba/bb
Give the leftmost , rightmost derivatives of the following strings
a) aabbba
b) baabab
c) aaabbb
Normal forms of contest free grammer
Simplification of CFG
goal to generate CFG in which all productions are of the form
ABC
Or
Aa
Where A,B,C are variables and a is terminal.
This form is called Chomsky Normal Form
Ways to get grammer in CNF
1) elimination useless symbol , those variables or terminal that do not appear in any
derivation of a terminal string from the start symbol.
2) Elimination of the ε productions those of the form Aε for some variable A
3) Elimination of unit productions of the form AB for variables A and B
Eliminating useless productions
Symbol X is said to be useless for a grammer
G = (V,T,P,S) if there is some derivation of the form
Sα×βw
X may be either in V or T and α×β may be first or last in the derivation
Example 1) consider the grammer
S aAa/aBC
Aas/bD
BaBa/b
Cabb/DD
DaDa
B,C are the only non-terminals directly queries Terminals strings hence generating
S also generates
D is not hence be generating. Hence can be eliminated
Eliminate D is production involving D
saAa/aBC
Aas
BaBa/b
Cabb
2) Sasb/ℇ
AaA
A non generating can be eliminated
Sasb/εis useful production
4) SA Sas/A/C
AaA/ℇ Aa
BbA Baa
cacb
C- non generating (elimination)
Sas/A
Aa
Baa
B-non reachable
Sas/A
Aa
5) AaA/a/Bb/cC
AaB
Ba/Aa
CcCD
Dddd
C- non generating
Aan/a/Bb
AaB
Ba/Aa
Dddd
D- non reachable
San/a/Bb
AaB
Ba/Aa
Eliminating ℇ productions
* ℇ production is grammer are not essential
* If L ha a CFG, then L-{ ℇ } has CFG without ℇ -production
* If ℇ is not in L, then L
Steps
Discover the nullable variable
A variable is nullable if
A ℇ
1) Determine all nullable symbols of G
*Any production of CFG of the form
A ℇ is called
ℇ productions
*Any variable A for which the derivation
A ℇ is possible is called nullable
Example 1
Consider the grammer
SABC
ABC/a
BbAC/ ℇ
CSAB/ ℇ
B,C are directly nullable
A is also nullable : ABC
S is also nullable : SABC
SABC/AB/BC/AC/A/B/C
ABC/a
BC/B/C/a
BbAC/bA/bC/b
CCAB/CA/CB/C
2) Sas1b
S1as1b/ ℇ
ℇ- production are S1ℇ can be removed after adding new production obtained by
substituting ℇ for S1 where it occurs to the sigma
Sa s1b/ab
S1a s1b/ab
3) S ABac
ABC
Bb/ ℇ
CD/ ℇ
Dd
A, B, C, S are nullable
SABaC/Bac/AaC/ABa/ac/Aa/Ba/a
AB/C/BC
Bb
CD
Dd
Eliminating unit productions
Any production of CFG of the form,
AB where
A, B, E, V is called unit productions
Replace any sequence of derivation in which
AB1B2....... Bnα,
By a productions that there was the non unit productions
Bnα directly from A
i.e A α
1) SAB
Aa
Be/b
CD
DE/bC
Ed/Ab
Unit Non unit
BC SAB
CD Aa
DE Bb
DbC
BCDE Ed/Ab
Productions generated from E can also be generated from D
Rewrite Dd/Ab/bC
Ed/Ab
Cd/Ab/bc
Bb/d/Ab/bc
Aa
AAB
Example
1. Remove unit productions Non unit productions
SAa/B SAa
BA/bb Bbb
Aa/bc/B Aa/bc
Dependency graph
A
S B
SAa
Aa/bc/bb
Ba/bc/bb
SAa/a/bc/bb
2. Ia/b/Ia/Ib/I0/I1
FI/(E)
Tf/T*F
ET/E+T
I F T
Non unit production
Ia/b/Ia/Ib/I0/I1
F(E)/a/b/Ia/Ib/I0/I1
TT*F/(F)/a/b/Ia/Ib/I0/I1
EE+T/T*F/(E)/a/b/Ia/Ib/I0/I1
3) SAa/B/Ca
BaB/b
CDb/D
DE/d
Eab
SB
CDE
Rewrite Eab
Dd/ab
CDb/d/ab
BaB/b
SAa/Ca/aB/b
CHOMSKY NORMAL FORM
Definition
A context free grammer G is in Chomsky normal form if all the production are of the form
1) AB,C A, B, C are variables
2) Aa, a is terminal
Where C1 has no useless symbols
Steps
1) Arrange that all bodies of the length 2 or more consists only of variables.
2) Break bodies of length 3 or more into cascade of productions, each with a body
consisting of two variables.
Example
1) Convert the grammer with the following productions to Chomsky normal form.
SABa
Aaab
BAc
Introduce new variables Ba, Bb, Bc
SABBa
ABaBab
BABC
Baa
Bbb
Bcc
2) Introduce additional variables to get the productions in normal form
SaD1 Baa
D1BBa Bbb
ABaD2 Bcc
D2BaBb D1BBa
BABc
Excerise 7.1.2
Grammer SABC/BaB
AaA/BaC/aaa
BbBb/a/D
CCA/AC
DE
1) Eliminate ℇ - productions.
2) Eliminate any unit productions in the resulting grammer.
3) Eliminate any useless symbol in the resulting grammer.
4) Put the resulting grammer into Chomsky normal form.
1) B, D are nullable
BbBb/bb/a
CCA/AC
AaA/aC/Bac/aaa
SABC/BaB/AC/Ba/aB/a
2) No unit productions
3) C is the only varaiable which is non generating eliminate C
SBaB/Ba/aB/a
AaA/aaa
BbBb/bb/a
4) CNF Introduce two variables
Ea
Fb
SBEB/BE/EB/a
AEA/EEE
BFBF/FF/a
SBG/BE/EB/a
GEB
AEA/EH
HEE
BFI/FF/a
IBF