0% found this document useful (0 votes)
3 views

MODULE 3 (24-10-2024)

Module 3 covers Context-Free Grammars (CFG) including their definitions, examples, and simplifications, as well as the concept of Pushdown Automata (PDA). It discusses the design and correctness of CFGs, derivation and parse trees, ambiguity, and normal forms such as Chomsky Normal Form. Additionally, the module provides exercises and examples to illustrate grammar ambiguities and the elimination of useless, ε, and unit productions.

Uploaded by

lekha DRTTIT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

MODULE 3 (24-10-2024)

Module 3 covers Context-Free Grammars (CFG) including their definitions, examples, and simplifications, as well as the concept of Pushdown Automata (PDA). It discusses the design and correctness of CFGs, derivation and parse trees, ambiguity, and normal forms such as Chomsky Normal Form. Additionally, the module provides exercises and examples to illustrate grammar ambiguities and the elimination of useless, ε, and unit productions.

Uploaded by

lekha DRTTIT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

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
SaB/bA
Aas/bAA/a
Bbs/aBB/b
SaB
aaBB (applying BaBB)
aaaBBB (applying BaBB)
aaabBB (applying Bb)
aaabbB (applying Bb)
aaabbaBB (BaBB)
aaabbabB (Bb)
aaabbabbs (Bbs)
aaabbabbbA (SbA)
aaababbba (Aa)

3) check whether the given grammer is ambiguous or not


Sicts
Sictses
Sa
Cb
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={ SAA
AAAA
Aa
ABa
AAb}

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
SAS/ ε
Aaa/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
ABC
Or
Aa
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 AB 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
Aas/bD
BaBa/b
Cabb/DD
DaDa
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
saAa/aBC
Aas
BaBa/b
Cabb
2) Sasb/ℇ
AaA
A non generating can be eliminated
Sasb/εis useful production

4) SA Sas/A/C
AaA/ℇ Aa
BbA Baa
cacb
C- non generating (elimination)
Sas/A
Aa
Baa
B-non reachable
Sas/A
Aa

5) AaA/a/Bb/cC
AaB
Ba/Aa
CcCD
Dddd
C- non generating
Aan/a/Bb
AaB
Ba/Aa
Dddd
D- non reachable
San/a/Bb
AaB
Ba/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
SABC
ABC/a
BbAC/ ℇ
CSAB/ ℇ
B,C are directly nullable
A is also nullable : ABC
S is also nullable : SABC
SABC/AB/BC/AC/A/B/C
ABC/a
BC/B/C/a
BbAC/bA/bC/b
CCAB/CA/CB/C
2) Sas1b
S1as1b/ ℇ
ℇ- production are S1ℇ can be removed after adding new production obtained by
substituting ℇ for S1 where it occurs to the sigma
Sa s1b/ab
S1a s1b/ab

3) S ABac
ABC
Bb/ ℇ
CD/ ℇ
Dd
A, B, C, S are nullable
SABaC/Bac/AaC/ABa/ac/Aa/Ba/a
AB/C/BC
Bb
CD
Dd

Eliminating unit productions


Any production of CFG of the form,
AB where
A, B, E, V is called unit productions
Replace any sequence of derivation in which
AB1B2....... Bnα,
By a productions that there was the non unit productions
Bnα directly from A
i.e A α

1) SAB
Aa
Be/b
CD
DE/bC
Ed/Ab
Unit Non unit
BC SAB
CD Aa
DE Bb
DbC
BCDE Ed/Ab

Productions generated from E can also be generated from D


Rewrite Dd/Ab/bC
Ed/Ab
Cd/Ab/bc
Bb/d/Ab/bc
Aa
AAB

Example
1. Remove unit productions Non unit productions
SAa/B SAa
BA/bb  Bbb
Aa/bc/B Aa/bc

Dependency graph
A

S B

SAa
Aa/bc/bb
Ba/bc/bb
SAa/a/bc/bb

2. Ia/b/Ia/Ib/I0/I1
FI/(E)
Tf/T*F
ET/E+T

I F T

Non unit production


Ia/b/Ia/Ib/I0/I1
F(E)/a/b/Ia/Ib/I0/I1
TT*F/(F)/a/b/Ia/Ib/I0/I1
EE+T/T*F/(E)/a/b/Ia/Ib/I0/I1

3) SAa/B/Ca
BaB/b
CDb/D
DE/d
Eab
SB
CDE
Rewrite Eab
Dd/ab
CDb/d/ab
BaB/b
SAa/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) AB,C A, B, C are variables
2) Aa, 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.
SABa
Aaab
BAc
Introduce new variables Ba, Bb, Bc
SABBa
ABaBab
BABC
Baa
Bbb
Bcc
2) Introduce additional variables to get the productions in normal form
SaD1 Baa
D1BBa Bbb
ABaD2 Bcc
D2BaBb D1BBa
BABc
Excerise 7.1.2
Grammer SABC/BaB
AaA/BaC/aaa
BbBb/a/D
CCA/AC
DE
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
BbBb/bb/a
CCA/AC
AaA/aC/Bac/aaa
SABC/BaB/AC/Ba/aB/a
2) No unit productions
3) C is the only varaiable which is non generating eliminate C
SBaB/Ba/aB/a
AaA/aaa
BbBb/bb/a
4) CNF Introduce two variables
Ea
Fb
SBEB/BE/EB/a
AEA/EEE
BFBF/FF/a
SBG/BE/EB/a
GEB
AEA/EH
HEE
BFI/FF/a
IBF

You might also like