Flat Unit-5 LM
Flat Unit-5 LM
UNIT – V
CONTEXT FREE GRAMMARS
Objective: To employ with Context Free Grammars and Push-Down Automata
Syllabus:
Ambiguity in context free grammars-definition, examples, Minimization of Context Free
Grammars-removal of useless symbols, ε-productions and unit productions, Chomsky
normal form-definition, examples on conversion of given CFG to CNF, Greibach normal
form-definition, examples on conversion of given CFG to GNF, Pumping Lemma for Context
Free Languages-examples, Enumeration of Properties of CFL (proofs not required)
Applications of CFLs.
Push down automata:
Push down automata, model of PDA, design of PDA, introduction to DCFL and DPDA,
applications of PDA
Outcome: To design PDA’s for the context free languages.
A A
A - A
A + A
A + A
a A - A
a a a
a a
Parse Tree for the above derivation: Parse Tree for the above derivation:
Left Recursion: A grammar is said to be Left recursive if and only if it is of the form AAα
such that A is a variable, and α Є (VUT)*.
Example: SS+S
SSa
AA α1|A α2....A αn|β1| β2... βm where β1, β2... βn doesn’t starts with A. It is said to be
Left Recursive production. This can be eliminated by introducing the following
productions.
Examples:
Left Factoring: Two or more productions of a variable A of the grammar G is said to be left
factoring if all the productions of the form
Aα β1| α β2|..... α βn where βi Є(VUT)* and βi doesn’t starts with α then all the
productions are said to have common left factor α example
Sab|ac|ad Here a is called common left factor.
Elimination of Left Factoring: Consider the A productions which are having the left
factoring as follows
Aα β1| α β2|..... α βn| γ1| γ2 ......| γm
γ 1, γ2 ........ γm doesn’t starts with α
we can eliminate left factoring in the following way
A αA1| γ1| γ2 ......| γm
A1β1|β2.......βm.
Examples:
1. Consider the CFG SaSa|aa|b eliminate left factoring.
Sol: In the given grammar there exist left factoring . a is common left factoring. We can
eliminate left factoring in following way.
SaA1|b
A1 Sa|a
2. Consider the grammar EE*T|F|E+T|T
TF
Fid eliminate left factoring if any.
Sol: In the given grammar there exist left factoring . ‘id’ is common left factoring.
To eliminate that left factoring we can write the productions as follows.
EidE1
E1*T|+T|id
Tid
Simplification of Grammars:
Grammar may contain some extra symbols , these will increase the length of the grammar.
Elimination of these unnecessary symbols is called simplification of CFGs.
Simplification of grammars generally includes
a. Elimination of useless symbols.
b. Elimination of Є productions.
c. Elimination of unit productions of the form AB.
a. Elimination of useless symbols: A symbol is useless if it can not derive a terminal
or it is not reachable from start symbol.
Examples:
Sol: In the given grammar there is no production for B. So we have to eliminate the
productions which contains B.
SCA
Aa
Cb.
b. Elimination of Є productions:
If some CFL contains the word Є then CFG must have a Є-production. However if a
CFG has a Є-production then the CFL doesn’t necessarily contain Є.
Example: SaX
XЄ
CFL={a}
Nullable Variables: In a given Context free grammar a non terminal X is nullable if
1. There is a production XЄ
2. There is a derivation that starts at X and leads to Є
i.e X-----Є
Examples:
Examples:
3. Eliminate string of terminals on the right hand side of the productions if it exceeds
one as follows,
for example if we have Sa1a2a3 where a1,a2,a3 are terminals
then introduce a non terminal ‘Cai’ for terminal ai as
Ca1a1
C a2a2
.
.
.
Canan
4. To restrict number of variables in the right hand side introduce new variable and
separate them as follows, suppose we have the production with N non terminals as
shown below
YX1X2X3X4X5
Add N-2 new productions using n-2 new non terminals and modify the productions
as shown in below.
YX1R1
R1X2R2
R2X3R3
R3X4X5
Where Ri are new non terminals.
Note: the languages generated by the new context free grammar is same as the
original context free grammar
Problems:
1. Convert the following CFG to CNF SAB|aB , Aaab|Є , BbbA
ACaCaCb
ECaCa
AECb
BDA
BCbCb
Then the final production in CNF is
SAB
SDA
SCbCb
SCaB
DCbCb
AECb
ECaCa
BDA
BCbCb
Caa
Cbb
Tuple representation for the grammar:
V=Set of variables={S,A,B,Ca,Cb,D,E}
T= Set of terminals={a,b}
P= productions={ SAB,SDA, SCbCb, SCaB, DCbCb, AECb, ECaCa, ,BDA,
BCbCb , Caa, Cbb}
S= start symbol= S.
AA α1|A α2....A αn|β1| β2... βm where β1, β2... βn doesn’t starts with A. It is said
to be Left Recursive production. This can be eliminated by introducing the following
productions.
A βA1
A α A1 | Є
1
Examples on GNF:
1. Convert the following CFG to GNF
SAA|a, ASS|b.
Sol:
A1A2A2|a----------1
A2A1A1|b----------2
STEP 3:
Take eq 1 A1A2A2|a here j >i so, leave the production as it is
Take eq 2 A2A1A1|b here j < i so, apply substitution rule i.e substitute A 1
production in A2 production.
A2A2A2A1|aA1|b here j = i so, apply left recursion.
A2aA1Z|bZ|aA1|b
ZA2A1Z|A2A1
Substitute A2 in Z then
ZaA1ZA1Z|bA1Z|aA1A1Z|bA1Z|aA1ZA1|bZA1|aA1A1|bA1
STEP 4: Substitute A2 in A1 then
A1aA1ZA2|bZA2|aA1A2|bA2|a
A2aA1Z|bZ|aA1|b
00 1 1 2 2
U V WXY
For i=2 Pump V,X
UV2WX2Y = 00121222
=021323 does not belongs to L.
There is a contradiction. So our assumption is wrong. Hence given grammar is not
Context free language.
2. Prove that L = {ww|w is bit string} is not in CFL.
Assume L is in Context free language.
Z=0n1n0n1n Є L
UViWXiY Є L
Z=0n 0n 1n 0n 1n
U VWX Y
Pump V, X
Z= 0n 0n+1 1n 0n+1 1n does not belongs to L.
Enumeration properties of CFL:
Context free languages are closed under substitution , union, concatenation, closure,
positive closure, reversal homomorphism, inverse homomorphism,
Context free languages are not closed under intersection, difference, and complement.
Control Unit
Z0
Stack
The PDA consists of three components.
1. A input tape
2. A finite control
3. A stack structure
An input tape consists of a linear configuration of cells each of its contain a character
from the input alphabet.
The control unit has some pointer (reading head). This points to the current symbol
which is to be read. The head position over the current stack element can read and write
special stack character from that position.
The stack is also a sequential structure that has a first element and close in either from
the other end.
Formal Definition of PDA:
A finite state push down automata is a seven Tuple M(Q,∑, δ,Γ,F,Z0,q0) where
Q=Finite set of states.
∑=Finite set of Input alphabet
Γ=Finite set of stack alphabet
q0=Initial state
F=Finite set of final state F ⊆ Q
Z0=Initial stack symbol Z0 ∈ Γ
δ=Transition function δ: Q X (∑ U *ε+) X Γ--> Q X Γ*
2. Design PDA which accepts equal number of a's and b's over ∑=*a,b+
δ(q0,a,Z0)=(q0,aZ0)
δ(q0,a,a)=( q0,aa)
δ(q0,b, Z0)=( q0,b Z0)
δ(q0,b,a)=( q0,ε)
δ(q0,a,b)=( q0,ε)
δ(q0,b,a)=( q0,ε)
δ(q0,ε, Z0)=( q1, Z0)
Deterministic PDA:
The PDA that has one choice of move in any state is called deterministic PDA.
A PDA P={ Q,∑, δ,Γ,F,Z,q0} is deterministic if and only if
1. δ(q,a,X) has atmost one number of q ∈a, a∈ Z or a=ε and X∈Γ.
2. if δ(q,a,X) is not empty for some a∈∑ then δ(q,ε,X) must be empty
DPDA is less powerful than NPDA
The context free languages could be recognized by NPDA.
Note: CFL's are not closed under complementation But DCFL's are closed . Hence if the
CFL's is formed to be closed under complement then it is DCFL.
4. While converting the context free grammar into Greibach normal form, which of the
following is not necessary
a) Elimination of null production b) Elimination of unit production
c) Converting given grammar in Chomsky normal form d)None of these
11. The push down automata indicate the acceptance of input string in terms of
a) Finial state b) Empty store
c) Both (a) and (b) d) None of these
16. A context free grammar G is in Chomsky normal form if every production is of the
form
a) A → BC or A → A b) A → BC or A → a
c) A → BCa or B → b d) None of these [
]
27. S –> aSa| bSb| a| b ;The language generated by the above grammar over the alphabet {a,b}
is the set of [ ]
a) All palindromes. b) All odd length palindromes.
c) Strings that begin and end with the same symbol d) All even length palindromes.
5. Let L denotes the language generated by the grammar S→0S0/00. GATE 1999
Which of the following is true?
a) L = 0+ b) L is regular but not 0+
c) L is context free but not regular d) L is not context free
6. consider the following two statements: GATE 2001
S1: {0 | n ≥1 }is a regular language
2n