Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
q
q
a
b a
a, b
a, b
q
3
TCS Sem V Computer Paper Solution By Prof Sameer Y Velankar (9324793514)
Glorious Academy www.gloriousacademy.org (Ph 9920030136 , 25621515) Page 10
Step 2 : Convert NFA to DFA
(a) Initial state of DFA is same as initial state of NFA q
0
(b) Find out transitions for symbol a and b for state q
.
o (q
, a) = { q
, q
} This is a new state of DFA
o (q
, b) = { q
}
(c) Find out transitions for symbols a & b for state q
q
o ( q
q
a
) = o ( q
, a
) U o ( q
a
)
o ( q
q
a
) = ( q
, q
) U { }
o ( q
q
a
) = { q
, q
}
AND
o ( q
q
b
) = o ( q
, b
) U o ( q
b
)
o ( q
q
b
) = {q
} U { q
2
}
o ( q
q
b
) = { q
, q
} Now state of DFA
(d) Find out transitions for state q
q
for symbols a & b
o ( q
q
2
a
) = o ( q
, a
) U o ( q
2
a
)
o ( q
q
2
a
) = {q
, q
1
} U { q
3
}
o ( q
q
2
a
) = { q
, q
1,
q
3
} Now state of DFA
AND
o ( q
q
2
b
) = o ( q
, b
) U o ( q
2
b
)
o ( q
q
2
b
) = {q
} U { }
o ( q
q
2
b
) = { q
}
q
q
q
a
b
q
q
q
q
q
a
b
b a
TCS Sem V Computer Paper Solution By Prof Sameer Y Velankar (9324793514)
Glorious Academy www.gloriousacademy.org (Ph 9920030136 , 25621515) Page 11
(e) Find out transitions for state q
q
q
3
for symbols a and b
o ( q
q
1
q
3,
a
) = o ( q
, a
) U o ( q
1
a
) U o ( q
3
a
)
o ( q
q
1
q
3,
a
) = { q
, q
} U {
} U { q
3
}
o ( q
q
1
q
3,
a
) = { q
, q
1,
q
3
}
AND
o ( q
q
1
q
3,
b
) = o ( q
, b
) U o ( q
1
b
) U o ( q
3
b
)
o ( q
q
1
q
3,
b
) = { q
} U { q
2
} U { q
3
}
o ( q
q
1
q
3,
b
) = { q
, q
2,
q
3
} New state
(f) Find out transitions for state q
q
2
q
3
for symbols a and b
o ( q
q
2
q
3,
a
) = o ( q
, a
) U o ( q
2
a
) U o ( q
3
a
)
o ( q
q
2
q
3,
a
) = { q
, q
} U {
q
3
} U { q
3
}
o ( q
q
2
q
3,
a
) = { q
, q
1,
q
3
}
q
q
q
q
q
a
b a
b
q
q
q
b
a
q
q
q
q
q
a
b a
q
q
b
q
q
b
b
a
TCS Sem V Computer Paper Solution By Prof Sameer Y Velankar (9324793514)
Glorious Academy www.gloriousacademy.org (Ph 9920030136 , 25621515) Page 12
AND
o ( q
q
2
q
3,
b
) = o ( q
, b
) U o ( q
2
b
) U o ( q
3
b
)
o ( q
q
2
q
3,
b
) = { q
} U { } U { q
3
}
o ( q
q
2
q
3,
b
) = { q
, q
3
} New state of DFA
(g) Find out transitions for state q
q
3
o ( q
q
3,
a
) = o ( q
, a
) U o ( q
3
a
)
o ( q
q
3,
a
) = o { q
, q
} U ( q
3
)
o ( q
q
3,
a
) = { q
, q
1,
q
3
}
AND
o ( q
q
3,
b
) = o ( q
, b
) U o ( q
3
b
)
o ( q
q
3,
b
) = o { q
} U {q
3
}
o ( q
q
3,
b
) = { q
, q
3
}
Now there are no more new states. Hence the final NFA.
The final states of DFA are those states which contain q
3
state of NFA.
a
a
b
q
q
q
q
q
a
b a
b
b
a
q
q
q
q
b
a
b
TCS Sem V Computer Paper Solution By Prof Sameer Y Velankar (9324793514)
Glorious Academy www.gloriousacademy.org (Ph 9920030136 , 25621515) Page 13
Q 3 c) Using pumping Lemma prove that following language is not regular
L = {a
n
b
n
/ n >= 1}
1. Let L be regular. Then there exists a DFA which will accept this language. Let P be the
pumping length
2. Choose a string w = a
p
b
p
. Clearly |w| >= p
3. Decompose w into xyz, but since |xy| <= p, xy must consist entirely of a's. Moreover, y
cannot be empty(ie |y| > 0). Let us assume that y = a.
4. Choose i = 2. This has the effect of adding one more a, without affecting the number of
b's. The resultant string has more a's than b's, hence does not belong to L. Therefore L is
not regular.
Q 4 a) Write NFA to accept following language
(a + bb)* (ba* + )
Step (a) Let us consider (a + bb)*
NFA for a is
NFA for bb is
Step b : Now we can show NFA for (a + bb) as
a
b
b
a
b b
c
c
c
c
TCS Sem V Computer Paper Solution By Prof Sameer Y Velankar (9324793514)
Glorious Academy www.gloriousacademy.org (Ph 9920030136 , 25621515) Page 14
Step c : NFA for (a + bb)* will be
Step d : Now consider (ba* + c )
NFA for b NFA for a*
NFA for (b a*) with be concatenation of NFA of b & NFA of a*
b
c
c
c a
c
c
c
c a b
c
a
b
b
c
c
c
c
c
c
c
c
TCS Sem V Computer Paper Solution By Prof Sameer Y Velankar (9324793514)
Glorious Academy www.gloriousacademy.org (Ph 9920030136 , 25621515) Page 15
Step e : NFA for (ba* + c) is
Step f : Finally NFA for entire RE (a + bb*) (ba* + c) will be concatenation of step c & step e.
Q5 a) Find out the languages defined by the following grammar
The grammar recoginizes all strings with even number of 1s and odd number of 0s.
The derivation of any string must start at start symbol S. This symbol can derive 0A or 1C. Note that only
A can be substituted with E symbol. Hence smallest string that can be derived from the above grammar
is 0.
S -> 0A
0
Clearly this string has odd 0s.
c
c c
c
c
c
c
c
c
c
c
c
c
c
c
c a b
c
c
c
c
c
c
c
c
c
c
a
b b
c
c
c
c
b c
a c
c
c
c
TCS Sem V Computer Paper Solution By Prof Sameer Y Velankar (9324793514)
Glorious Academy www.gloriousacademy.org (Ph 9920030136 , 25621515) Page 16
On the other hand , suppose we use S -> 1C as the first point in the derivation. Then there is no way to
terminate the derivation except by bringing A into our derivation which necessarily brings odd number
of 0s.
As other Example :
consider derivation of string 1111011
S -> 1C
11S
111C
1111S
11110A
111101B
1111011A
1111011
Clearly this string has even number of 1s and odd number of 0s.
ii)
S -> 0A | 1C
A -> 0S | 1B
B -> 0C | 1A |
C -> 0B | 1S
This grammar accepts all strings with odd number of 1s and odd number of 0s.
(Left as exercise to prove)
Q 5 b) Design PDA to accept language
L = {a
n
b
m
a
n
| m,n >= 1}
The push Down automata M is 7 tuple
M = ( Q, E, I, o, q
0
, z
0
, F )
For the language L =
{ }
>=
n m n
a b a |m,n 1
We have
E = { a, b }
The stack of PDA will store X for every a. The initial stack symbol is z
0
.
Hence I = { X, z
0
}
The idea = To recognize the language L =
{ }
>=
n m n
a b a |m,n 1
We will push one X in the stack when ever PDA reads a.
TCS Sem V Computer Paper Solution By Prof Sameer Y Velankar (9324793514)
Glorious Academy www.gloriousacademy.org (Ph 9920030136 , 25621515) Page 17
For example if the string w = aabbbaa. Then after reading first 2 a s the stack will be
X 2 X for 2 as
X
Z
0
initial stack symbol
Now for the next 3 bs our PDA will not change the stack. In other words we will ignore or skip all
bs.
Finally for last 2 as we will pop one X for every a
Hence the stack will contain only Z
0
at the end of process. Which means that number of as In the
begining are equal to number of as at the end.
The state diagram for PDA is
The PDA M has
Q = { q
0
, q
1
, q
2
, q
3
}
q
0
= initial state
E = { a, b }
I = { X, Z
0
}
Z
0
= initial stack symbol.
F = { q
3
} final state
o : transition function is
o : ( q
0
, a, Z
0
) = ( q
0
, XZ
0
)
o : ( q
0
, a, X ) = ( q
0
, XX )
o : ( q
0
, b, X ) = ( q
1
, X )
o : ( q
1
, b, X ) = ( q
1
, X )
o : ( q
1
, a, X) = ( q
0
, )
o : ( q
2
, a, X) = ( q
2
, )
o : ( q
2
, , Z
0
) = ( q
3
, Z
0
)
a, X / c
q
q
q
b, X / X
a, X / c
a, Z
0
/ XZ
0
b, X / X
q
c, Z
0
/ Z
0
a, X / XX
TCS Sem V Computer Paper Solution By Prof Sameer Y Velankar (9324793514)
Glorious Academy www.gloriousacademy.org (Ph 9920030136 , 25621515) Page 18
Working of this PDA can be shown by considering string w = aabbbaa. The initial state of PDA is
q
0
and initial stack symbol is
( q
0
aabbbaa, Z
0
)
( q
0
aabbbaa, XZ
0
)
( q
0
aabbbaa, XXZ
0
)
( q
1
aabbbaa, XXZ
0
)
( q
1
aabbbaa, XXZ
0
)
( q
1
aabbbaa, XXZ
0
)
( q
2
aabbbaa, XZ
0
)
( q
2
aabbbaa_, Z
0
)
( q
3
aabbbaa_, Z
0
)
q
3
is the, final state & the i/p is over. Therefore string aabbbaa is valid & accepted.
Q 6 a) Differentiate between Mealy and Moore machine with proper example. Carry out conversion of
Moore MIC to Mealy MIC
Difference between Mealy and Moore Machine
Moore Machine Mealy Machine
1 It is FSM in which output depends only on
the current state.
It is FSM in which output depends on the
current state, and current input
2. For a Moore Machine M=(Q,E, o, , q
,I)
Where
Q= finite set of states.
E =Finite set of i/p symbol
o = Transfer function
o: Q x E Q
=output function
: Q I
q
0
= Initial State
I = finite set of output symbols
For a Mealy Machine M=(Q,E, o, , q
,I)
Where
Q= finite set of states.
E =Finite set of i/p symbol
o = Transfer function
o: Q x E Q
=output function
: Q x E I
q
0
= Initial State
I = finite set of output symbols
3. If the input given to Moore machine has n
symbols, then output of Moore machine has n+1
symbols. Therefore length of output is one more
then length of input
If the input given to mealy machine has n
symbols, then output of mealy machine has n
symbols. Therefore length of output equal to
length of the input.
TCS Sem V Computer Paper Solution By Prof Sameer Y Velankar (9324793514)
Glorious Academy www.gloriousacademy.org (Ph 9920030136 , 25621515) Page 19
Following Moore machine which with output A if input ends 11, output B if input
ends with 10 output C otherwise.
The equivalent Mealy machine is
Q 6 b) Design a Turing machine that accepts the language L = {a
n
b
n
| n >= 1}
TM = ( Q , I, o, B, F )
Where = { a, b }
I = { X, , B, a, b } ( tape symbols)
The idea : every a will be over written as X and every b will be over written as .
The TM will do multiple scans of the input (from left to right and right to left) as follows
B a a b b B
|
X
Replace first a with X and move forward till we get first b
1/A
1
TCS Sem V Computer Paper Solution By Prof Sameer Y Velankar (9324793514)
Glorious Academy www.gloriousacademy.org (Ph 9920030136 , 25621515) Page 20
B X a b b B
|
y
Replace b with y & move backward till we get X. On reaching X move one symbol right & change
a to X.
B X a y b B
|
X
This process goes on till all as are changed to X & b s are changed to y. finally the TM will check if
there are any pending a or bs.
Q7 Write short note on ant four
a) Recursive and Recursively enumerable language
Recursive and Recursively enumerable languages
Recursively Enumerable Languages
A TM accepts a string w if the TM halts in a final state. A TM rejects a string w if the TM halts in a
nonfinal state or the TM never halts.
A language L is recursively enumerable if some TMaccepts it.
A language L is recursive if some TM accepts it and halts on every input. Note: the complement of
a recursive language is also recursive.
L is recursive implies L is recursively enumerable.
q
q
q
a / XR
b/ YL
q
q
Y / YR
a / aR
Y/YR
a / aL
Y / YL
Y / YR
B / BS
X/ XR
TCS Sem V Computer Paper Solution By Prof Sameer Y Velankar (9324793514)
Glorious Academy www.gloriousacademy.org (Ph 9920030136 , 25621515) Page 21
The Halting problemis r.e. but not recursive. Indeed one can run the Turing Machine and accept
if the machine halts, hence it is r.e. On the other hand the problem is undecidable.
Some other r.e. language that are not recursive :
post correspondance problem
Travelling salesman problem
Satisfiability problem
b) Intractable Problem
A problem is called Intractable if all algorithms to solve it take exponential time instead of
polynomial time.
Example: the Towers of Hanoi
Consider the following problem:
Move the three rings, which are piled up in descending order of magnitude, from peg A to peg
C, possibly using peg B in the process, moving rings one at a time, and at no time allowing a
larger ring to rest on top of a smaller one
The puzzle above has the solution:
move topmost ring from A to C;
move topmost ring from A to B;
move topmost ring from C to B;
move topmost ring from A to C;
move topmost ring from B to A;
move topmost ring from B to C;
move topmost ring from A to C;
Note that for number of rings = 3 , the total number of moves necessary = 7
Which can be written as 2
rings-1
.
TCS Sem V Computer Paper Solution By Prof Sameer Y Velankar (9324793514)
Glorious Academy www.gloriousacademy.org (Ph 9920030136 , 25621515) Page 22
We cant just imagine solving this puzzle if number of rings were in a large number. for example
if number of rings = 64 then the number of moves = 2
64
- 1.
c) Simplication of CFG
CFG can be simplified by
i) Eliminating useless symbols
ii) Eliminating E-productions
iii) Eliminating Unit-productions
Eliminating Useless Symbols from a CFG
- A symbol X is useful for a CFG if there is a derivation of the form S
*
X
*
w for
some string of terminals w. In other words we say that X is useful if we can derive some
terminal string w from X.
- If X is not useful, then we say X is useless. Such useless symbols can be removed from
CFG. The modified CFG will also generate the same language as the original CFG.
- Symbol X is useful if
1. X can be used to derive some string of terminals.
2. X must be reachable; that is, there needs to be a derivation of the form S
*
X
where and are strings of nonterminals and terminals.
- To eliminate useless symbols from a grammar, we
1. identify the non generating symbols (that is symbols which do not generate
terminal string w) and eliminate all productions containing one or more of these
symbols, and then
2. eliminate all productions containing symbols that are not reachable from the
start symbol.
- In the grammar
S AB | a
A b
We see that S a. So S is a generating symbol because S derives terminal a.
We see that A b. So A is a generating symbol because A derives terminal b.
But B does not derive any terminal string directly or indirectly.
S, A are generating. B is not generating.
Eliminating the productions containing the nongenerating symbols we get
S a
A b
Now we see A is not reachable fromS, so we can eliminate the second production to get
S a This is final Simplified CFG
TCS Sem V Computer Paper Solution By Prof Sameer Y Velankar (9324793514)
Glorious Academy www.gloriousacademy.org (Ph 9920030136 , 25621515) Page 23
Eliminating -productions from a CFG
- Consider a CFG given as S aA ; A a | . Note that this CFG will generate a language
of strings that begin with a. The empty string cannot be part of the language
generated by this CFG. Hence if L is the language generated by the above given CFG,
then we see that L does not have although there is a production rule A . Such
productions can be removed from the original CFG without changing the Language
generated.
- A nonterminal A in a grammar is nullableif A
*
.
- The nullable nonterminals can be determined iteratively.
- We can eliminate all -productions in a grammar as follows:
o Eliminate all productions with bodies.
o Suppose A X
1
X
2
... X
k
is a production and mof the kX
i
's are nullable. Then add
the 2
m
versions of this production where the nullable X
i
's are present or absent.
(But if all symbols are nullable, do not add an -production.)
- Let us eliminate the -productions from the grammar G
S AB
A aAA |
B bBB |
S, A and B are nullable.
For the production S AB we add the productions S A | B
For the production A aAA we add the productions A aA | a
For the production B bBB we add the productions B bB | b
The resulting grammar H with no -productions is
S AB | A | B
A aAA | aA | a
B bBB | bB | b
Eliminating Unit Productions from a CFG
- A unit production is one of the formA B where both A and B are nonterminals.
- Let us assume we are given a grammar G with no -productions.
- From G we can create an equivalent grammar H with no unit productions as follows.
Removal of Unit Productions
If we have a production A B (which is a unit production) and B is not a unit production
then add a production A .
- Consider one example which is the standard grammar G for arithmetic expressions:
TCS Sem V Computer Paper Solution By Prof Sameer Y Velankar (9324793514)
Glorious Academy www.gloriousacademy.org (Ph 9920030136 , 25621515) Page 24
E E + T | T
T T * F | F
F ( E ) | a
The unit productions in this CFG are
E T
T F
To remove these unit productions we can replace T on the right side of production E T
with E T * F | F (because T T * F | F)
But now we have added one more unit production E F which can be written as E ( E )
| a because F ( E ) | a.
Similar process can be done on the unit production T F
The equivalent grammar H with no unit productions is:
E E + T | T * F | ( E ) | a
T T * F | ( E ) | a
F ( E ) | a
d) Decision properties of regular languages
Decision Properties of Regular languages
Given regular languages, specified in any one of the four methods (regular expression, DFA,
NFA , NFA with E-moves) , can we develop algorithms that
answer the following questions:
1. Is the language empty?
2. Is the language finite?
3. Is a given string in the language?
The term Decision property means what we can conclude from regular expression OR DFA. In
other words If regular expression OR DFA is given what can we know about the language
accepted
For example if R is the regular expression. We want to find out
i) Membership : Is the string w in Language recognized by regular expression R.
ii) Emptiness : Is the language recognized by regular expression empty.
iii) Finiteness : Is the language recognized by regular expression finite.
TCS Sem V Computer Paper Solution By Prof Sameer Y Velankar (9324793514)
Glorious Academy www.gloriousacademy.org (Ph 9920030136 , 25621515) Page 25
e) Rice theorem
Rice's theorem states any nontrivial property about the language recognized by a Turing machine
is undecidable.
An example of a property of Turing-recognizable languages could be, say, the property of being
a regular language.
The following problems are all undecidable. Given Turing machine M,
1. does M accept the empty string?
2. does M accept no inputs at all?
3. does M accept all inputs?
4. is L(M) regular ? Context-free ? Turing-decidable?
Dec 2012
Q1 (a) What is finite Automata? Give the finite automata M for accepting
(a, b)* (b aaa)
Finite Automata M is defined as a machine with finite number of states. Such a machine
reads input string and recognizes whether the string is ACCEPTED or REJECTED by the language or
not.
Eg : We can design finite automata which will accept string over {0, 1} only if the strings
begin with 0 and end with 1.
In general finite automata M is defined as 5 tuple.
M = { Q E, o, q
0
, F }
where Q = finite set of states
E = finite set of i/p symbols
o = transition function
o = Q x E Q
q
0
= initial state
F = set of final states.
FA can be any of following types
(a) DFA (Deterministic finite automata)
(b) NFA (Non deterministic finite automata)
(c) NFA with moves
TCS Sem V Computer Paper Solution By Prof Sameer Y Velankar (9324793514)
Glorious Academy www.gloriousacademy.org
NFA for the language (a, b)* (baaa) is
Q1 (b) Explain Chomsky Heivarchy with language used forms of productions in grammer
& accepting devices.
The Chomsky hierarchy is a way of classifying formal grammars into four types
The Chomsky hierarchy consists of the following levels:
Type-0 grammars (unrestricted grammars
exactly all languages that can be recognized by a
known as the recursively enumerable languages
recursive languages which can be
Type-1 grammars (context-sensitive grammars
These grammars have rules of the form
and strings of terminals and nonterminals. The strings
must be nonempty. The rule
any rule. The languages described by these grammars are exactly all languages that can be
recognized by a linear bounded automaton
is bounded by a constant times the length of the input.)
Type-2 grammars (context-free grammars
defined by rules of the form
nonterminals. These languages are exactly all languages
deterministic pushdown automaton
the syntax of most programming languages
Type-3 grammars (regular grammars
restricts its rules to a single nonterminal on the left
consisting of a single terminal, possibly followed (or preceded, but not both in the same
grammar) by a single nonterminal. The rule
appear on the right side of any rule. These languages are exactly all languages that can be
decided by a finite state automaton
obtained by regular expressions
patterns and the lexical structure of programming languages.
b
q
0
TCS Sem V Computer Paper Solution By Prof Sameer Y Velankar (9324793514)
www.gloriousacademy.org (Ph 9920030136 , 25621515)
NFA for the language (a, b)* (baaa) is
xplain Chomsky Heivarchy with language used forms of productions in grammer
is a way of classifying formal grammars into four types
The Chomsky hierarchy consists of the following levels:
unrestricted grammars) include all formal grammars. They generate
exactly all languages that can be recognized by a Turing machine. These languages are also
recursively enumerable languages. Note that this is different fr
which can be decidedby an always-halting Turing machine
sensitive grammars) generate the context-sensitive languages
These grammars have rules of the form with a nonterminal and
strings of terminals and nonterminals. The strings and may be empty, but
must be nonempty. The rule is allowed if does not appear on the right side of
any rule. The languages described by these grammars are exactly all languages that can be
linear bounded automaton (a nondeterministic Turing machine whose tape
is bounded by a constant times the length of the input.)
free grammars) generate the context-free languages
with a nonterminal and a string of terminals and
nonterminals. These languages are exactly all languages that can be recognized by a non
pushdown automaton. Context-free languages are the theoretical basis for
programming languages.
regular grammars) generate the regular languages. Such a grammar
restricts its rules to a single nonterminal on the left-hand side and a right-hand side
consisting of a single terminal, possibly followed (or preceded, but not both in the same
by a single nonterminal. The rule is also allowed here if
appear on the right side of any rule. These languages are exactly all languages that can be
finite state automaton. Additionally, this family of formal languages can be
regular expressions. Regular languages are commonly used to define search
patterns and the lexical structure of programming languages.
a
a
a
q
1
q
2
q
3
TCS Sem V Computer Paper Solution By Prof Sameer Y Velankar (9324793514)
Page 26
xplain Chomsky Heivarchy with language used forms of productions in grammer
) include all formal grammars. They generate
. These languages are also
. Note that this is different from the
uring machine.
sensitive languages.
a nonterminal and ,
may be empty, but
appear on the right side of
any rule. The languages described by these grammars are exactly all languages that can be
(a nondeterministic Turing machine whose tape
free languages. These are
a string of terminals and
that can be recognized by a non-
free languages are the theoretical basis for
. Such a grammar
hand side
consisting of a single terminal, possibly followed (or preceded, but not both in the same
does not
appear on the right side of any rule. These languages are exactly all languages that can be
. Additionally, this family of formal languages can be
define search
q
4
TCS Sem V Computer Paper Solution By Prof Sameer Y Velankar (9324793514)
Glorious Academy www.gloriousacademy.org
Grammar Languages
Type-0
Recursively
enumerable
Turing machine
Type-1 Context-sensitive
Linear
machine
Type-2 Context-free Non
Type-3 Regular Finite state automaton
Q1 (c) Differential between mealy & moore
Moore Machine
1 It is FSM in which output depends only on
the current state.
2. For a Moore Machine M=(Q,E, o, ,
Where
TCS Sem V Computer Paper Solution By Prof Sameer Y Velankar (9324793514)
www.gloriousacademy.org (Ph 9920030136 , 25621515)
Automaton
Production rules
(constraints)
Turing machine
Linear-bounded non-deterministic Turing
machine
Non-deterministic pushdown automaton
Finite state automaton and
Differential between mealy & moore machines.
Mealy Machine
It is FSM in which output depends only on It is FSM in which output depends on th
current state, and current input
E, o, , q
,I)
TCS Sem V Computer Paper Solution By Prof Sameer Y Velankar (9324793514)
Glorious Academy www.gloriousacademy.org (Ph 9920030136 , 25621515) Page 28
Q= finite set of states.
E =Finite set of i/p symbol
o = Transfer function
o: Q x E Q
=output function
: Q I
q
0
= Initial State
I = finite set of output symbols
Q= finite set of states.
E =Finite set of i/p symbol
o = Transfer function
o: Q x E Q
=output function
: Q x E I
q
0
= Initial State
I = finite set of output symbols
3. If the input given to Moore machine has n
symbols, then output of Moore machine has n+1
symbols. Therefore length of output is one more
then length of input
If the input given to mealy machine has n
symbols, then output of mealy machine has n
symbols. Therefore length of output equal to
length of the input.
Q1 (d) Give example & explain ambiguous context free grammer.
An ambiguous grammar is a formal grammar for which there exists a string that can have more than
one leftmost derivation, while an unambiguous grammar is a formal grammar for which every valid
string has a unique leftmost derivation.
The context free grammar
A A + A | A A | a
is ambiguous since there are two leftmost derivations for the string a + a + a:
LeftMost Derivation 1
A -> A + A
a + A
a + A + A
a + a + A
a + a + a which is the required string
LeftMost Derivation 2
A -> A + A
TCS Sem V Computer Paper Solution By Prof Sameer Y Velankar (9324793514)
Glorious Academy www.gloriousacademy.org (Ph 9920030136 , 25621515) Page 29
A + A + A
a + A + A
a + a + A
a + a + a which is the required string
As another example, the grammar is ambiguous since there are two parse trees for the string a +
a a:
Q2 a) Design a finite state machine that adds 2 binary numbers of equal length
We want to construct a finite state machine that will add two numbers.
The input is two binary numbers, (x
n
x
1
x
0
)
2
and (y
n
y
1
y
0
)
2
At each step, we can compute (x
i
+y
i
) starting with (x
0
+y
0
).
If (x
i
+y
i
)=0, we output 0.
If (x
i
+y
i
)=1, we output 1.
If (x
i
+y
i
)=2, we have a problem.
The problem is we need a carry bit.
In fact, our computation needs to know the carry bit at each step (so we compute x
i
+y
i
+c
i
at each step),
and be able to give it to the next step.
We can take care of this by using states to represent the carry bit.
We will use the following states
State S
0
occurs if the carry bit is 0
State S
1
occurs if the carry bit is 1
Since when we begin the computation, there is no carry, we can use S
0
as the start state,
So, how does which state we are in affect the output?
If we are in state S
0
(we have a carry of 0)
If (x
i
+y
i
)=0, we output 0, and stay in state S
0
If (x
i
+y
i
)=1, we output 1, and stay in state S
0
TCS Sem V Computer Paper Solution By Prof Sameer Y Velankar (9324793514)
Glorious Academy www.gloriousacademy.org (Ph 9920030136 , 25621515) Page 30
If (x
i
+y
i
)=2, we output 0, and go to state S
1
If we are in state S
1
(we have a carry of 1)
If (x
i
+y
i
+1)=1, we output 1, and go to state S
0
If (x
i
+y
i
+1)=2, we output 0, and stay in state S
1
If (x
i
+y
i
+1)=3, we output 1, and stay in state S
1
From the previous observations, we can construct the state table and state diagrams
for the binary adder
Next State Output
Input Input
State 00 01 10 11 00 01 10 11
S
0
S
0
S
0
S
0
S
1
0 1 1 0
S
1
S
0
S
1
S
1
S
1
1 0 0 1
TCS Sem V Computer Paper Solution By Prof Sameer Y Velankar (9324793514)
Glorious Academy www.gloriousacademy.org (Ph 9920030136 , 25621515) Page 31
Q2 (b) Give rules for defining languages associated with any regular expression.
Let L
1
= all words beginning with a
Let L
2
= all words ending with a
What is L
1
intersection L
2
?
Q3 (a) Give the statement for pumping lemma for regular langauages.
Q3 (b) Construct NFA with moves for
(i) (00 + 1 )* (10)*
(ii) (( 0 + 1 )* 10 + ( 00 )* ( 11 )* )*
(i) (00 + 1 )* (10)*
c
c
0
0
c
c
c
c
c
c
c c
c
c
c 1 1
0
0