Professional Documents
Culture Documents
Unit 2
Unit 2
Unit 2
Verifiedby: Approvedby:
UNIT-II
Context-Free Grammar and languages: Definitions and More Examples - Regular languages and Regular
Grammars - Derivation Trees and Ambiguity - Simplified Forms and Normal Forms - Chomsky Normal
Form – Greibach Normal Form. Pushdown Au to mata: Definitions and Examples - A PDA from a Given
CFG - A CFG from a Given PDA. pumping lemma .
SRI MANAKULA VINAYAGAR ENGINEERINGCOLLEGE
SRI MANAKULA VINAYAGAR ENGINEERINGCOLLEGE
2 MARKS
1. What is pumping lemma for regular sets?
A basic result called the pumping lemma , which is a powerful tool for proving certain
languages, is regular or not .
It is also useful in the development of algorithms to answer certain questions concern
infinite automata, such as whether the language accepted by a given FA is finite or infinite.
If a language is regular,it is accepted by a DFA M=(Q,Σ,δ,q0,F) with particular number of states.
Let L be a regular set. the n the re is a constant n such that if Z is any word in L, and |z|≥n. we may
write Z=uvw in such a way that |uv|≤n,|v|≥1 and for all L≥0 the nuviw is in L.
15. Definesubtree?
Asubtree of aderivation treeisaparticularvertex of the tree to ge the rwithallits descendants, the
edges connecting the m and the ir labels.
Itlookslikeaderivation tree,exceptthat the label of the rootmay not be the startsymbol of the
grammar. If a variable A labels the root, the n we call the subtree anA-tree.
Thus “S-tree” is a synonym for “derivation tree” if S is the startsymbol.
SRI MANAKULA
SRI MANAKULA VINAYAGAR
VINAYAGAR ENGINEERING COLLEGE
ENGINEERINGCOLLEGE
16. Give an example for asubtree?
S abbbab A bbba
A subtree looks like a derivation tree except that the label of the root may not be S. It is called A- Tree, if
the label of the root is A.
Example:
the grammar G= ({E}, {+, *, (, ), id}, P,E)
P1 : E E +E
P2 : E E *E
P3 : E id
the input string is ω = id + id * id
If the string is matched with the given production means, it is a correct grammar.
SRI MANAKULA
SRI MANAKULA VINAYAGAR
VINAYAGAR ENGINEERING COLLEGE
ENGINEERINGCOLLEGE
19. Define the RMD wi the xample?
If at each step in a derivation a production is applied to the right most variable the n the derivation is
called “Right Most Derivation (RMD)”.
Example:
the Grammar G= ({E}, {+, *, (, ), id}, P,E)
P1 : E E +E
P2 : E E *E
P3 : E id
the input string is ω = id + id * id
20. Consider a grammar given as G = ({S, A}, {a, b}, P, S), where P consists
of SaAS|a
ASbA | SS | ba
Find Derive the input string ω= aabbaa using LMD and RMD.
Solution
the Productions are
P1: S aAS
P2: S a P3:
A SbA P4:
A SS
P5: Aba
LMD
SaAS byP1
aSbAS byP3
aabAS byP2
aabbaS byP5
aabbaa byP2
the above string is matched with the given string.
SRI MANAKULA
SRI MANAKULA VINAYAGAR
VINAYAGAR ENGINEERING COLLEGE
ENGINEERINGCOLLEGE
RMD
S aAS byP1
aAa byP2
aSbAa byP3
aabAa byP2
aabbaa byP5
the above string is matched with the given string.
22. Consider the grammar G = ({S, A}, {a,b},P,S), where P consists of SaAS | b , ASbA | ba. Draw
its equivalent derivation tree.
A symbol x is useful, if the re exists aderivation S αXβ w , where α,β are sentential
form(VUT)*, w is any string inT*.
the variableorterminalswhichdo not appearinanyderivation of aterminalstringfromastart
symbol are called as uselesssymbol.
38. Consider the grammar G= ({S, C}, {a,b},P,S} where P consists of SaCa,CaCa|b. Find
L(G). Solution:
S aCa
aaCaa
aaaCaaa
……….
aiCai
anban Because C b
the language L(G)={ anban|n>0}
39. What is the language generated by the grammar G= (V, T, P, S) where P={S->aSb|aS->ab}?
S ==> aSb
==>aaSbb
==>aaabbb
i.e the general form is anbn. Thus the language L(G)={ anbn | n≥1}. the language has strings with equal
number of a’s and b’s.
Solution:
the given grammar is SSS | aSb | bSa |
ε Let the input string is abab
LMD1:
S aSb
abSab
bεab
abab
LMD2:
SSS
aSbS
aSbε
aSb
aaSbb
aaεbb
aabb
Here, we get two leftmost derivations for the same input string. Hence it is ambiguous grammar.
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE
41. Find out the context free languages SaSb | aAb;AbAa|ba; (APR
2015)Solution:
the given grammar is SaSb | aAb; AbAa | ba;
(i) SaSb
| aaAbb
| aababb
(ii) SaAb
| abAab
|abbabb
the CFL for the given grammar L(G)={aababb, abbabb, ababb, aaababbb…}
Let G={V,T,P,S}beaCFG the nastring of terminalsand variables αis called asententialformif (ie)
α in T*suchthat is asententialform.
For the same string two different parse trees can be generated. Hence the given grammar is ambiguous.
48. What are the additional features of PDA has when comp are d withNFA?
In PDA we use push down list called stack which is not used in NFA. also we use cache memory and
auxiliary memory which is not present in NFA. We have 7 tuples in PDA the y are M= {Q, Σ, δ, Γ, q0, z0, F)
the additional2tupleswhich are presentinPDAand not inNFA are ΓandZ0.
NFA PDA
the language accepted by NFA is the regular the language accepted by PDA is context free language
language
NFA has no memory PDA is essentially an NFA with a stack (memory)
It can s to re only limited amount of It s to res unbounded limit of information
information
A language/string is accepted only by reaching It accepts a language ei the r by empty stack or by
the final state reaching a final state
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE
58. Give an example for the PDA that accepts the language of the
grammar?
S →AB
A → aA | e
B → aBb|e (e = null state) and check for stringaaaabb.
Solution:
We can construct PDA
M = (Q, Σ, Γ, δ, q0, Z, F) where
Q = {s, f}
Σ = {a, b} terminals of
grammar Γ = variables of
grammar U {Z0}
= {S, A, B, a, b, Z0}
F = {f}
Processing of abcba:
(P, abcba, Z0) ⊢ (q, abcba, SZ0) – R1
⊢ (q, abcba, aSaZ0) – R2
⊢ (q, bcba, SaZ0) – R5
⊢ (q, bcba, bSbaZ0) – R3
⊢ (q, cba, SbaZ0) –R6
⊢ (q, cba, cbaZ0) –R4
⊢ (q, ba, baZ0) – R7
⊢ (q, a, aZ0) – R6
⊢ (q, ϵ, Z0) – R5
⊢ (q, ϵ) – R8
Hence string is accepted. the stack variable is as follows.
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE
64. What are the closure properties of CFL? (NOV 2015) (APR2017)
the closure properties of CFL are
CFL’s are closed under union, concatenation and Kleeneclosure.
CFL’s are closed underhomomorphism
CFL’s are closed undersubstitutions
If L is a CFL and R is a regular language the n, L ⋂R is aCFL.
CFL’s are not closed underintersection
CFL’s are closed undercomplementation.
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE
11 MARKS
Statement:
Let “L” be a regular set. the n the re is a constant n such that if z is any word in L, and |z|≥n. We may
write z=uvw in such a way that |uv|≤n, |v|≥1 and for all i≥0, the n uviw is in L.
Pro of :
If a language is accepted by a DFA M= (Q, Σ, δ, q0, F) the n it is regular with some particular number of
states, say n. Consider a input of n or more symbols a1a2…..am, m≥n and for i=1,2,…..,m.
Let δ (q0, a1a2…..ai) = qi
It is not possible for each of the n+1 states q0,q1,……qn to be distinct, since the re are only different
states. This the re are two integers j and k, 0≤j<k≤n, such that qj=qk. the path labeled a1a2….am in the
transition diagram of M. Since j<k, the string aj+1. ak is of length 1.
2. Explain the closure properties of RegularSets? (11 MARKS) (APR 2016, 2017) (NOV2015)
the re are manyoperationson languagesthatpreserveregularsetsthat the operationsapplied to regular
sets result in regular sets.
Forexample, the union of tworegularsetsisregularsets,sinceifr1andr2 are regul are xpressions de
not ing regular sets L1 and L2, the n r1+r2 de not es L1L2, so L1L2isregular.
Similarly, the concatenation sets is a regular set and the Kleene closure of a regular setisregular.
If a class of languages is closed under a particular operation, we call that fact a closure property of the
class of languages. the closure properties that describes for languages in the class, the re is an algorithm
to construct a representation for the language that results by applying the operation to the se
languages.
1. Union
2. Concatenation
3. Kleeneclosure
4. Complementation
5. Intersection
6. Substitution
7. Homomorphism
1. KleeneClosure
the regular sets are closed under union, concatenation and Kleene closure.
Pro of :
Let Σ be a finite set of symbols and let L, L1 and L2 be sets of strings from Σ*. the concatenation of L1 and
L2, de not ed L1L2, is the set {xy| x is in L1and y is in L2}. That is, the strings in L1L2 are formed by choosing
a string L1andfollowingitbyastring inL2,inallpossiblecombinations.DefineL0={ε}andLi=LLi-1fori≥1.
the Kleene closure of L de not ed L*, is the set
L*=Σi=0Li
and the positive closure of L, de not ed L+, is the set
L+=Σi=1Li SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE
That is, L* de not es words constructed by concatenating any number of words from L. L+ is the same, but the
case of zero words, whose concatenation is defined to be ε, is excluded. not e that L+ contains ε if and only if L
does.
2. Complementation
the class of regular set is closed under complementation. That is if L is a regular set and L ⊆ Σ*,
the n Σ*- L is a regular set.
Pro of :
Let L be L (M) for DFA M= (Q, Σ1, δ, q0, F) and let L⊆Σ*.
Assume that Σ1= Σ, for if the re are symbols in Σ1 not in Σ, we may delete all transitions of M on symbols
not in Σ. the fact that L ⊆ Σ* assumes us that we shall not the reby change the language of M.
If the re are symbols in Σ not in Σ1, the n none of the se symbols appear in words of L. We may the
refore introduce a “dead state” d in to M with δ(d, a)=d for all a in Σ and δ(q, a)=d for all q in Q and a in Σ-
Σ1.
Now to accept each Σ*-L, complement the final states of M. That is let M’ = (Q, Σ, δ, q0, Q-F). the n M’
accepts a word ω if and only if δ (q0, ω) is in Q-F i.e. ω is in Σ*-L. not e that it is essential to the pro of
that M is deterministic and without εmoves.
3. Intersection
the regular sets are closed under intersection.
Pro of :
L1∩L2= (L1UL2), where the overbar de not es complementation with respect to an alphabet including the
alphabets of L1 and L2.Closure under intersection the n follows from closure under union and
complementation.
It is worth not ing that a direct construction of a DFA for the intersection of two regular sets exists. the
construction involves taking the Cartesian product of states, and we sketch the construction as follows:
Let M1= (Q1, Σ, δ1, q1, F1) and M2= (Q2, Σ, δ2, q2, F2) be two deterministic finite au to
mata. Let M= (Q1x Q2, Σ, δ, [q1, q2], F1 x F2)
where for all p1 in Q1, p2 in Q2, and a in Σ,
δ ([p1, p2], a) = [δ1 (p1, a), δ2 (p2, a)].
T(M)=T(M1) ∩ T(M2).
4. Substitution
the class of regular set is closed under substitution.
Pro of :
Let R ⊆ Σ* be a regular set. For each a in Σ let Ra ⊆ Δ* be a regular set. Let f : ΣΔ* be the substitution
defined by f(a)= Ra. Select regular expression de not ing R and each Ra. Replace each occurrence of the
symbol a in the regular expression for R by the regular
SRIexpression
MANAKULA forVINAYAGAR
Ra. ENGINEERING COLLEGE
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE
to prove that the resulting regular expression de not es f(R), observe that the substitution of a union,
product or closure is union, product or closure of substitution. Thus for example f(L1 U L2) = f(L1) U f(L2).
A simple induction on the number of opera to rs in the regular expression completes the pro of .
A type of substitution that is of special interest is the homomorphism. A homomorphism h is a
substitution such that h(a) contains a single string for each a. We generally take h(a) to be the string itself,
ra the r than the set containing that string. It is useful to define the inverse homomorphic image of a
language L to be
h-1(L)={λ| h(x) is in L}
We also use, for string w;
h-1(w)={λ| h(w) = w}
5. Homomorphism
the class of regular set is closed under homomorphism and inverse homomorphism.
Pro of :
Closure under homomorphism follows immediately from closure under substitution since every
homomorphism is a substitution in which h(a) has one member.
to show closure under inverse homomorphism let M= (Q, Σ, δ, q0, F) be DFA accepting L. Let L
be homomorphism from Δ to Σ*.
Construct M’ that accepts h-1(L) by reading symbol a in Δ and simulating M on h(a). Formally, let M’ = (Q, Δ,
δ’, q0, F) and define δ’(q, a) for q in Q and a in Δ to be δ(q, h(a)).
not e that h(a) may be long string or λ but δ is defined on all string by extension. It is easy to show by
induction on |x| that δ’(q0, x) = δ(q0, h(x)). the refore M’ accepts x if and only if M accepts h(x) i.e.
L(M) = h-1(L(M)).
6. Quotients of languages
the class of regular sets is closed under quotient with arbitrary sets.
Pro of :
Let M= (Q, Σ, δ, q0, F) be a finite au to ma to n accepting some regular set R, and let L be an arbitrary
language. the quotient R/L is accepted by a finite au to ma to n M’= (Q, Σ, δ, q0, F’), which behaves like M
except that the final states of M’ are all states q of M such that the re exists y in L for which δ(q0, y) is in
F. the n δ(q0, x) is in F’ if and only if the re exists y such that δ(q0, xy) is in F. Thus M’ acceptsR/L.
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE
not ations:
the capital letters A, B, C, D, E, and S de not e variables ornon-terminals.
the lower case letters a, b, c, d, e, digits and boldface strings are terminals.
the capital letters X, Y, and Z de not e symbols that may be ei the r termin also rnon-terminals.
the lower case letters u, v, w, x, y, and z de not e strings are terminals.
the lower case Greek letters α, β, and γ de not e strings having variables andterminals.
Example:
Consider this grammar as ({E}, {+,*, (,), id}, P, E), where P consists of
EE+E
EE*E
E(E)
Eid
Derivation tree
Let G= (V, T, P, S) be a CFG. A tree is a derivation or parse tree for G if:
1. Every vertex has a label, which is a variable or terminal or ε. i.e. V U TU(ε).
2. the label of the root is S (startsymbol).
3. If a vertex is interior and has label A, the n A must be inV.
4. Ifnhas labelAandvertices n1,n2,n3, ……nk are the sons of vertexn,in orderfrom the left,with
labelsx1,x2,……xkrespectively, the n Ax1,x2,x3,…… ,xk mustbeaproductioninP.
5. If vertex n has label ε, the n n is a leaf and is the only son of itsfa the r.
Subtree:
Asubtree of aderivation treeisaparticularvertex of the tree to ge the rwithallits descendants, the
edges connecting the m and the ir labels.
Itlookslikeaderivation tree,exceptthat the label of the rootmay not be the startsymbol of the
grammar. If a variable A labels the root, the n we call the subtree anA-tree.
Thus “S-tree” is a synonym for “derivation tree” if S is the startsymbol.
S aabbaa A abba
A subtree looks like a derivation tree except that the label of the root may not be S. It is called A-Tree,
if the label of the root is A.
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE
Types of Derivation:
the two types of derivations are
Ifateachstepinaderivationaproductionisapplied to the leftmostvariable the n the derivation is
called “Left Most Derivation(LMD)”.
Ifateachstepinaderivationaproductionisapplied to the right most variable the n the derivation is
called “Right Most Derivation(RMD)”.
Example:
Consider a grammar given as G = ({S, A}, {a, b}, P, S), where P consists
of SaAS |a
ASbA | SS | ba
Derive the input string ω= aabbaa using LMD and RMD.
Solution
the Productions are
P1: S aAS
P2: S a
P3: A SbA
P4: A SS
P5: A ba
Example:
Consider the grammar for ({S, A, B}, {a, b}, P, S) that has the
productions: SbA | aB
AbAA | aS | a
BaBB | bS | b
Solution:
the given productions
are SbA
SaB
AbAA
AaS
Aa
BaBB
BbS
Bb
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE
In the given grammar the re are no useless symbols, no unit productions and no ε-productions.
Step 3:
the only productions P5, P8 are Aa and Bb are of the form CNF.
the following o the r production consists of more than two non-terminal and combination to
replace to form a CNF
SbA SaB
AbAA AaS
BaBB BbS
Consider P1:
SbA
Introduce a new temp variable (Cbb)
∴ SCbA
Consider P2:
SaB
Introduce a new temp variable (Caa)
∴ S CaB
ConsiderP3:
AbAA
ACbAA (Cbb)
ConsiderP4:
AaS
∴ACaS (Caa)
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE
Consider P6:
BaBB
BCaBB (Caa)
Consider P7:
BbS
∴BCbS (Cbb)
Step 4:
the remaining productions P3 and P6 are ACbAA , BCaBB which are not in CNF form. Because
it contains more than two non-terminals.
We have to replace the m.
Every CFL without ε can be generated by a grammar for which every productions of the form Aaα.
Where A is a variable, a is a terminal, and α is a string of variables. This type of G is said to be GNF. So
RHS should contain only one terminal symbol that should be the left most symbols on the RHS followed by
0 or more no of variables.
end;
end
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE
Rules:
In Greibach Normal Form (GNF), it uses the production whose R.H.S. each starts with the terminal
symbol perhaps followed by some variables or non-terminals.
Example:
Convert the following grammar to
GNFform SAA|a
ASS |b
Solution:
P2: A1 a
P3: A2A1A2
P3: A2b
ii. Preliminary verificationstep
the first productions i value=1 and j value=2. So i<j. No need to disturb now.
P1: A1 A2A2
1<2 // temporary correctproduction
the Secondproduction
P2: A1a // already in GNFform
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE
the first productions i value=2 and j value=1. So i<j. No need to disturb now.
P3: A2A1A2
2<1 // temporary correctproduction
the fourthproduction
P4: A2b // already in GNFform
i) Add Bk α
Bk=B2
B2 A2A1
B2 A2A1 B2
∴
A2A1B2
A2 aA1B2
∴
A2aA1
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE
A2 bB2
∴
A2 b
B2 A2A1B2
B2 bB2A1B2 | bA1B2 | aA1A1B2 | aA1B2A1B2
P1: A1 A2A2
A1aA1B2A2 | aA1A2 | bB2A2 |bA2
A1aB1
∴
A1a
A2 aB1A1 | aA1
Example 1:
Consider the Grammar G is the
grammar SS b S | a
Prove that given grammar is ambiguous.
Solution:
Consider the string or word ω= a b a b a b a
LMD1:
S S bS
a bS
a b S bS
a b a bS
a b a b S bS
a b a b a bS
a b a b a b a
LMD2:
SS b S
S b S b S
a b S bS
a b a bS
a b a b S b a
a b a b a b a
RMD:
S S b S
S b a
S b S ba
S b a ba
S b s b a b a
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE
S b a b a ba
a b a b a ba
If both LMD and RMD give same string, it is called ambiguous.
Example 2:
Show that the CFG with production S->a|Sa|bSS|SSb|SbS is ambiguous?
Solution:
Consider the input string or word w=babaa
LMD:
S bSS
baS
babSS
babaS
babaa
RMD:
SbSS
bSa
bSbSa
bSbaa
babaa
Both LMD and RMD are same. Hence the given grammar is ambiguous.
Example 3:
Show that the grammar SaB|ab
AaAB|a
BABb|b is ambiguous.
Solution:
Consider the input string or word ω =ab
LMD
the LMD of G are
SaB
a b
Sab
Here ab is ambiguous available by the grammar and so the given grammar is ambiguous.
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE
Let G= ({S},a,p,S}
Where p1={SSS, S1}
We observe that
L(G)={ an/n≥1}
Consider the word a3 ε L
(G) Now,
LMD1:
SSS
SSS
aSS
aaS
aaa
LMD2:
SSS
aS
aSS
aaS
aaa
Thus a3 has different LMD Grammar is ambiguous. Thus the re exist an unambiguous grammar generating
{ an/n≥1}.
Solution:
the given grammar
EE+E
EE*E
E(E)
E id
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE
LMD1:
E ===>E +E
===>id+E
===> id + E * E
===> id + id * E
===>id + id * id
Parse tree 1:
E
/|\
/ | \
E + E
| /|\
| / |\
id E*E
| |
id id
LMD2:
E ===> E * E
===> E + E * E
===>id + E *E
===>id + id *E
===> id + id * id
Parse tree 2:
E
/|\
/ | \
E * E
/|\ |
/ | \|
E + E id
| |
id id
the given input string id+id*id has two leftmost derivation and parse tree. Hence given grammar is
ambiguous grammar.
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE
Example:
Consider the grammar EE+T |T
TT*F | F
F (E) | id
the input string is id + id * id and id * id + id. Show that it is unambiguous grammar or not ?
Solution:
the given grammar is
EE+T |T
TT*F | F
F (E) |id
LMD:
E ===>E +T
===>T +T
===>F +T
===>id +T
===> id + T * F
===> id + F * F
===> id + id * F
===>id + id * id
Parse tree:
E
/|\
/ | \
E + T
| /|\
T T *F
| | |
F F id
| |
id id
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE
RMD:
E ===> E +T
===>T +T
===>T * F +T
===>F * F + T
===> id * F + T
===>id * id +T
===>id * id +F
===>id * id + id
Parse tree:
E
/|\
/ | \
E + T
| |
T F
/|\ |
/ | \id
T * F
| |
F id
|
id
Hence, we have unique leftmost derivation and unique rightmost derivation for the input string. Hence
the given grammar is unambiguous grammar.
16. Simplify the following grammar and find its equivalent inCNF
SAB | CA, BBC | AB , Aa , CaB| b (5 MARKS) (APR2015)
Solution:
CNF Format:
NT NTNT
NT T
Where , NT – Non terminal
T – Terminal
Given
P : SAB | CA
BBC | AB
Aa
CaB | b
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE
Step 1:
In the given grammar the below three production are already in CNF form. the y
are : SAB | CA
BBC | AB
Aa
to convert the remaining production in to CNF, do the following
Simplification of grammar:
(i) Elimination of εproduction
ε production of the form :
NT ε
the re is ε production in the given grammar. the refore P’ is same as P
(ii) Elimination of Unitproduction
Unit production of the form:
NT NT
the re is Unit production in the given grammar. the refore P’’ is same as P’
(iii) Elimination of Useless symbolproduction
the symbol B doesn’t lead to a terminal string . the refore B is the useless symbol. the refore
eliminate B from P’’
Before elimination After elimination
P’’: SAB | CA P’’’: SCA
BBC |AB Aa
Aa Cb
CaB | b
(iv) Elimination of symbol from P’’’ that is not reachable from startstate
S A C
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE
Now all the states are reachable from the start state. .Hence the re is no elimination from p’’’.
Simplified CNF Productions
P’’’: SCA
Aa
Cb
17. Find the GNF equivalent of the grammar SAA | 0, A SS| 1 (6 MARKS) (MAY 2015)
GNFFormat:
NT TNTNTNT…………..
NT T
Where , NT – Non terminal
T – Terminal
(or)
A context free grammar is said to be in Greibach Normal Form (GNF) if all productions are in the
following form:
A αX
A a
where
A is a non-terminalsymbols
α is a terminalsymbol
X is a sequence of non-terminalsymbols.
Every CFL without ε can be generated by a grammar for which every productions of the form Aaα.
Where A is a variable, a is a terminal, and α is a string of variables. This type of G is said to be GNF. So RHS
should contain only one terminal symbol that should be the left most symbols on the RHS followed by 0 or
more no of variables.
the given grammar is
SAA | 0
A SS | 1
Convert the following grammar to GNF form
SAA
S0 A
SS
A1
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE
Solution:
Step1: Simplification of a Grammar
In the given grammar the re are no useless symbols, no unit productions and no ε-productions.
P2: A1 0
P3: A2A1A1
P3: A21
v. Preliminary verificationstep
the first productions i value=1 and j value=2. So i<j. No need to disturb now.
P1: A1 A2A2
1<2 // temporary correctproduction
the Secondproduction
P2: A10 // already in GNFform
the first productions i value=2 and j value=1. So i<j. No need to disturb now.
P3: A2A1A1
2<1 // temporary correctproduction
the fourthproduction
P4: A21 // already in GNFform
iv) Add Bk α
Bk=B2
B2 A2A1
v) add Bk αBk
B2 A2A1B2
vi) remove Ak Akα
Now A2 A2A2A1 is removed
B2 A2A1 B2
A2A1B2
A2 0A1B2
A20A1
A2 1B2
A2 1
B2 A2A1B2
B2 1B2A1B2 | 1A1B2 | 0A1A1B2 | 0A1B2A1B2
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE
P1: A1 A2A2
A10A1B2A2 | 0A1A2 | 1B2A2 |1A2
A10B1
A10
Substitute all A1 production value in above P 3 productions, we get
P3: A2 A1A1
A2 0B1A1 | 0A1
For the same string two different parse trees can be generated. Hence the given grammar is ambiguous.
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE
Step 1:
Simplification of grammar:
(v) Elimination of ε-production
ε production of the form :
NT ε
In the given production, a production Bε is a ε-production. the refore P’ becomes
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE
All the symbols in P’’ leads to a terminal string . the refore P’’’ is same as P’’
(viii) Elimination of symbol from P’’’ that is not reachable from startstate
S A B
Now all the states are reachable from the start state. .Hence the re is no elimination from p’’’.
Simplified Grammar
P’’’: SaSA | aAA | b
A bBBB
Bb
Step 2:
Convert to CNF:
(i) Name the productions
P1: SaSA
P2: SaAA
P3: Sb
P4: A bBBB
P5: Bb
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE
(ii) Consider the production
P1: SaSA
Substitute Da a
SDaSA
Substitute D1 SA
SDaD1
Now the production P1 in the form of CNF
P1 Productions are
S DaD1
D1 SA
Da a
Let the string w = ababa, below is the two different derivation tree for the same string.
22. Show that the language L = { ai bi ci | i>=1} is not contextfree. (7 MARKS) (NOV2015)
Solution:
the given language L = { ai bi ci | i>=1}
Let i=p
u = ap
vwx= bp-i
y = cp
z= uviwxiy
= u vi-1 v1 w xi-1 x1y
= uvwx vi-1 xi-1 y
z = uvwxvxi-1y --------- (1)
vx= b p-m
Substitute in equation (1)
z = ap bp (b p-m)i-1 cp
z = apbpb(p-m)(i-1)cp----------------- (2)
case (i)
Let i = 1 p = 3 m= 2
Substitute in equation (2)
z = a3 b3 b(3-2)(1-1) c3
z = a3 b3 b(1)(0) c3
z = a3 b3 c3 ε L
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE
case (ii)
Let i = 2 p = 3 m= 2
Substitute in equation (2)
z = a3 b3 b(3-2)(2-1) c3
z = a3 b3 b(1)(1) c3
z = a3b4c3 L
the given language is not accepted.
Derivation Tree:
TA
A +TA | ε
T MB
B *MB| ε
M id
B’s Production:
TMB B *MB
Bε Bε
T Mε B *Mε
TM B *M
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE
Consider the P1
Production: P1: E
E + T
Introduce a temp variable Da +
E E Da T
Introduce a new temp variable Db EDa
∴ E DbT
Consider the
P2Production: P2:
E T *F
Introduce a temp variable Dc *
E T Dc F
Introduce a new temp variable Dd TDc
∴ E DdF
Consider the
P3Production: P3:
T T *F
Already a temp variable Dc *
T T Dc F
Introduce a new temp variable Dd TDc
∴ T DdF
Consider the P4
Production: P4: T
(E)
Introduce a temp variable De (
T De E )
Introduce a new temp variable Df )
T De E Df
Introduce a new temp variable Dg De E
∴ T Dg Df
Consider the
P6Production: P6:
F (E)
Introduce a temp variable De (
F De E )
Introduce a new temp variable Df
) F De E Df
Introduce a new temp variable Dg De E
∴ F Dg Df
the Push down Au to ma to n (PDA) have an input tape, a finite control and a stack. the stack is a
string of symbols from some alphabet. the leftmost symbol of the stack is considered to be at the “
to p” of the stack. the device will be non-deterministic, having some finite number of choices of
moves in each situation.
the moves will be of two types.
1. In the first type of move, an input symbol is used. Depending on the input symbol, the to p
symbol on the stack, and the state of the finite control, a number of choices are possible. Each
choice consists of a next state for the finite control and a (possibly empty) string of symbols to
replace the to p stack symbol. After selecting a choice, the input head is advanced one symbol.
2. the second type of move (called a ε-move) is similar to the first, except that the input symbol is
not used, and the input head is not advanced after the move. This type of move allows the PDA
to manipulate the stack without reading input symbols.
Accepted languages
1. For PDAM=(Q,Σ, Γ, δ,q0, z0, F)we define L(M), the language acceptedbyfinal state, to be
{w| (q0, w, z0) ├* (p, ε, γ) for some p in F and γ ε Γ *}.
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE
2. We define N (M), the language accepted by empty stack (or null stack) to be
{w| (q0, w, z0) ├* (p, ε, ε) for some P inQ}.
A PDA is simply an NFA augmented with an "external stack memory". the addition of a stack provides
the PDA with a last-in, first-out memory management capability. This "Stack" or "pushdown s to re" can be
used to record a potentially unbounded information. It is due to this memory management capability with
the help of the stack that a PDA can overcome the memory limitations that prevents a FA to accept many
interesting languages like {an bn | n>=0}. Although, a PDA can s to re an unbounded amount of information on
the stack, its access to the information on the stack islimited.
It can push an element on to the to p of the stack and pop of f an element from the to p of the
stack. to read down in to the stack the to p elements must be popped of f and are lost. Due to this
limited access to the information on the stack, a PDA still has some limitations and can not accept some o
the r interesting languages.
As shown in figure, a PDA has three components: an input tape with read only head, a finite control and a
pushdown s to re. the input head is read-only and may only move from left to right, one symbol (or cell) at
a time. In each step, the PDA pops the to p symbol of f the stack; based on this symbol, the input symbol it
is currently reading, and its present state, it can push a sequence of symbols on to the stack, move its read-
only head one cell (or symbol) to the right, and enter a new state, as defined by the transition rules of the
PDA. PDA are nondeterministic, by default. That is, - transitions are also allowed in which the PDA can pop
and push, and change state without reading the next input symbol or moving its read-only head. Besides this,
the re may be multiple options for possible next moves.
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE
Transition Function
1. Let ((P,a,β),(q,γ)) εδ
2. It means that we,
a. Read a from tape.
b. Pop the string β from stack.
c. Move from stack P to stateq.
d. Push string γ on to stack.
3. We will draw itas
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE
Pushing and Poping
When we push β, we must push the symbols of β as we read the m right to left.
When we pop γ, we pop the symbols of γ as we read the m from left to
right. i.e, If we push abc, the new can pop a, the n pop b, the n pop c.so e push
like this
Left most
a
b
c
Rightmost
Thus, if we push the string abc and the n pop it, we will get back
abc.
Accepting Strings
1. After processing the string on the tape.
the PDAisinei the rafavorableoranunfavorablestate,and
the stack is ei the r empty / not empty.
2. the input string is acceptedif
the final state is favorable,and
the stack isempty.
From CFL’s to
PDA’s
1. For a context free grammar G, the re is a equivalent CFG withGNF.
2. We can construct PDA for the CFG +GNF.
3. the PDA we are about to construct will represent the derivation by keeping the variables in
the right part of the sentential form on its stack, while the left part, consisting of terminals is
identical with the inputread.
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE
4. Webeginby pumping the startsymbolon the stack.Afterthat, to simulate the application of production
A→ ax, we must have the variable A on to p of stack and terminal a as the input symbol. the
variable on the stack is removed and replaced by the variable stringx.
Example:
For a CFG G= ({S}, {a, b}, S, P), where S→ aSbb|a. Construct a PDA?
Solution:
Step 1:
We must construct equivalent CFG + GNF by changing productions
to S→ aSA | a
A→ bB
B→ b
Step 2:
Now we can construct PDA in the following way M= (Q, Σ, Γ, δ, q0, Z, F) where Q= {q0, q1, q2} → q0-
initial state, q2- final state.
Σ = terminals of grammars = {a,
b} Γ = variables of grammar U
{Z0}
= VG U {Z0}
F = {q2} final state and δ is
1) First the start symbol S is put on δ(q0, ϵ, Z0) = {q1,SZ0}.
2) the production S → aSA will be simulated by PDA by SA, while reading a from the input. Similarly,
the rule S → a should cause the PDA to read an a while simply removing S. the refore the
productions are represented in PDA by δ(q1, a, S ) = {(q1, SA), (q1,ϵ)}
3) In a similar manner, the o the r productions
are δ(q1, b, A) = {(q1, B)} for rule
A→bB
δ(q1, b, B) = {(q1, ϵ)} for rule B→b
4) the appearance of stack start symbol on to p of the stack signals the completion of derivation
and PDA is put in final stateby
δ(q1, ϵ, Z0) = {(q2, ϵ)}
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE
30.Given G (V, T, P, S) where V= {S, a, b, c} T= {a, b, c} P= {S → aSa, S→ bSb, S → c}. Construct a PDA
and run forstring abcba. (5MARKS)
Solution
Let M = (Q, Σ, Γ, δ, q0, Z0, F) be a PDA.
Q = {p, q} p = start state q= final state
Σ = {a, b, c} terminals of grammar
Γ = {S, a, b, c} U {Z0}
F = {q}
δ is
R1 : δ(P, ϵ, Z0) = {(q, SZ0)} start symbol put on the
stack R2 : δ(q, ϵ, S) = {(q, aSa)} prod 1
Processing of abcba:
(P, abcba, Z0) ⊢ (q, abcba, SZ0) – R1
⊢ (q, abcba, aSaZ0) – R2
⊢ (q, bcba, SaZ0) – R5
⊢ (q, bcba, bSbaZ0) – R3
⊢ (q, cba, SbaZ0) –R6
⊢ (q, cba, cbaZ0) –R4
⊢ (q, ba, baZ0) – R7
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE
⊢ (q, a, aZ0) – R6
⊢ (q, ϵ, Z0) – R5
⊢ (q, ϵ) – R8
Hence string is accepted. the stack variable is as follows.
31.Design a PDA that accepts the language {anbn|n>=0}. (5MARKS) (APR-22)
the PDA that accepts the language like {an bn |
n>=0} M= (Q, Σ, Γ, δ, q0, z0,F)
Q = {q1, q2, q3, q4}
Σ = {a, b}
Γ = {a, b, z}
F= {q1, q4}
δ consists of the following
transitions 1. δ (q1, a , z) = {(q2
,az)}
2. δ (q2, a , a) = {(q2 ,aa)}
3. δ (q2, b , a) = {(q3 , ε)}
4. δ (q3, b , a) = {(q3 , ε)}
5. δ (q3, ε , z) = {(q4 , z)}
Informally, whenever the PDA M sees an input a in the start state q1 with the start symbol z on the to p of
the stack it pushes a on to the stack and changes state to q2. On state q2 if it sees anymore a, it simply
pushes it on to the stack. not e that when M is on state q2, the symbol on the to p of the stack can only
be a. On state q2 if it sees the first b with a on the to p of the stack, the n it needs to start comparison
of number s of a's and b's, since all the a's at the beginning of the input have already been pushed on to
the stack. It start this process by popping of f the a from the to p of the stack and enters in stateq3.
On state q3, it expects only b's in the input (if it sees any more a in the input thus the input will not be in
the proper form of anbn). Hence the re is no more on input a when it is in state q3. On state q3 it pops of f an a
from the to p of the stack for every b in the input. When it sees the last b on state q3 (i.e. when the input
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE
is exhausted), the n the last a from the stack will be popped of f and the start symbol z is exposed. This is
the only possiblecasewhen the input(i.e.onε-input) the PDAMwillmove to stateq4whichis anacceptstate.
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE
Example 1
the computation of the PDA on a given input using the IDs and next move relations. For example, following
are the computation on two input strings.
(i) Let the input be aabb.We start with the start configuration and proceed to the subsequent IDs using
the transition function defined
δ(q1,aabb,z) |-- (q2 , abb,az)
|-- (q2, bb ,aaz)
|-- (q3, b , az)
|-- (q3, ε ,z)
|-- (q4, ε ,z)
No fur the r move is defined at this point. Hence the PDA gets stuck and the string aabab is not accepted.
the orem: CFLs are closed under union operation but not under intersection.
If L1 and L2 are CFLs, the n L1 U L2 is a CFL.
Pro of :
1. LetL1 andL2 begeneratedby the CFG, G1=(V1,T,P1,S1)andG2 =(V2,T2,P2,S2),respectively.
2. Without loss of generality, subscript each nonterminal of G1 with a 1, and each nonterminal of G2 with
a 2 (so that V1 ∩ V2 =Ф).
3. Define the CFG, G, that generates L1 U L2 as follows: G = (V1 U V2 U { S }, T1 U T2, P1 U P2 U {S S1 |
S2}, S).
4. A derivation starts with ei the r S S1 or SS2.
5. Subsequent steps use productions entirely from G1 or entirely fromG2.
6. Each word generated thus is ei the r a word in L1 or a word inL2.
Explanation:
We will take one input string: "aabbbcc"
1. Scan string from left to right
2. First input is 'a' and follow the rule:
3. on input 'a' and STACK alphabet Z, push the input 'a' in to STACK as : (a,Z/aZ) and statewillbeq0.
4. Second input is 'a' and so follow the rule:
5. On input 'a'and STACK alphabet 'a', push the input 'a' in to STACK as:(a,a/aa)andstatewillbeq0.
6. Third input is 'b' and so follow the rule:
7. on input 'b' and STACK alphabet 'a', do not hing as : (b,a/&epsiloon;) and state will benowq1.
8. Fourth input is 'b' and so follow the rule:
9. on input 'b' and STACK alphabet 'a', do not hing as : (b,a/&epsiloon;) and state willremainq1.
10. Fifth input is 'b' and so follow the rule:
11. on input 'b' and STACK alphabet 'a', do not hing as : (b,a/&epsiloon;) and state willremainq1.
12. Sixth input is 'c' and to p of STACK is 'a' so follow the rule:
13. oninput 'c' andSTACK alphabet'a' andstateis q1,popone'a' as:(c,a/ε) andstate willbeq2.
14. Seventh input is 'c' and to p of STACK is 'a' so follow the rule:
15. oninput 'c' andSTACK alphabet'a' andstateisq2,popone'a' as:(c,a/ε)andstatewillremainq2.
16. We reached end of the string, so follow the rule:
17. on input ε and STACK alphabet Z, go to final state(qf) as : (ε,Z/Z).
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE
36.State and explain push down Au to mata. Design a push down Au to mata for the languageanbn over
{a, b}+. (11 MARKS) (APR 2017) (OCT-22)
Explanation
We will take one input string:"aabb"
1. Scan string from left to right.
2. First input is 'a' and follow the rule:
3. on input 'a' and STACK alphabet Z, push the input 'a' in to STACK as : (a,Z/aZ) and statewillbeq0.
4. Second input is 'a' and so follow the rule:
5. oninput 'a'andSTACK alphabet'a', push the input'a'in to STACKas:(a,a/aa)andstate willbeq0.
6. Third input is 'b' and so follow the rule:
7. on input 'b' and STACK alphabet 'a', pop STACK with one 'a' as : (b,a/&epsiloon;) and state will be now
q1.
8. Fourth input is 'b' and so follow the rule:
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE
9. on input 'b' and STACK alphabet 'a' and state is q1, pop STACK with one 'a' as : (b,a/&epsiloon;) and
state will beq1.
10. We reached end of the string, so follow the rule:
11. on input ε and STACK alphabet Z, go to final state(qf) as : (ε,Z/Z).
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE
Conversion from CFG to PDA :
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE
Conversion from PDA to CFG:
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE
2 MARKS
1. Find out the context free languages SaSb | aAb; AbAa | ba; (APR 2015) (Ref.Qn.No.44,Pg.no.12)
2. What is sentential form? (APR 2015) (Ref.Qn.No.45,Pg.no.12)
3. Generate the CFG for the language L= {0*1*}. (NOV2015)
4. What is meant by derivation? (NOV 2015) (Ref.Qn.No.13,Pg.no.4)
5. What do you mean by Griebach Normal Form? (NOV 2015) (Ref.Qn.No.40,Pg.no.10)
6. Define Chomsky Normal Form (CNF)? (APR 2015, 2016) (Ref.Qn.No.39,Pg.no.10)
7. What is context free grammar? (APR 2016) (NOV 2017) (Ref.Qn.No.8,Pg.no.3)
8. List the closure properties of regular languages. (NOV 2016) (Ref.Qn.No.6,Pg.no.4)
9. When a grammar is in Chomsky Normal Forms? (NOV 2016) (Ref.Qn.No.39,Pg.no.10)
10. State the pumping lemma forRegular languages.(APR2016) (NOV2017) (Ref.Qn.No.2,Pg.no.2)
11. What is an ambiguous grammar? (NOV 2017) (Ref.Qn.No.28,Pg.no.9)
12. State the pumping lemma for Context free languages. (APR 2015) (Ref.Qn.No.32,Pg.no.11)
13. What are the closure properties of CFL? (NOV 2015) (Ref.Qn.No.33,Pg.no.12)
14. What is push down au to mata? (APR, NOV 2016) (Ref.Qn.No.1,Pg.no.2)
15. Define PDA accepted by empty stack. (NOV 2016) (Ref.Qn.No.12, Pg.no.4)
16. How can we prove that a given language is not a Context Free language? (NOV2016) (Ref.Qn.No.32,
Pg.no.11)
17. List out the properties of the CFL. (APR 2017) (Ref.Qn.No.33, Pg.no.12)
10 MARKS
APRIL 2015 (REGULAR)
1. (a). Simplify the following grammar and find its equivalent inCNF;
SAB | CA, BBC | AB, Aa , CaB|b (5 MARKS) (Ref.Qn.No.16,Pg.no.36)
(b). Find the GNF equivalent of the grammar (5 MARKS) (Ref.Qn.No.17,Pg.No.38)
SAA | 0 , A SS | 1
(OR)
2. (a).Show that the grammarSa|Sa|bSS|SSb|SbSis ambiguous.(5MARKS)(Ref.Qn.No.18,Pg.no.41)
(b). Consider the productions
SaB|bA AaS | bAA |a B bS|aBB|b
For the string aaabbabbba , find a left most derivation. (5 MARKS) (Ref.Qn.No.19, Pg.no.42)
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE
NOVEMBER 2015 (ARREAR)
1. (a). If G is the grammar SSbS | a show that G is ambiguous. (5 MARKS)(Ref.Qn.No.21,Pg.no.46)
(b). Show that the language L = { ai bi ci | i>=1} is not context free. (5 MARKS) (Ref.Qn.No.22,
Pg.no.46) (OR)
2. (a). What are the closure properties of regular sets? Explain. (10 MARKS)(Ref.Qn.No.2,Pg.no.14)
(b). Find the derivation tree of a * b + a* b given that a * b + a* b is in L(G).where G is given
by S S + S , S S * S , S a|b. (5 MARKS) (Ref.Qn.No.23, Pg.no.47)
3. Briefly discuss about Chomsky normal form. (5 MARKS) (Ref.Qn.No.10,Pg.no.24)
(OR)
MAY 2019
(OR)
MAY 2018
1. Convert PDA to CFG where PDA is given by P({q0,q1},{0,1},{X,Z0},,q0,Z0,q1) Transition definedby
δ (q0,0,Z0)=(q0,XZ0)
δ (q0,0,X)=(q0,XX)
δ (q0,1,X)=(q1,ε)
δ (q1,1,X)=(q1,ε)
δ (q1, ε,X)=(q1,ε)
δ (q1, ε,Z0)=(q1, ε)
(OR)
2. (a) construct a PDFA for the language L={an bn+m cm /n>=1,m>=1}(5marks)
(5marks)
MAY 2019
(OR)
2. Explain the closure properties of context free languages.