Unit 2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 83

SRI MANAKULA VINAYAGAR ENGINEERINGCOLLEGE

DEPARTMENT of COMPUTER SCIENCE ANDENGINEERING

SUBJECT NAME: AU to MATA AND COMPILER DESIGN SUBJECT CODE:U20CST305

Prep are d By:


Mr.M.Shanmugam, AP/CSE

Mr. A. Prakash, AP/CSE

Mrs. S.Deeba, AP/CSE

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.

2. List the applications of the pumping lemma ? (APR 2016) (NOV2017)


the applications of pumping lemma for regular set are
 the pumping lemma is a powerful tool providing and proving certain language is regular or not .
 It is also useful in the development of algorithms to answer certain question concern
infinite automata, such as whether the language accepted by a given FA is finite or infinite.
(Or)
1) the pumping lemma is useful in proving that certain sets are not regular. the application is
an “adversary arguments” of the following form.
2) Select the language L to prove non-regular.
3) the “adversary” picks n, the constants mentioned in the pumping lemma .
4) Select a string z in L.
5) the adversary breaks zin to u,v and w,subject to the constraints that|uv|≤n and |v|≥1.
6) A contradiction to the pumping lemma by showing, for any u, v, and w, determined by the
adversary, that the re exists an i for which uviw is not in L. It may the n be concluded that L is not
regular.
SRI MANAKULA
SRI MANAKULA VINAYAGAR
VINAYAGAR ENGINEERING COLLEGE
ENGINEERINGCOLLEGE
3. What is homomorphism with example?
A string homomorphism is a function on strings that works by substituting a particular string for each
symbol.
Example:
the function h given by h(0) = abb and h(1) = ba is a homomorphism which replaces each 0 by abb
and each 1 by ba. Thus h(1011) = baabbbaba.

4. What is inverse homomorphism?


Homomorphism can also applied in reverse and in this mode the y also preserve regular language. That
is, suppose h is a homomorphism from some alphabet Σ to strings in ano the r (possibly the same)
alphabet T-1. Let L be a language over alphabet T. the n h-1 is the set of strings w in Σ* such that h(w) is in
L.

5. Define Context Free Grammar(CFG)? (APR , NOV2016)(OCT-22)


 A context free grammar (CFG) is a finite set of variables ( also called non-terminals or syntactic
categories) each of which represents a language.
 CFG is a way of describing languages by recursive rules or substitution rules called productions.
 A CFG consists of sets of variables, a set of terminal symbols and a start variable, and productions.
Each production consists of a starting variable and a body consists of a string of zero or more
variables or terminals.

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

6. Write the formal specification of Context Free Grammar(CFG)?


A Context Free Grammar (CFG) is de not ed G= (V, T, P, S),
Where
1) Vis a finite set of variables or non-terminals.
2) Tis a finite set of terminal symbols.
3) Pis a finite set of productions; each production is of the form Aα, where A is a non-terminal
or variable and α is a string of symbols from (VUT)*.
4) Sis a special variable called the start symbol.

7. What are the notations used in Context Free Grammar(CFG)?


1) the capital letters A, B, C, D, E, and S de not e variables or non-terminals.
2) the lower case letters a, b, c, d, e, digits and bold face strings are terminals.
3) the capital letters X, Y, and Z de not e symbols that may bee i the r termin also r non-terminals.
4) the lower case letters u, v, w, x, y, and z denote strings are terminals.
5) the lower case Greek letters α, β, and γ denote strings having variables and terminals.
SRI MANAKULA VINAYAGAR ENGINEERINGCOLLEGE

8. Give the applications of context free grammar?


Context free languages are used in:
 Defining programming languages
 Formalizing the notation of parsing
 Translation of programming languages
 String processing applications

9. What are the uses of context free grammars?


 Construction of compliers.
 Simplified the definition of programming languages.
 Describes the arithmetic expressions with arbitrary nesting of balanced p are n the sis{(,)}.
 Describes block structure in programming languages.
 Model neuralnets.

10. What is meant by derivation? (NOV2015)


 Derivation is a process of producing any string derivable from start symbol of the grammar using
the production of the grammar.
 Beginning with the start symbol, we derive terminal strings by repeatedly replacing a variable by the
body of some production with that variable in the head. the language of the CFG is the set of
terminal strings we can so drive; it is called a context-free language.

11. Define derivation trees?


 Derivation or parse trees, is structure on the words of a language that isuse ful in applications such
as the compilation of programming languages.
 the vertices of a derivation tree are labeled with terminal or variable symbols of the grammar.

12. What is meant by parse tree?


 the parse tree is a tree that shows the essentials of a derivation. Interior nodes are labeled byvariables,
and leaves are labeled by terminals or ε.
 For each internal node, the re must be a production such that the head of the production is the label
of the node, and the labels of its children, read from right to left, from the body of that
production.

13. What is yield of a parse tree?


If we look at the leaves of any parse tree and concatenate the m from the left, we get a string, called the
yield of the tree, which is always a string that is derived from the root variable. the fact that the yield
is derived from the root will be proved shortly. of special importance are those parse tree such that:
a. the yield is a terminal string. That is, all
SRIleaves are labeled
MANAKULA either with
VINAYAGAR a terminal or with ε.
ENGINEERINGCOLLEGE
b. the root is labeled by the start symbol.

14. Give the Formal definition of 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 (start symbol).
3) If a vertex is interior and has label A, the n A must be in V.
4) If n has label A and vertices n1, n2, n3, ……nk are the sons of vertex n, in order from the left,
with labels x1, x2, ……xk respectively, the nAx1, x2, x3, ……, xk must be a production in P.
5) If vertex n has label ε, the n n is a leaf and is the only son of its father.

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?

 ASubtree(A bbba) Aba which is derived from the above derivationtree

 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.

17. What are the types of Derivation?


the two types of derivations are
i. Left mostderivation
ii. Right mostderivation

18. Define LMD with suitableexamples? (OCT-22)


If at each step in a derivation a production is applied to the leftmost variable the n the derivation is
called “Left Most Derivation (LMD)”.

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

Left Most Derivation (LMD):


E E + E
E + E * E
id + E * E
id + id * E
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

Right Most Derivation (RMD):


E E + E
E + E *E
E + E *id
E + id * id
id +id * id
the above string is matched with the given string.

20. Consider a grammar given as G = ({S, A}, {a, b}, P, S), where P consists
of SaAS|a
ASbA | 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: Aba
LMD
SaAS 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.

21. Consider the productionSaSb|ab.FindLMDandRMDfor the inputstringaaabbb.


Solution
P1 : SaSb
P2 : S ab
LMD
SaSb
aaSbb byP1
aaabbb byP2
RMD
Sasb
aasbb byP1
aaabbb byP2
the given input string is accepted.

22. Consider the grammar G = ({S, A}, {a,b},P,S), where P consists of SaAS | b , ASbA | ba. Draw
its equivalent derivation tree.

1) the vertices are number ed for reference 1, 2. . .10


2) the label of the vertices are variables |terminals
3) the label of the root vertex is S-startsymbol
4) the interior vertices are 1,3,4,5,7 which are variables

Thus the left- to -right ordering of derivationis:S aAS aSBAb abbbab


SRI MANAKULA
SRI MANAKULA VINAYAGAR
VINAYAGAR ENGINEERING COLLEGE
ENGINEERINGCOLLEGE
23. What is left sententialform?
If the string is produced or derived from the start symbol from left most derivation, the n the string is
called left sentential form.

24. What is right sententialform?


If the string is produced or derived from the start symbol from right most derivation, the n the string is
called right sentential form.

25. What isambiguousgrammar? (NOV2017)


 A contextfreegrammar Gsuchthatstringorwordhastwoparsetreesissaid to beambiguous.
 For some CFG’s, it is possible to find a terminal string with more than one parse tree, or equivalently,
more than one leftmost derivation or more than one rightmost derivation. Such a grammar is called
ambiguous grammar.

26. What is unambiguousgrammar?


For some CFG’s, it is possible to find a terminal string with at most one parse tree, or equivalently, at
most one leftmost derivation or at most one rightmost derivation. Such a grammar is called unambiguous
grammar.

27. What is meant by inherentambiguity?


A context-free language L is said to be inherent ambiguous if all its grammars are ambiguous. If even
one grammar for L is unambiguous, the n L is an unambiguous language.

28. What is meant by eliminatingambiguity?


For many useful grammars, such as those that describe the structure of programs in a typical
programming language, it is possible to find an unambiguous grammar that generates the same
language. Unfortunately, the unambiguous grammar is frequently more complex than the simplest
ambiguous grammar for the language. the re are also some context-free languages, usually quite
contrived, that are inherently ambiguous, meaning that every grammar for that language is ambiguous.

29. What are the 3 ways to Simplify Context FreeGrammar?


1) By removing the useless symbols from set of productions.
2) Elimination of empty or null production(Aε).
3) Elimination of unit productions(AB).

30. Give the productions obtained fromCFG?


Every production should be in the form of ABC,
Aa, Where A, B, C are variables and ‘a’ is a terminal.
SRI MANAKULA
SRI MANAKULA VINAYAGAR
VINAYAGAR ENGINEERING COLLEGE
ENGINEERINGCOLLEGE
31. Define uselesssymbol?
Every production of G be one of the form Aaα where α is the string of variable only. This is called
as useless symbols.

32. Define usefulsymbol?

 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.

33. What is meant by nullableproductions?


the elimination of productions of the form A ε, the n A is called nullable or ε-productions.

34. Define unitproduction?


A production of the form AB where A and B are variables is called unit productions.

35. What are the types of normal forms inCFG?


the two types of normal forms are
 Chomsky Normal Form(CNF)
 Greibach Normal Form(GNF)

36. Define Chomsky NormalForm(CNF)? (APR 2015, 2016) (NOV2016)


the context free language is in Chomsky Normal Form (CNF) is generated by a grammar in which all
productions are of the form:
 ABC(Non-TerminalNon-Terminal*Non-Terminal)
 Aa (Non-TerminalTerminal)
where
A, B, and C are variables or non-terminals and a is a terminal.

37. Define Greibach NormalForm(GNF)? (NOV2015)


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.
SRI MANAKULA
SRI MANAKULA VINAYAGAR
VINAYAGAR ENGINEERING COLLEGE
ENGINEERINGCOLLEGE

38. Consider the grammar G= ({S, C}, {a,b},P,S} where P consists of SaCa,CaCa|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.

40. Is the grammar G with following productions


ambiguous?Justify. SSS | aSb | bSa |ε

Solution:
the given grammar is SSS | aSb | bSa |
ε Let the input string is abab

LMD1:
S aSb
abSab
bεab
abab
LMD2:
SSS
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 SaSb | aAb;AbAa|ba; (APR
2015)Solution:
the given grammar is SaSb | aAb; AbAa | ba;
(i) SaSb
| aaAbb
| aababb
(ii) SaAb
| abAab
|abbabb
the CFL for the given grammar L(G)={aababb, abbabb, ababb, aaababbb…}

42. What issententialform? (APR2015)


String that are derived or produced from the start symbol is called sentential form.

Let G={V,T,P,S}beaCFG the nastring of terminalsand variables αis called asententialformif (ie)

α in T*suchthat is asententialform.

43. Give an example for an ambiguousgrammar?


For some CFG’s, it is possible to find a terminal string with more than one parse tree, or equivalently,
more than one leftmost derivation or more than one rightmost derivation. Such a grammar is called
ambiguous grammar.
Let the string w = aaba , parse tree for the string shown below.

For the same string two different parse trees can be generated. Hence the given grammar is ambiguous.

44. What is Push downAu to mata(PDA)? (APR, NOV2016) (Arp-22)


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 pushdown au to ma to n is essentially a finite au to ma to n with the control
of both an input tape and a stack, or “first in last out” list.
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

45. Write the ma the matical technique is used inPDA?


A push down au to mata M is defined by (Q, Σ, Γ, δ, q0, z0, F) where,
1. Q is the finite set of states;
2. Σ is the alphabet called inputalphabet;
3. Γ is the alphabet calledstackalphabet;
4. q0 in Q is the stack state/initialstate;
5. z0 in Γ is the particular stack symbol calledstartsymbol;
6. F⊆Q is the set of finalstates;
7. δ is a mapping from Q x (Σ U{ ε }x Γ to finite subsets of Q xΓ*.

46. Explain the transition mapping of PDA?


the transition mapping for PDA can be given
as Q * (Σ U ε) * Γ Q* Γ*
where Γ is a finite set of stack symbols and Γ* consist of stack symbols with z0.

47. Why the re is a need for stack inPDA?


In PDA stack acts as Cash memory and Auxiliary memory. Cash memory is used to s to re the frequently
accessing data and the data can be retrieved as fast as possible. If the re is no enough space in input tape
the n the data can be s to red in external memory.

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.

49. How stack is being implemented inPDA?


the symbol can be entered or removed only on the to p of s to rage. When a symbol is added on
to p, the symbol previously on the to p becomes the second and so on. Similarly when a symbol is
removed from to p of stack the symbol previously second from to p becomes to p symbol and so on.
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE
Input tape
A b b B a b a
Reading head
b
Finite cell a
b
a
a

50. Difference between CFL and PushdownAu to mata?

Context Free language Push Down Au to mata


A language can not be described by Push down au to mata can accept CFL. It is essentially an
DFA/NFA NFA with a stack
the re is no memory It has a memory, which can be used to count the number

51. Difference between context free language and regular language?

Context Free language Regular language


A language can not be described by DFA/NFA Can be described by DFA/NFA
the re is no memory the re is memory
This may be ambiguous or unambiguous This is always unambiguous
With the help of context free grammar we can With the help of regular grammar we can
generate
generate this language
Regular language

52. Comparison of NFA andPDA?

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

53. Specify the two types of moves inPDA?


 the moves dependent on the input symbol(a) scanned
is: δ (q, a, Z) = {(p1, γ1),(p2,γ2),…........ (pm,γm)}
where q and pi, 1≤ i ≤ m, are states, a is in Σ, Z is a stack symbol and γi is in Γ*. PDA is in state q,
with input symbol a and Z the to p symbol on state enter state pi replace symbol Z by string γi.
 the movesdependen to n the inputsymbolis(ε-move)δ(q,ε,Z)={(p1,γ1),(p2,γ2),… ............................ (pm, γm)}is
that PDA is in state q, independent of input symbol being scanned and with Z the to p symbol
on the stack enter a state pi replace symbol Z by string γi.

54. What are the different types of language acceptances by aPDA?


the re are 4 types of language acceptances of a PDA. the y are
8. Acceptance by finalstate
9. Acceptance by emptystate
10. From empty to finalstate
11. From final state to emptystate

55. Define PDA accepted by final stateand emptystack. (NOV2016)


12. Acceptance by finalstate
Let M=(Q,Σ,Ѓ,δ,q0,z0.F) be a PDA. the n, the language accepted by M is set of strings
that, L(M)=(w/(q0,w, z0)├* (p, ε, γ) for some P ε F, γ ε Ѓ*).
13. Acceptance by emptystack:
Let N(M) be the language accepted by M is given by,
N(M)= (w/(q0,w, z0)├* (p, ε, ε) for some P ε Q.

56. What is the equivalence of PDA andCFG?


A language is generated by a CFG.
a) If and only if it is accepted by a PDA byemptystack.
b) If and only if it is accepted by a PDA by finalstate.

GRAMMAR PDA BYEMPTYSTACK PDA BY FINALSTATE


SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE
57. Define Instantaneous description inPDA?
Instantaneous description (ID) describe the configuration of a PDA at a given instant. ID is a triple such
as (q, w, γ),
where
 q is the state,
 w is the remaininginput
 γ is the stackcontents
If M= {Q, Σ, δ, Γ, q0, z0, F} is a PDA we say that (q, aw, Zα) ├ (p, w, αβ) if δ(q, a, Z) contains (p, α)
Example: (q1, BG) is in δ (q, 0, G) tells that (q1, 011, GGR) ├ (q1, 11, BGGR).

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}

δis R1 : δ(S, 𝜀, Z0) = { (f,SZ0)}


R2 : δ(f, 𝜀, S) = { (f, AB)} for prod1
R3 : δ(f, ϵ, A) = { (f, aA)} for prod2
R4 : δ(f, ϵ, A) = { (f, ϵ)} for prod 2
R5 : δ(f, ϵ, B) = { (f, aBb), (f, ϵ)} for prod 2
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE
R6 : δ(f, a, a) = { (f,ϵ)}
R7 : δ(f, b, b) = { (f,ϵ)}
R8 : δ(f, ϵ, Z0) = { (f, ϵ)}

Processing of string aaaabb:


(S, aaaabb, Z0) ⊢ (f, aaaabb, SZ0) – R1
⊢ (f, aaaabb, ABZ0) – R2
⊢ (f, aaaabb, aABZ0) – R3
⊢ (f, aaabb, ABZ0) – R6
⊢ (f, aaabb, aABZ0) – R3
⊢ (f, aabb, ABZ0) – R6
⊢ (f, aabb, BZ0) – R4
⊢ (f, aabb, aBbZ0) – R5
⊢ (f, abb, BbZ0) – R6
⊢ (f, abb, aBbbZ0) – R5
⊢ (f, bb, BbbZ0) – R6
⊢ (f, bb, bbZ0) – R5
⊢ (f, b, bZ0) – R7
⊢ (f, ϵ, Z0) – R7
⊢ (f, ϵ) – R8
Hence the string is
accepted.

59. ConstructaPDAusingthisinputabcba.GivenG(V,T,P,S) whereV={S,a,b,c}T={a,b,c} P= {S →


aSa, S→ bSb, S →c}.
Solution:
M = (Q, Σ, Γ, δ, q0, Z0, F) be aPDA.
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)} prod1
R3 : δ(q, ϵ, S) = {(q, bSb)} prod2
R4 : δ(q, ϵ, S) = {(q, c)} prod 3
R5 : δ(q, a, a) = {(q,ϵ)}
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE
R6 : δ(q, b, b) = {(q,ϵ)}
R7 : δ(q, c, c) = {(q, ϵ)} SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

R8 : δ(q, ϵ, Z0) = {(q, ϵ)}

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

60. How to eliminate the left recursivepatterns?


When the given production is of the format A→Aα|β the n left recursive occurs. to eliminate
the left recursive we must introduce two new production which is of the form A→βA’ and
A’→αA’|ε.
Example:
Given production are E→E+T|T
This production is of the form A→Aα|β
Where, A= E, α= +T, β=T
Now the production will change
to E→TE’
E’→+TE’|ε

61. Write down the application of pumping lemma forCFL?


the applications of pumping lemma for CFL are to check whe the r the given language is CFL or not
. to check whe the r the given string is finite or infinite

62. What is pumping lemma for regularsets?


the pumping lemma for regular sets states that every sufficiently long string in a regular set contains a
short substring that can be pumped. That is, inserting as many copies of the substring always yields a
string in the regular sets.

63. What is pumping lemma forCFL?


the pumping lemma for CFL states that the re are always two short substrings close to ge the r that can
be repeated, both the same number of times.

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

1. Explain the pumping lemma forRegularSets? (5MARKS)


 Abasicresult called the pumping lemma ,whichisapowerful to olforprovingcertain languages,is
regular or not .
 Itis also usefulin the development of algorithms to answercertainquestions concern
ingfinite au to mata,suchas whe the r the language acceptedbyagiven FA is finiteor infinite.

the pumping lemma for Regular Sets

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.

Path in transition diagram of DFA M.


If qm is in F, that is a1a2……..am is in L(M), the n a1a2….....ajak+1ak+2……am is also in L(M), since the re isa
path from q0 to qm that goes through qj but not around the loop labeled
aj+1… ak. δ(q0,a1……..ajak+1……am) = δ(δ(q0,a1……..aj) ak+1……am)
=δ(qj,ak+1……am) (sinceδ(q0,a1 .......... aj)=qj)
= δ(qk, ak+1……am) (since qj= qk)
=qm
which is the final state of the au to mata to be accepted. Hence proved.
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE

Application of the pumping lemma


the pumping lemma is useful in proving that certain sets are not regular. the application is an
“adversary arguments” of the following form.
1. Select the language L to provenon-regular.
2. the “adversary” picks n, the constants mentioned in the pumping lemma .
3. Select a string z inL.
4. the adversarybreaks zin to u,vandw,subject to the constraintsthat|uv|≤n and|v|≥1.
5. A contradiction to the pumping lemma by showing, for any u, v, and w, determined by the
adversary, that the re exists an i for which uviw is not in L. It may the n be concluded that L is not
regular.

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 L1L2, so L1L2isregular.
 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

3. Explain Context FreeGrammar(CFG)? (11MARKS)


 A context free grammar (CFG) is a finite set of variables ( also called non-terminals or syntactic
categories) each of which represents a language.
 CFG is a way of describing languages by recursive rules or substitution rulescalledproductions.
 A CFG consists of sets of variables, a set of terminal symbols and a start variable, and productions.
Each production consists of a starting variable and a body consists of a string of zero or more
variables or terminals.
A Context Free Grammar (CFG) is de not ed G= (V, T, P,
S), Where
1. Vis a finite set of variables ornon-terminals.
2. Tis a finite set of terminalsymbols.
3. Pis a finite set of productions; each production is of the form Aα, where A is a
non- terminal or variable and α is a string of symbols from (V UT)*.
4. Sis a special variable called the startsymbol.

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
EE+E
EE*E
E(E)
Eid

Applications of context free grammar are


 Defining programming languages
 Formalizing the not ation of parsing
 Translation of programming languages
 String processingapplications
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE

4. What are derivation trees withanexample? (11MARKS)


 Derivationorparsetrees,isstructureon the words of a languagethat isusefulinapplicationssuchas the
compilation of programming languages.
 the vertices of aderivationtree are labeledwithterminalorvariablesymbols of the grammaror
possibly withε.
 If an interior vertex n is labeled A, and the sons of n are labeled X1, X2, …, Xk from the left,
the n AX1 X2… Xk must be aproduction.

Example of derivation tree:


Consider this grammar as ({E}, {+,*, (,), id}, P, E), where P consists of
EE+E
EE*E
E(E)
Eid
the input string is (id+id) *id

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 Ax1,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.

Consider the grammar G = ({S, A}, {a,b},P,S), where P consists


of SaAS | a
ASbA | SS|ba
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE

Draw its equivalent derivation tree.

1) the interior vertices are number ed for reference 1,3,4,5, and7.


2) the label of the vertices are variables |terminals
3) the label of the root vertex is S-startsymbol
4) the interior vertices are 1,3,4,5,7 which are variables
Thus the left- to -right ordering of derivation is: S aAS aSbAS aabAS aabbaS abbbab

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.

Example for a Subtree:


 A Subtree (A Sba abA abba) ASba which is derived from the abovederivationtree

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 SaAS |a
ASbA | 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

Left Most Derivation (LMD):


SaAS byP1
aSbAS byP3
aabAS byP2
aabbaS byP5
aabbaa byP2
the above string is matched with the given string.

Right Most Derivation (RMD):


S aAS byP1
aAa byP2
aSbAa byP3
aabAa byP2
aabbaa byP5
the above string is matched with the given string.
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

5. Give a grammar to generate the CFL L= {a n bn:n<=m+3}. (5MARKS)


1. S aABb
A aa
Bbb
S aABb
SaaaBb
Saaabbb
L= {a n bn: n <= m+3}
2. SaaSbb
Sab
(i) SaaSbb
Saaabbb
(ii) SaaSbb
SaaaaSbbbb
Saaaaabbbbb
L= {a n bn: n <= m+3}
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE

10. State and prove Chomsky NormalForm(CNF)? (11MARKS) (NOV2015)


the each state that all context free grammars are equivalent to grammar with restrictions in the form of
production.
the context free language is in Chomsky Normal Form (CNF) is generated by a grammar in which all
productions are of the form
 ABCor
 Aa
where
A, B, and C are variables or non-terminals and a is a terminal

For a given grammar, we have to eliminate


1. uselesssymbol
2. productions
3. unitproductions
 the n check whe the r the production contains ABC and Aa more than 2 non-terminals are present,
so it is not a CNF and combination of terminals and non-terminals are present it is also not CNF.
 A context free grammar G is in CNF if every production is of the form Aa or ABC and Sλ is in
G if λ ϵ L (G). When λ is in L (G) we assume that S does not appear on the R.H.S of anyproduction.

Example:
Consider the grammar for ({S, A, B}, {a, b}, P, S) that has the
productions: SbA | aB
AbAA | aS | a
BaBB | bS | b

Solution:
the given productions
are SbA
SaB
AbAA
AaS
Aa
BaBB
BbS
Bb
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE

Step 1: Simplification of a Grammar

In the given grammar the re are no useless symbols, no unit productions and no ε-productions.

Step 2: Converting CFG in to CNF


P1: SbA
P2: SaB
P3: AbAA
P4: AaS
P5: Aa
P6: BaBB
P7: BbS
P8: Bb

Step 3:

the only productions P5, P8 are Aa and Bb 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
SbA SaB
AbAA AaS
BaBB BbS

Consider P1:
SbA
Introduce a new temp variable (Cbb)
∴ SCbA

Consider P2:
SaB
Introduce a new temp variable (Caa)
∴ S CaB
ConsiderP3:
AbAA
ACbAA (Cbb)
ConsiderP4:
AaS
∴ACaS (Caa)
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE

Consider P6:
BaBB
BCaBB (Caa)

Consider P7:
BbS
∴BCbS (Cbb)

Step 4:
the remaining productions P3 and P6 are ACbAA , BCaBB which are not in CNF form. Because
it contains more than two non-terminals.
We have to replace the m.

Again Consider P3:


ACbAA
Introduce a new temp variable (D1AA)
∴ A CbD1

Again Consider P6:


BCaBB
Introduce a new temp variable (D2BB)
∴ BCaD2

the Productions for the grammar in CNF form


SCbA|CaB D1AA
ACaS |CbD1|a D2BB
BCaD2|CbS Caa
Cbb
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE

11. Explain Greibach Normal Form (GNF)withanexample? (11 MARKS) (APR2016)(OCT-22)


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-terminal symbols
 α and a is a terminal symbol
 X is a sequence of non-terminal symbols.

Every CFL without ε can be generated by a grammar for which every productions of the form Aaα.
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.

Greibach Normal Form (GNF) algorithm:


begin
1) for k=1 to ndo
begin
2) for j=1 to k 1do
3) for each production of the
formAkAjdobegin
for all production A j do

add production A k ;


remove production Ak A j 
end;
4) for each productionAkAkdo
begin
add productions B k and B k B k ; remove
productions A k A k 
end;
5) for each production Ak , where does not
begin with Ak do
add production Ak Bk

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.

Steps to solve GNF problem:


1) Simplification of CFGgrammar
2) Converting CFG grammar in to CNFform
3) Converting CNF form in to GNFform
i. Change the variables to include suffix number s
ii. Preliminary verificationstep
iii. Substitute a correct production in to incorrectproduction

Example:
Convert the following grammar to
GNFform SAA|a
ASS |b
Solution:

Step1: Simplification of a Grammar


In the given grammar the re are no useless symbols, no unit productions and no ε-productions.

Step2: Convert CFG in to CNF form


Here all the productions are already in the CNF form.

Step3: Convert CNF form in to GNF form


i. Change the variables to include suffix number s
the given productions are in the form of CNF. So we have to convert this in to GNF
form, Let rename above production by S=A1 and A=A2
P1: A1 A2A2

P2: A1 a
P3: A2A1A2
P3: A2b
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: A1a // 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: A2A1A2
2<1 // temporary correctproduction
the fourthproduction
P4: A2b // already in GNFform

iii. Substitute a correct production in to incorrectproduction


Substitute all A1 Production value P3 production,
A1 A2A2 and A1 a
P3: A2 A1A1
A2 A2A2A1 and A2 aA1

Consider the production A2 A2A2A1


This productions of the form of Ak Ak α
means add Bk α and Bk αBk and remove Ak
Ak α
Ak=A2, k=2 and α= A2A1

i) Add Bk α
Bk=B2
B2 A2A1

ii) add Bk αBk


B2 A2A1B2
iii) remove Ak Akα
Now A2 A2A2A1 is removed

B2 A2A1 B2

A2A1B2

Consider the production A2aA1


This production is of the form Ak β means add Ak β
Bk Now Ak=A2, β =aA1, Bk=B2 and k=2, the n
Add Ak β Bk

A2 aA1B2

A2aA1
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE

Consider the production P4: A2 b


This production of the form Ak β
Now Ak=A2, β =b, Bk=B2 and k=2, the
n Add Ak β Bk

A2 bB2

A2 b

Substitute all A2 production value in B2 productions, we get


B2 A2A1

B2 bB2A1 | bA1 | aA1A1 | aA1B2A1

B2 A2A1B2
B2 bB2A1B2 | bA1B2 | aA1A1B2 | aA1B2A1B2

P1: A1 A2A2
A1aA1B2A2 | aA1A2 | bB2A2 |bA2

Consider the production P2: A1a


This production of the form Ak β
Now Ak=A1, β =a, Bk=B1 and k=1, the
n Add Ak β Bk

A1aB1

A1a

Substitute all A1 production value in above P3 productions, we get


P3: A2 A1A1

A2 aB1A1 | aA1

the resulting GNF grammar productions are

A1aA1B2A2 | aA1A2 | bB2A2 |bA2 | aB1 | a


A2 aB1A1 | aA1|aA1B2 | bB2| b
B2 bB2A1 | bA1 | aA1A1 | aA1B2A1 | bB2A1B2 | bA1B2 | aA1A1B2 | aA1B2A1B2
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE

12. Explain Ambiguous Grammarwi the xample? (11MARKS)


 A contextfreegrammar Gsuchthatstringorwordhastwoparsetreesissaid to beambiguous.
 For some CFG’s, it is possible to find a terminal string with more than one parse tree, or equivalently,
more than one leftmost derivation or more than one rightmost derivation. Such a grammar is called
ambiguous grammar.
 the grammar G is said to be ambiguous when the same start symbol produces same language with
‘n’ number of left most and right mostderivation.

Example 1:
Consider the Grammar G is the
grammar SS 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:
SS 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:
SbSS
bSa
bSbSa
bSbaa
babaa
Both LMD and RMD are same. Hence the given grammar is ambiguous.

Example 3:
Show that the grammar SaB|ab
AaAB|a
BABb|b is ambiguous.
Solution:
Consider the input string or word ω =ab
LMD
the LMD of G are
SaB
a b
Sab
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

13. Provethat the language,L={an|n≥1}isunambiguous.EventhoughL the reexistaambiguous


grammargeneratingit. (5MARKS)

Let G= ({S},a,p,S}
Where p1={SSS, S1}
We observe that
L(G)={ an/n≥1}
Consider the word a3 ε L
(G) Now,
LMD1:
SSS
SSS
aSS
aaS
aaa
LMD2:
SSS
aS
aSS
aaS
aaa
Thus a3 has different LMD Grammar is ambiguous. Thus the re exist an unambiguous grammar generating
{ an/n≥1}.

14. Consider the grammar


EE+E
EE*E
E(E)
Eid
the input string is id + id * id. Show that it is ambiguousgrammaror not ? (5MARKS)

Solution:
the given grammar
EE+E
EE*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

15. Explain unambiguous grammarwithanexample? (6MARKS)


For some CFG’s, it is possible to find a terminal string with at most one parse tree, or equivalently, at
most one leftmost derivation or at most one rightmost derivation. Such a grammar is called unambiguous
grammar.

Example:
Consider the grammar EE+T |T
TT*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
EE+T |T
TT*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
SAB | CA, BBC | AB , Aa , CaB| b (5 MARKS) (APR2015)
Solution:
CNF Format:
NT NTNT
NT T
Where , NT – Non terminal
T – Terminal
Given
P : SAB | CA
BBC | AB
Aa
CaB | 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 : SAB | CA
BBC | AB
Aa
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’’: SAB | CA P’’’: SCA
BBC |AB Aa
Aa Cb
CaB | 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’’’: SCA
Aa
Cb

17. Find the GNF equivalent of the grammar SAA | 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 Aaα.
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
SAA | 0
A SS | 1
Convert the following grammar to GNF form
SAA
S0 A
SS
A1
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.

Step2: Convert CFG in to CNF form


Here all the productions are already in the CNF form.

Step3: Convert CNF form in to GNF form


iv. Change the variables to include suffix number s
the given productions are in the form of CNF. So we have to convert this in to GNF
form, Let rename above production by S=A1 and A=A2
P1: A1 A2A2

P2: A1 0
P3: A2A1A1
P3: A21
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: A10 // already in GNFform
the first productions i value=2 and j value=1. So i<j. No need to disturb now.
P3: A2A1A1
2<1 // temporary correctproduction
the fourthproduction
P4: A21 // already in GNFform

vi. Substitute a correct production in to incorrectproduction


Substitute all A1 Production value P3 production,
A1 A2A2 and A1 0
P3: A2 A1A1
A2 A2A2A1 and A2 0A1
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE

Consider the production A2 A2A2A1


This productions of the form of Ak Ak α
means add Bk α and Bk αBk and remove Ak
Ak α
Ak=A2, k=2 and α= A2A1

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

Consider the production A20A1


This production is of the form Ak β means add Ak β
Bk Now Ak=A2, β =0A1, Bk=B2 and k=2, the n
Add Ak β Bk

A2 0A1B2
A20A1

Consider the production P4: A2 1


This production of the form Ak β
Now Ak=A2, β =1, Bk=B2 and k=2, the
n Add Ak β Bk

A2 1B2
A2 1

Substitute all A2 production value in B2 productions, we get


B2 A2A1
B2 1B2A1 | 1A1 | 0A1A1 | 0A1B2A1

B2 A2A1B2
B2 1B2A1B2 | 1A1B2 | 0A1A1B2 | 0A1B2A1B2
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE

P1: A1 A2A2
A10A1B2A2 | 0A1A2 | 1B2A2 |1A2

Consider the production P2: A10


This production of the form Ak β
Now Ak=A1, β =0, Bk=B1 and k=1, the
n Add Ak β Bk

A10B1
A10
Substitute all A1 production value in above P 3 productions, we get
P3: A2 A1A1
A2 0B1A1 | 0A1

the resulting GNF grammar productions are


A10A1B2A2 | 0A1A2 | 1B2A2 |1A2 | 0B1 | 0
A2 0B1A1 | 0A1|0A1B2 | 1B2| 1
B2 1B2A1 | 1A1 | 0A1A1 | 0A1B2A1 | 1B2A1B2 | 1A1B2 | 0A1A1B2 | 0A1B2A1B2

18. Show that the grammarSa|Sa|bSS|SSb| SbSisambiguous.(5MARKS)(APR2015)


A grammar G is ambiguous if and only if the re exist at least one string w εT* .For which two or more
different parse tree exist by applying ei the r by LMD or RMD
Let the string w = aaba , parse tree for the string shown below.

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

19. Consider the


productions SaB
|bA
AaS | bAA |a
B bS |aBB |b
For the string aaabbabbba, find a leftmostderivation. (6 MARKS) (APR2015)
S aB
aaBB [B aBB]
aaaBBB [B aBB]
aaabBB [B b]
aaabbB [B b]
aaabbaBB [B aBB]
aaabbabB [B b]
aaabbabbS [B bS]
aaabbabbbA [SbA]
aaabbabbba [Aa]
Hence the pro of .

20. Convert the grammar G with productions to Chomsky normalform


SaSA | aAA | b , A bBBB , Bb|ε (10 MARKS) (APR2015) (APR-22)
Solution:
CNF Format:
NT NTNT
NT T

the given grammar is


P: SaSA | aAA |b
A bBBB
Bb | ε

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

Before elimination After elimination


P: SaSA | aAA | b P’: SaSA | aAA | b
A bBBB A bBBB
Bb | ε Bb

(vi) 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’
(vii) Elimination of Useless symbolproduction

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’’’: SaSA | aAA | b
A bBBB
Bb

Step 2:
Convert to CNF:
(i) Name the productions
P1: SaSA
P2: SaAA
P3: Sb
P4: A bBBB
P5: Bb
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE
(ii) Consider the production
P1: SaSA
Substitute Da a
SDaSA
Substitute D1 SA
SDaD1
Now the production P1 in the form of CNF
P1 Productions are
S DaD1
D1 SA
Da a

(iii) Consider the production


P2: SaAA
Substitute Da a
SDaAA
Substitute D2 AA
SDaD2
Now the production P2 in the form of CNF
P2 Productions are
SDaD2
D2 AA
Da a

(iv) Consider the production


P3: Sb
It is already in CNF form.

(v) Consider the production


P4: A bBBB
Substitute Db b
ADbBBB
Substitute D3 DbB and D4 BB
AD3D4
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE
P4 Productions are
AD3D4
D3 DbB
D4 BB
Db b

(vi) Consider the production


P5: Bb
It is already in CNF form.

(vii) Now all productions are converted in to CNFform


P1 P2 P3 P4 P5
Productions Productions Productions Productions Productions
S DaD1 SDaD2 Sb AD3D4 Bb
D1 SA D2 AA D3 DbB
Da a Da a D4 BB
Db b

the refore the CNF productions are : (eliminate Duplicate productions)


CNF Productions are
S DaD1
D1 SA
Da a
SDaD2
D2 AA
Sb
AD3D4
D3 DbB
D4 BB
Db b
Bb
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE

21. If G is the grammar SSbS | a. Show thatGisambiguous. (6 MARKS) (NOV2015)

Let the string w = ababa, below is the two different derivation tree for the same string.

Hence the grammar is ambiguous.

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.

23. 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 (3 MARKS) (NOV2015)

24. Find the rightmostderivationandderivationtreefor the string“abbaaba”using the following


grammar: (5MARKS) (NOV2016)
S XaaX
X aX | bX | ε

Right Most Derivation:


the given input string is “abbaaba”
S XaaX
XaabX [ X bX ]
XaabaX [ X aX ]
Xaaba ε [ X ε ]
Xaaba
aXaaba [ X aX ]
abXaaba [ X bX ]
abbXaaba [ X bX ]
abbεaaba [ X ε ]
abbaaba
the given input string “abbaaba” is accepted by the grammar.
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE

Derivation Tree:

25. Eliminateε-productionsandunitproductionsfrom the followinggrammar:(6MARKS)(NOV2016) E

TA

A +TA | ε

T MB

B *MB| ε

M id

1. Eliminating ε-productions from the


followinggrammar: (i).Identify the
NullableProduction ={A ,B}
(ii). Removing ε-Production
A’s Production:
ETA A+TA
Aε Aε
ETε A+Tε
ET A+T

B’s Production:
TMB B *MB
Bε Bε
T Mε B *Mε
TM B *M
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE

P becomes P’: E TA| T


A +TA | +T
T MB | M B
*MB| *M M
id

2. Eliminating unit productions from the


followinggrammar: (i). Identify the unitproductions
ET T M
(ii). Place no-unit production in P’:
P’: E TA
A +TA | +T
T MB
B *MB| *M
M id
(iii). After eliminating unit production we get P’becomes:
P’: E TA |MB
A +TA | +T
T MB | id B
*MB| *M M
id
SRI MANAKULA
SRI VINAYAGAR
MANAKULA ENGINEERING
VINAYAGAR COLLEGE
ENGINEERING COLLEGE
26.Consider the CFGG =({E, T, F},(+,*,(, ),a},P,E) (11MARKS) (NOV 2017)
Where P: E E + T | T
T T * F | F
F (E) | a
Convert this Grammar to Chomsky Normal Form.

the given Grammar G = ( {E, T, F}, (+, *, (, ), a}, P, E )


P: E E + T | T
T T * F | F
F (E) | a
Step 1: Here the re is no ε-Production.
Step 2: Eliminating unit production from the above grammar
(i). Identify the unit productions
ET T F
(ii). Place no-unit production in P’:
P: E E +T
T T * F
F (E) | a
(iii). After eliminating unit production we get P’ becomes:
P’ : E E + T | T * F
T T * F | (E) | a
F (E) | a
Step 3: Here the re is no useless symbols.
Step4: Convert CFG in to CNF form
NT NT NT
NT T
P1: E E + T
P2: E T * F
P3: T T * F
P4: T(E)
P5: Ta
P6: F (E)
P7: Fa
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 P5 Production:


∴Ta // Already in CNFform
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE

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

Consider the P7 Production:


∴ Fa // Already in CNFform

27.Explain the Deterministic Push Down Au to mata? (11 MARKS) (APR2017)


 the pushdown au to ma to n is essentially a finite au to ma to n with the control of both an input
tape and a stack, or “first in last out” list. That is, symbols may be entered or removed only at the to p of
the list. When a symbol is entered at that to p, the symbol previously at the to p becomes second
from the to p, the symbol previously second from the to p becomes third and soon.
 Similarly, when a symbol is removed from the to p of the list, the symbol previously second from the
to p becomes the to p symbol, the symbol previously third from the to p becomes second, andsoon.

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.

Finally, the language accepted by a pushdown au to ma to n.


1. the language accepted to be the set of all inputs for which some sequences of moves causes the
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE
pushdown au to ma to n to empty its stack. This language is referred to as the language
accepted by empty stack.
2. the language accepted is similar to the way a finite au to ma to n accepts inputs. That is, some
states as final states and defines the accepted language as the set of all inputs for which some
choice of moves causes the pushdown au to ma to n to enter a final state.

A push down au to mata M is defined by (Q, Σ, Γ, δ, q0, z0, F) where,


1. Q is the finite set of states;
2. Σ is the alphabet called input alphabet;
3. Γ is the alphabet called stack alphabet;
4. q0 in Q is the stack state/initial state;
5. z0 in Γ is the particular stack symbol called start symbol;
6. F⊆Q is the set of final states;
7. δ is a mapping from Q x (Σ U{ ε }x Γ to finite subsets of Q xΓ*.
Moves
 the moves dependent on the input symbol(a) scanned
is: δ (q, a, Z) = {(p1, γ1),(p2,γ2),… ........ (pm, γm)}
where q and pi, 1≤ i ≤ m, are states, a is in Σ, Z is a stack symbol and γi is in Γ*. PDA is in state q,
with input symbol a and Z the to p symbol on state enter state pi replace symbol Z by string γi.
 the moves dependent on the input symbol is (ε-
move) δ (q, ε, Z) = {(p1, γ1),(p2,γ2),…
................................................................................(pm,γm)}
is that PDA is in state q, independent of input symbol being scanned and with Z the to p symbol
on the stack enter a state pi replace symbol Z by string γi.

Instantaneous description (ID)


Instantaneous description (ID) describe the configuration of a PDA at a given instant. ID is a triple such
as (q, w, γ),
where
 q is the state,
 w is the remaining input
 γ is the stack contents
If M= {Q, Σ, δ, Γ, q0, z0, F} is a PDA we say that (q, aw, Zα) ├ (p, w, αβ) if δ(q, a, Z) contains (p, α)
Example: (q1, BG) is in δ (q, 0, G) tells that (q1, 011, GGR) ├ (q1, 11, BGGR).

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

Example 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

28.Comp are the NFA and PDA in detail? (5


MARKS)NFA
 (P,a,q)ε Δ means if machine Mis in stateP, the non reading“a” from input tape go to stack q.
 (P, ε,q) ε Δ means if machine M is in state P, goes to state q, withoutconsuminginput.
PDA
1. ((P,a,β),(q,y)) if machine is in state P, the symbol read from input state is ‘a’, and β is on to p of
stack, goes to state q, and replace β by y on to p of stack.
2. ((s,a,e),(s,a)) if machine M is in state s reads ‘a’, remains in state s and push aon to stack.
3. ((s,c,e),(f,e))if read‘c’in state sand stack is empty, goes t of inal state f and not hing to pushon to
the stack.
4. ((s,e,e),(f,e)) if in state s, goes to state f.
5. ((f,a,q),(f,φ)) if read a in stack f, remain in state f and pop afrom stack.
6. PDA’s are non-deterministic.

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.

29.Explain Push Down Au to mata and (5 MARKS)


CFL? Equivalence of PDA andCFG
A language is generated by a CFG.
a) If and only if it is accepted by a PDA by emptystack.
b) IfandonlyifitisacceptedbyaPDAbyfinalstate.

GRAMMAR PDA BY EMPTY STACK PDA BY FINAL STATE

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

R3 : δ(q, ϵ, S) = {(q, bSb)} prod 2


R4 : δ(q, ϵ, S) = {(q, c)} prod 3
R5 : δ(q, a, a) = {(q,ϵ)}
R6 : δ(q, b, b) = {(q,ϵ)}
R7 : δ(q, c, c) = {(q,ϵ)}
R8 : δ(q, ϵ, Z0) = {(q, ϵ)}

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)}

the PDA can also be described by the adjacent transition diagram.

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)

q4 is final state. Hence, accept. So the string aabb is accepted by M.


(ii) Let the input beaabab.
δ(q1, aabab, z) |-- (q2 , abab,az)
|-- (q2, bab ,aaz)
|-- (q3, b , az)

No fur the r move is defined at this point. Hence the PDA gets stuck and the string aabab is not accepted.

32.Design a PDA to accept palindromes over{a,b}. (APR


2015)Solution:
Construct the PDA:
M: Q= {q0,q1}
Σ = {a, b}
Γ = {a,}
F= {q1}

δ consists of the following


transitions δ (q0, a , λ) = {[q0
,A], [q1,λ]}
δ (q0, b , λ) = {[q0 ,B], [q1,λ]}
δ (q0, λ , λ) = {[q1,λ]}
δ (q1, a , A) = {[q1,λ]}
δ (q1, b , B) = {[q1,λ]}
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE
33.Show that CFL are closed under union operation but not under intersection. (5 MARKS)(APR2015)

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 SS2.
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.

34.Construct a PDA for the languages L = { am bn cm | n ≥ 1, m ≥1}. (5 MARKS) (NOV 2016)

DPDA for an bm cn n≥,m≥1


 Approach we just need to look forbm.
 First we have to count number of a's and that number should be equal to number of c's.
 That we will achieve by pushing a's in STACK and the n we will pop a's when ever"c"comes.
 But we have to take c are b's coming between 'a' and'c'.
 For occurrence of b's we will do not hing, means we will not make in changes in STACKforb's.
 Soin the end of the strings if not hing is left in the STACK the new can say that language is accepted in
the PDA.
We have designed the PDA for the problem:
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE
Stack Transition Function
δ(q0, a, Z) = (q0,aZ)
δ(q0, a, a) = (q0,aa)
δ(q0, b, a) = (q1,a)
δ(q1, b, a) = (q1,a)
δ(q1, c, a) = (q2,ε)
δ(q2, c, a) = (q2,ε)
δ(q2, ε, Z) = (qf,Z)

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)

Deterministic Push down Au to mata for anbn


 First we have to count number of a's and that number should be equal to number of b's.
 That we will achieve by pushing a's in STACK and the n we will pop a's whenever"b"comes.
 Soin the end of the strings if not hingis left in the STACK the new can say that languageis accepted in the
PDA.

We have designed the PDA for the problem:

Stack Transition Function


δ(q0, a, Z) = (q0,aZ)
δ(q0, a, a) = (q0,aa)
δ(q0, b, a) = (q1,ε)
δ(q1, b, a) = (q1,ε)
δ(q1, ε, Z) = (qf, Z)

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

PONDICHERRY UNIVERSITY QUESTIONS

2 MARKS
1. Find out the context free languages SaSb | aAb; AbAa | 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;
SAB | CA, BBC | AB, Aa , CaB|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)
SAA | 0 , A SS | 1
(OR)
2. (a).Show that the grammarSa|Sa|bSS|SSb|SbSis ambiguous.(5MARKS)(Ref.Qn.No.18,Pg.no.41)
(b). Consider the productions
SaB|bA AaS | 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 SSbS | 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)

APRIL 2016 (REGULAR)


1. Explain Greibach Normal Form. (Ref.Qn.No.11,Pg.no.27)

NOVEMBER 2016 (ARREAR)


1. a) Find the right most derivation and derivation tree for the string “abbaaba” using the following grammar:
(5 MARKS) (Ref.Qn.No.24, Pg.no.47)
S XaaX
X aX | bX | ε
b) Eliminate ε-productions and unit productions from the following grammar:
(5MARKS) (Ref.Qn.No.25, Pg.no.48)
E TA
A +TA |
ε T MB
B *MB|
ε M id
(OR)
2. Put the grammar S SS | 0S1 | 01 in to aGNF.

APRIL 2017 (REGULAR)


1. Consider the CFGG=({E,T,F},(+,*,(,),a},P,E) (Ref.Qn.No.26,Pg.no.50)
Where P: E E + T | T
T T * F |
F F (E) |
a
Convert this Grammar to Chomsky Normal Form.
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE
MAY 2018

1. State and prove the pumping lemma of regular languages.(10marks)

(OR)

2. Put the following grammar in CNF(10marks)


SAACD
AaAb | c
CaC | a
DaDa | bDb|ε

MAY 2019

1. Discuss the properties of regular sets.(10marks)

(OR)

2. Explain the advantages and disadvantages of CNF and GNF(10marks)

APRIL 2015 (REGULAR)


1. Design a PDA to accept palindromes
over{a,b}.(Ref.Qn.No.11,Pg.no.36) (OR)
2. (a). State the properties of CFL. (5 MARKS) (Ref.Qn.No.7,Pg.no.27)
(b). Show that CFL are closed under union operation but not under intersection. (5
MARKS) (Ref.Qn.No.12, Pg.no.37)

NOVEMBER 2015 (ARREAR)


1. Design PDA for languageL={ a3nbn|n≥0}and simulates its action on the input string aaaaaabb.
(OR)
2. Explain in detail about pumping lemma for CFL. (Ref.Qn.No.6,Pg.no.25)

APRIL 2016 (REGULAR)


1. Explain the properties of CFL. (Ref.Qn.No.7,Pg.no.27)

NOVEMBER 2016 (ARREAR)


1. State and prove the pumping lemma of context free languages. (Ref.Qn.No.6,
Pg.no.25) (OR)
2. Construct a PDA for the languages L = { am bn cm | n ≥ 1, m ≥ 1 }. (5 MARKS) (Ref.Qn.No.13,Pg.no.37)
3. Show that { 0n 1n 2n | n ≥ 1} is not a Context Free languages. (5 MARKS) (Ref.Qn.No.14,Pg.no.40)
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE
APRIL 2017 (REGULAR)
1. Summarize the properties of Context Free languages with suitable examples.(Ref.Qn.No.7,Pg.no.27)
(OR)
2. State and explain push down Au to mata. Design a push down Au to mata for the language anbn over
{a, b}+. (Ref.Qn.No.1 & 15, Pg.no.13&41)

NOVEMBER 2017 (ARREAR)


1. Explain to p-down parsing with all example.(Ref.Qn.No.5,Pg.no.21)

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)

(a) show that {0n 1n 2n /n>=1} is not a context free language.

(5marks)

MAY 2019

1. Explain the pumping lemma and its applications

(OR)
2. Explain the closure properties of context free languages.

You might also like