Automata Theory
Automata Theory
Automata Theory
ip
AUTOMATON
q1, q2, ,qn
o1
o2
...
...
i1
i2
i3
oq
Model of a automaton
$ $ $
Reading Head
Characteristics of automaton:
1. Input: i1, i2,,ip are the input of the model
each of which can take a finite number of
fixed values from an input I.
2. Output: o1, o2,,oq are the outputs of the
model each of which can give the finite
number of fixed values to an output O.
3. States: At any instant of time the automaton
can be in one of the states q1, q2, ,qn.
4. States Relation: The next state of an
automaton at any instant of time is determine
by the present state and present input.
5. Output relation: The output is related to
either state only or to both the input and the
state.
Note: An automaton in which the output depends
only on the input is called an automaton without a
memory. Ex:- logic gate. An automaton in which
the output depends on the state and input is called
an automaton with a finite memory. Ex:- flipflops, shift register, Mealy machine. An
automaton in which the output depends only on
the states of the machine is called Moore
Machine.
$ $
Input Tape
Finite Control
Block diagram of a finite automaton
I)
1/1
1/0
q0
q1
0/0
Fig. A Transition System.
2.
state.
III. is finite Q Q .
q0
0/0
^/0
q1
q3
^/0
q2
1011/0
3.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
Corresponding
Regular Expression
{^}
^
{0}
0
{001} or ({0},{0},{1})
001
0+1
{0,1} or ({0}{1})
0+10
{0,10} or ({0} {10})
{1,^}{001}
(1+^)001
{110}*{10}
(110)*(0+1)
{1}*{10}
(1)*10 or 1*0 or 1+0
*
{10,111,11010}
(10+111+11010)*
{0,10}*{{11}*U{001,^}) (0+10)*((11)*+001+^)
Definition:
Regular Languages and Regular
Expressions over : The set R of regular languages
over and the corresponding regular expressions are
defined as follows:
0
q2
q3
q0
q1
Input symbol
q1
1
1
q0
0
0
1
q1
q0
q3
q2
q3
1
q2
( q1, 1 ) ( q0, ) q0
0
1
1
1
1 q
0 q
Hence q0 q1
q3 q1 q0
0
2
aa(NFA)
q1
(a+b)(c+d)
q0
q1
q0
b
q2
Figure: Transition system representing NDA
If an automaton is in a state {q0} and the input
symbol is 0, what will be the next state?, from
figre it is clear that next state will be either {q0}
or {q1}. Some moves of the machine cannot be
determined uniquely by the input symbol and the
present state. Such machines are called NDA.
Definition: A Non-Deterministic Finite Automata
(NDFA) is a 5 tuple (Q, , , q0, F), where
I) Q is a finite non-empty set of states.
q0
q0
q0
q0
q1
0+1
q0
0 + 10
q0
(1 + ) 001
q0
q1
q2 1
0 ,1
q2
F
1
0 q1 1
1
1+0
q0 1
a
q1
q0
(10+111+11010)
q1
q1
1
F
a
a
aa
0
a
q0
qf
0
q0
q3 1
b
(a+b)c
q1 0
(110)* (0+1)
q0
a+b
a ,b
q0
q0
a ,b
(a + b)
001
q4
q2
0
1
q3
q1
q0
q3
1
q5
Example: Design a FA which accepts the
language L= {w/ w has both an even number of
0s and an even number of 1s over alphabets
= {0, 1}}.
Solution:
1
1
q5
0, 1
1
0
q2
q2
q4
1
Example: Design FA for the Language
L ={(01)I 12j | i1, j1}.
Solution: By analysis Language L, it is clear that
FA will accepts strings start with any number of
01(not empty) and end with even number of 1s.
1
0
0
1
1 q4
1
q0
q3
q1
q2
q2
q0
q0
q1
q1
0 0
q3
example:NFA
0
1
q0
1
0 ,1
q1
q2
0
q3
w =0100
q4
0 ,1
Example: NFA
II.
0 ,1
q0
q1
0 ,1
q2
0 ,1
q3
* (q0, 11) = ( r , 1 )
r (q0,1)
= (r,1)
r(q0,q1)
= (q0,1)(q1,1)
* ( q0 , 01 )
* (q0, 111)
{ q0, q1 } { q2 }
= ( r, 1 )
r (q0,0)
= ( r, 1 )
r { q0 }
= (q0, 1 )
= {q0, q1 }
= ( r, 1 )
r (q0,11)
= ( r, 1 )
= 1 (1 (q1, y),a )
= 1 ( (q0, y),a )
= ( r, a)
r (q0,y)
= ( q0, ya )
automaton
q0 , {q0} )
1
q1
q0q1
Solution:
i.
The states are subset of {q0, q1} i.e. {},
{q0}, {q1}, { q0, q1}.
ii.
[q0] is initial state.
iii.
[q0] and [q0, q1] are final states, both
states contain q0.
iv.
Transition table for DFA
state/
0
1
[q0]
[q1]
[q0]
[q1]
[q1]
[q0, q1]
[q0, q1]
[q0, q1]
[q0, q1]
When M has n-states, the corresponding FA has
2n states. However, we need not to construct
for all these 2n states. But only for those states
are reachable from {q0}, we halt on when no
more new states appear under the input.
Example:
Find a deterministic acceptor
equivalent to M = ( [q0, q1, q2 ], {a, b}, , q0,
{q2} ) where is
state/
q0
q1
q2
0
q0, q1
q0
__
1
q2
q1
q0, q1
Solution:
M = { 2Q, {a,b}, , [q0], F }
F = { [ q2], [q0, q2], [q1. q2], [q0, q1, q2] }
state/
[q0]
[q2]
[q0, q1 ]
[ q1, q2]
0
[q0, q1]
__
[q0, q1 ]
[q0]
1
[q2]
[q0, q1]
[ q1, q2]
[q0, q1]
0
q0, q1
q2
q3
__
1
q0
q1
q3
q2
0
[q0, q1]
[q0,q1,q2]
[q0, q1, q2, q3]
[q0,q1,q2]
[q0, q1, q2, q3]
1
[q0]
[q0, q1]
[q0,q1,q3]
[q0,q1,q2]
[q0, q1, q2, q3]
[q3]
[q4]
[q3]
[q4]
[qf]
[q3, qf]
[q1, q3]
[q2, q4]
[q1]
[q1, q3]
[q3, qf]
[q3]
[q3, qf]
[q4]
[q2, q4]
[q3, qf]
S3
S4
S5
q0
q5
qf
b
q2
q4
[q0, q2]
[q0, q1]
[q0, q2]
[q0, q1]
[q1, q2]
[q0, q2]
[q1, q2]
[q0, q2]
[q0]
b
S2
Q = { S, A, B }
= { a, b}
F = { A, B }
b
B
Example: Consider the following NFA with moves. Construct an equivalent DFA.
a
q3
q5
q1
q2
q7
q8
b
q4
q6
q9
state/
S
A
B
C
a
A
B
B
B
C
C
C
B
a
a
2. Removal of Null Moves
The basic Strategies for the removal of null
string are as follows:
Let A and B be the two states connected by a
null string .
b) Concatenation
f1
q0 = q1
B
A
B
f2
f1
q2
f2
Concatenation(PQ)
c) Kleene*
qf
f1
q1
f1
Kleenee* P*
Kleenes Theorem:
(a)
(c)
(b)
a) Union
f1
q0
q1
f1
q2
f2
f2
Union(P + Q )
q8
0,1
q0
(a)
(0 +
1)*
q1
(0 + 1)
q5
(00 + 11)
q2
(b)
(00 + 11)
q1
(0 +
q2
q6
q1
q7 1
0,1
q2
q8
(e)
qf
10
qf
( 0 + 11) 0*1
(b)
q1
0
0+11
q2
q0
1
q4
q0
qf
qf
q3
(c)
q1
qf
(0 + 1)
(d)
(a)
q0
qf
1)*
q8
q6
q0
q2
10 + ( 0 + 11) 0*1
q0
q1
0, 1
Figure 4
Figure 3
a, b
Figure 2
Figure 1
q0
q5
0,1
Previous
State
q0
q7
qf
q3
(d)
q1
q2
q2
d
(e)
qf
qf
S aSb | ab
S aSXc | abc
cX Xc
bX bb
it can be seen that every string in the language
L has the length 3n.
expression
- a ( a + b )* a
32. Strings having atmost one pair 0s or
atmost one pair of 1s.
- ( 1 + 01)* + ( 1 + 01)* 00 ( 1 + 10)* +
( 0 + 10 )* + ( 0 + 10)*11(0 + 01 )*
33. String in which number of occurrences of
a is divisible by 3.
- ( b* a b* a b* a b*)*
34. String with 3 consecutive
- ( a + b )* bbb ( a + b )*
35. Strings beginning with 00
- 00 ( 0 + 1 )*
36. String ending with 00 and beginning with 1.
- 1 ( 0 + 1 )* 00
Difference between DFA and NDFA
Sr.
DFA
NDFA
1.
Deterministic
Finite Automata
Non-Deterministic
Finite Automata
2.
Every transition is
unique
and
deterministic
in
nature.
3.
Null-transitions
are not allowed
Null-transitions
allowed.
4.
Requires
less
memory
as
transition diagram
needs less number
of states.
Requires
more
memory as transition
diagrams needs more
number of states.
5.
Range
transition
:QQ.
6.
are
of Range of transition is
is :Q2Q
6.
7.
8.
Applications of Finite Automaton.
1. Lexical Analyzers: Lexical analysis is a
part of compilation and used to recognize
the validity of input programs. Whether
the input program is grammatically
constructed or not.
9.
0
Moore and Mealy Machines: Finite automata
with output: The finite automata which we were
considered in the earlier have binary output i.e.
either they accept the string or they do not accept
the string. This acceptability was decided on the
basis of the reachability of final state by the
initial state. Now, we remove this restriction and
consider the model where the outputs can be
chosen from some other alphabets.
The value of the output function Z(t) in the most
general case is a function of the present state q(t)
and present input x(t), i.e.
Z(t) = (q(t), x(t) )
Where is called the output function. This
generalized model is usually called the mealy
machine.
if the output function Z(t) depends only on the
present state and is independent of the current
input, the output function may be written as
q0/0
q3/0
0
1
q1/1
0
q2/0
0
Transition graph
input string 0111
0
q0
q3 1
q0 1
q1 1
q2
output 00010
Z(T) = (q(t))
Example: Mealy Machine
Present
state
q0
q1
q2
q3
Present
State
Next State
a=0
a=1
state
output
state
output
q3
0
q2
0
q1
1
q4
0
q2
1
q1
1
q4
1
q3
0
q1
q2
q3
q4
q3
Output
( )
0
1
0
0
0/1
q1
0/1
q4
q2
Transition graph
Input string : 0011
q1
q3 0
q2 1
q4 1
q3
Next State
a=0
a=1
q3
q1
q1
q2
q2
q3
q3
q0
Output
()
0
1
0
0
Present
a=0
a=1
State
Next
O/p
Next
O/p
q3
0
q1
1
q0
q1
q1
1
q2
0
q2
q2
0
q3=
0
q3
q3
0
q0
0
Convert the Following Moore Machine to Mealy
Machine.
Present
State
q1
q2
q3
Present
State
q1
q2
q3
Next State
a=0
a=1
q1
q2
q1
q3
q1
q3
Next
q1
q2
q3
a=0
O/p
0
0
0
Next
q2
q3
q0
Output
()
0
0
1
a=1
O/p
0
1
1
Next
q3
q1
q2
q4
a=0
O/p
0
1
1
1
Next
q2
q4
q1
q3
a=1
O/p
0
0
1
0
Present
State
q1
q20
q21
q3
q40
q41
Present
State
q1
q20
q21
q3
q40
q41
Next
q3
q1
q1
q21
q41
q41
a=0
O/p
0
1
1
1
1
1
Next
q20
q40
q41
q1
q3
q3
Next State
a=0
a=1
q3
q20
q1
q40
q1
q41
q21
q1
q41
q3
q41
q3
a=1
O/p
0
0
1
1
0
0
Output
()
1
0
1
0
0
1
Next
q2
q2
q2
Present
State
q1
q21
q22
q3
q3
Next
q21
q22
q22
q21
q21
Present
State
q1
q21
q22
q3
q3
a=0
a=0
O/p
z1
z2
z1
Next
q3
q3
q3
O/p
z1
z2
z2
z1
z1
Next
q31
q31
q31
q32
q32
Next State
a=0
a=1
q21
q31
q22
q31
q22
q31
q21
q32
q21
q32
a=1
a=1
O/p
z1
z1
z2
O/p
z1
z1
z1
z2
z2
Output
()
z1
z2
z1
z2
counts
aab
a
q0/0
q0/1
b
q0/0 b
q0/1
0/1
1/0
1/0
1/0
Question: Construct a mealy machine which
calculate residue mod-4 for each binary string
treated as binary integer.
Solution:
M0 = (Q, , , , ,q0)
Q = { q0, q1, q2, q3 }
= { 0, 1}
= { 0, 1, 2, 3}
0/0
q0
1/3
1/1
0/2
0/0
q2
0/1
1/3
q1
1/1
q2
1/0
0/0
q0
0/2
q3
R = Q + RP ---(i)
a
q1
b
q2
i+1
= Q + QP + QP + + QP + RP
= Q ( + P + P2 + + Pi) + RPi+1
a
q3
...
2
q2 = q1a( b + aa )*
Now substitute q2 in q1
QP*
q1 = q1a + q2b +
R = R
q1 =
+ Q QP*
.( a + a(b + aa )*)*
= ( a + a(b + aa )*)*
q2 = ( a + a ( b + aa )*)*a( b + aa )*
q3 = ( a + a ( b + aa )*)*a( b + aa )*a
Since q3 is a final state, the set of strings
recognized by the graph.
Example:
a
q1
b
a
q3
q2
a
b
b
q4
q3 = q1b
q1 = q2b + q3b +
q1 = q1ab + q1ba +
q1 = q1 (ab + ba ) +
R =R
q0 = * ( q 0 , )
+ Q QP*
q1 = * ( q0 , a1 )
q1 = .(ab + ba )*
q1 = (ab + ba )*
q2 = * ( q0 , a1a2 )
q3 = * ( q0 , a1a2a3 )
. . .
Example:-
0
q1
Pumping
Leema
for
regular
Languages/Regular Sets:
Pumping Lemma: let M = (Q, , , q0, F ) be a
finite automaton with n states. let L be the regular
set accepted by M. let x L and |x| n, then there
exists such that x = , and m L
for each m 0.
q2
0, 1
0
q3
q2 = q11 + q21
q3 = q20 + q3( 0 + 1 )
q1 = q10 +
R = RP + Q QP*
q1 = .(0)* = 0 *
q2 = q11 + q21
q2 = 0*1 + q21
R = Q + RP QP*
q2 = 0*11*
solve q3.
q1 + q2
0* + 0*11*
0* ( + 11*) = 0*1*
qn = * ( q0 , a1a2. . . an )
qi
q0
suppose qi = qi + p
qf
0ii+pn
then
* ( q0 , a1a2. . . ai ) = qi
* ( qi , ai+1ai+2. . . ai+p ) = qi+p = qi
* ( qi , ai+p+1ai+p+2. . . an ) = qf F
To simplify the notation,
let = a1a2. . . ai
= ai+1ai+2. . . ai+p
= ai+p+1ai+p+2. . . an
if i 0 will be
if i + p 0 will be
16
Sol: L = {, a , a , a , a , . . . }
= { , a, aaaa, aaaaaaaaa, . . . }
Step 3: Consider .
| 2 | = | | + 2|| + | | > | | + || + || as || >
0.
this means
n 2 = | | = | | + || + || < | 2 |
as || n we have || n. therefore
| 2 | =| | + 2|| + | | n 2 + n.
i.e.
n 2 <| 2 | n 2 + n < n 2 + n + n + 1
Example: Is L = {a | n 1} regular?
Solution: We can write a2n as aa(a2)m, where m
0 } is simply {a2 }* so L is represented by the
regular expression aa(P)*, where P represents
{a2}. the corresponding FA is shown in figure:
q0
q1
qf
q2
a
q1
d0,1
Fo
q0
q1
q2
0,1
s1
s2
and
By De Morgans Law, S1 S2 = ( s1 s2 ).
Hence, (S1 S2) is also a regular set.
Therefore, regular sets are closed over the
intersection operation.
6. Regular sets are closed over the difference
operation.
Since L1 is regular.
q0
qi
qk
qf
q0
qi
qk
q0
q1
qf
since L2 is regular
If the number of equivalent classes over a
language L is finite then the language L is
regular. This statement is refered to as MyhillNerode theorem.
L3 = {0n1n|n>0} to recognize this machine should
be able to remember how many 0s it has seen
and if the no. of 1s is exactly the same as that of
number of 0s. However, the FSM is devoid of
any kind of memory the 0 count and match it
a is a palindrome
b is a palindrome
if a string x is a palindrome then axa, bxb are
palindromes.
To recognize L4, a machine should be able to
mark the middle of the string and match the
symbols on the both sides of the middle. FSM
has no memory, so counting is nt possible.
another possible mechanism is to create a class
corresponding to the string before the middle of
the input string and to match this class with the
class of the reverse string after the middle. if they
are same then the string is a palindrome.
Otherwise, it is not. However the number of
possible classes for the string before the middle is
infinite; hence the language L4 can not be
implemented on a finite automaton and hence, is
not regular.
L5 = {ww | w is a string over {0.1} is not regular}
because the language L5 belongs to the same
group as the language L4.
L6 over = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; a string w
over * belongs to L6 if the number formed by w
is divisible by 9.
For example: 9, 18, 27, . . . are belongs to L6 1,
2, 3, . . . rest does not belongs to L6.
Here string can be divided into 9 equivalent
classes, with each class corresponding to a
remainder 0 to 8. if remainder 0, then the string
0
q0
q1
q2
q3
q4
q5
q6
q7
q8
1
q1
q2
q3
q4
q5
q6
q7
q8
q0
2
q2
q3
q4
q5
q6
q7
q8
q0
q1
3
q3
q4
q5
q6
q7
q8
q0
q1
q2
Input Symbol
4 5 6
q4 q5 q6
q5 q6 q7
q6 q7 q8
q7 q8 q0
q8 q0 q1
q0 q1 q2
q1 q2 q3
q2 q3 q4
q3 q4 q5
7
q7
q8
q0
q1
q2
q3
q4
q5
q6
8
q8
q0
q1
q2
q3
q4
q5
q6
q7
9
q0
q1
q2
q3
q4
q5
q6
q7
q8
a
q1(N)
q5(N)
qf(F)
q6(N)
qf(F)
q5(N)
q5(N)
q0(N)
Input State
b
q4(N)
qf(F)
q5(N)
q4(N)
q5(N)
q3(N)
qf(F)
qf(F)
Input Symbol
aaa
aab
aba
abb
baa
bab
q5(N) q3(N) q0(N) qf(N) q0(N) qf(N)
q5(N) q3(N) q0(N) qf(N) q0(N) qf(N)
bba
bbb
q5(N) q3(N)
q5(N) q3(N)
(minimization
Finite
Step 1: (construction of 0): By definition of 0level equivalence, 0 = {Q10, Q20 } where Q10 is
the set of all final states and Q20 = Q Q10.
Step 2: (Construction of k+1 and k ): let Qik be
any subset in k. if q1 and q2 are in Qik, they are
(k+1)-equivalent provided (q1, a ) and (q2, a )
are in the same equivalent class in k for every
a.
Current
state
q0
q3
q5
aa
q5(N)
q5(N)
q5(N)
Input String
ab
ba
qf(F) qf(F)
qf(F) qf(F)
q3(N) q6(N)
bb
q5(N)
q5(N)
q4(N)
q1
q6
q5(N)
q5(N)
q3(N)
q3(N)
q0(N)
q0(N)
qf(F)
qf(F)
q2
q4
q0(N)
q0(N)
qf(F)
qf(F)
q5(N)
q5(N)
q3(N)
q3(N)
Example:
Current State
q0
q1
q2
q3
q4
q5
q6
q7
Input Symbol
a
b
q1(N)
q0(N)
q0(N)
q2(N)
q3(F)
q1(N)
q3(F)
q0(N)
q3(F)
q5(N)
q6(N)
q4(N)
q5(N)
q6(N)
q6(N)
q3(F)
By Step 1:
Q10 = {q3}, Q20 = Q Q10
0
q1(N)
q6(N)
q0(N)
q2(F)
q7(N)
q2(F)
q6(N)
q6(N)
Input Symbol
1
q5(N)
q2(F)
q2(F)
q6(N)
q5(N)
q6(N)
q4(N)
q2(F)
By Step 1:
Q10 = F = {q2}, Q20 = Q Q10 = { q0, q1, q3, q4, q5,
q6, q7 }
0 = {{q2}, { q0, q1, q3, q4, q5, q6, q7 }}. As {q2 }
can not be partitioned further.
Q11 = {q2 }, Q21 = {q3, q5 }, Q31 = {q1, q7 }, Q41 =
{q0, q4, q6 }
1 ={ {q2 }, {q3, q5 }, {q1, q7 }, {q0, q4, q6 }}
Q12 = {q2}, Q32 = {q1, q7 }, Q22 = {q6 }, Q42 = {q3,
q5}, Q52 = {q0, q4 }.
2 ={{q2}, {q1, q7 }, {q6 }, {q3, q5}, {q0, q4 }}.
3 = 2 (n+1 = n )
q3
Figure: Automaton M
c
q4
d
q5
q7
c
q6
Figure: Automaton M
Current state
Input Symbol
c (qc, qc)
(q1, q4)
(q3, q6)
(q2, q7)
(q3, q6)
d (qd, qd)
(q2, q5)
(q1, q4)
(q3, q6)
(q1, q4)
q1
d d
q2
q3
q4
q5
q7
c
d
q6
d
Sol: The initial states in M1 and M2 are q1 and q4
respectively. Hence the first column in
comparison table is (q1, q4). q2 and q5 are dreachable from q1 and q4. we see fro the
comparison table that q1 and q6 are d-reachable
(q,q)
(q1,q4)
(q2,q5)
Input Symbol
c
d
(qc,qc)
(qd,qd)
(q1,q4)
(q2,q5)
(q3,q7)
(q1,q6)
Grammar:
Before giving the definition of
grammar, we shall study, two types of sentences
in English, with a view to formalizing the
construction of theses sentences. The sentences
we consider are those with a noun and a verb, or
those with a noun-verb and adverb (such as Ram
ate quickly or Sham ran). the sentences Ram
are quickly has the words Ram, ate, quickly
written in that order. If we replace Ram by
sham, tom, etc. i.e. be any verb in the past
tense, and quickly by slowly i.e. by an adverb,
we get other grammatically correct sentences so
the structure of Ram ate quickly can be given as
<noun><verb><adverb>.
for <noun>, we can substitute Ram, Sham,
Tom etc.
for <verb>, we can substitute ate, ran,
walked etc.
for <adverb>, we can substitute quickly,
slowly etc.
we have to note that <noun> <verb> <adverb>
is not a sentence but only the description of a
particular type of sentence. if we replace
<noun> <verb> and <adverb> by suitable
words, we get actual grammatically correct
sentences.
Let S be a variable denoting a sentence. Now we
can form the following rules to generate two
types of sentences.
Productions:
S <noun><verb><adverb> | <noun><verb>
<noun> Ram|Sham|Tom|Geeta
<verb> ran|ate|walked
<adverb> slowly|quickly
Example:
Sol: As S is a production. S . So is in
G
L(G).
also, for all n 1.
bSb
bnSbn (using n step
derivation)
n m
m n
or
bnamSambn
S a b Sb a
(using m step
derivation)
or
bnamambn (using
S anbmbman
S
R
*
So L(G) = { : (a+b) }
S
S
aSa
anSan
or
or
S S S ( using S S S)
L
a S ( using S a)
L
a S + S ( using S S + S )
L
a a + S ( using S a )
L
a a + b ( using S b )
L
The last symbol from the left is b, so using S b ).
S S S ( using S S S)
R
S S S + S ( using S S + S)
R
(since, in the above sentential form second
symbol from the right is so, we can not use S
a|b. therefore, we use S S + S ).
S S + b (using S b )
R
S a + b (using S a )
R
a a + b (using S a )
R
(Note: Rightmost variable is highlighted in each step).
Chomsky
classification
of
Grammars|
Languages (Chomsky Hierarchy): Noam
chomsky has classified all grammars in four
categories ( type 0 to type 3) based on the right
hand side forms of the productions.
Type 3: This is most restricted type. productions
of type A or A B|B. where A, B VN,
and T are known as type 3 or regular
grammar productions. in production of type 3. S
is also allowed, if is in generated
language.
for example: production S aS, S a are type
3 production.
Left-linear production: A Ba
Right-linear production: A aB
ii)
iii)
S S+S
S a
S
a
iv)
S b
S
b
SSS
Turing
Machine
Push down
Automata
Finite
Automata
Linear Bounded
Autmata
A A1|A2|A3||An|1| 2| 3|| n
A 1 A| 2 A| 3 A|| n A
a
b
Ambiguity: A grammar G is ambiguous if there
exists some string L(G) for which there are
two or more distinct derivation trees or there are
two of more distinct leftmost derivations.
Example: CFG E E + T | T
i.e.
Left-most derivation
a) S S + S a + S a + S + S a + a +
L
L
L
L
Sa+a+a S
L
+
b) S S + S S + S + S a + S + S a
L
L
L
L
+a+Sa+a+a
L
S
S
S
a
E E + T is replaced by E TE
T T F is replaced by T FT
=a+a+a
F id.
Where E +TE |
S S+S|a
TTF|F
S
a
Where T FT |
So, productions of G are:
E +TE
E +TE |
T FT
T FT |
F id.
Left Factoring: Two or more productions of a
variable A of the grammar G are said to have left
factoring if A productions are of the form
A 1| 2| 3|| n, where i (VN T)*
and does not start with . All these A-productions
have common left factor .
Elimination of Left Factoring: let the variable A
has (left factoring) productions as follows:
A 1| 2| 3|| n|1| 2| 3|| n, where
1, 2, 3, , n and 1, 2, 3, , n, do not
contain as a prefix, then we replace Aproduction by,
A A | 1| 2| 3|| n
Where A 1| 2| 3|| n
S Sa | a
Sol: Step 1:
Example: S aS | aA | , A
Solution: S aS | aA | a | a
S aS | a
Example: S a | Xb | aYa,
XY|
Yb|X
Solution: S a | Xb | aYa | b | aa
XY
Yb|X
AB
BC
C aS
Solution: we find S aA bS aC aaS.
It means we are getting as after two intermediate
steps, A B C aS. So, we have the
production S aaS, instead of A B, B C,
C aS, which create indirection.
Steps for removing unit production.
Application of Grammars:
1. Specifying syntax of programming
languages.
2. Representation symtactic structure in
natural languages,
3. Model of computation.
Step 1: W1 = { A, C } as A a and C b be
productions with a terminal string on RHS.
W2 = { A, C } { A1 | A1 with (
{ A, C } ) }
= { A, C } { S } as we have S CA.
W3 = { A, C, S } { A1 | A1 with (
{ A, C } ) }
= { A, C, S } = W2
VN = W2 = { S, A, C }
P = { A1 |A1, ( VN ) }
P = { S CA, A a, C b }
Thus,
G1 = ({S, A, C} , { a, b},
{ S CA, A a, C b }, S )
Step 2: W1 = { S }
W2 = { S } { A, C } [because S CA ]
= { S, A, C }
W3 = { S, A, C } { a, b} = { S, A, C, a, b}
[because A a, C b ]
As W3 = VN T, P = P
W4 = { C, E, A, S} = W3 = W3
Hence
VN = W3 = {S, A, C, E}
W3 = {S, A, a} { S, b, C } = {S, A, C, a, b}
As we have C abb
production.
P = { A | A1 W3 }
S ASA,
X1 SA ( In CNF)
W4 = W3 {a, b} = W3
Hence
P4 : S b ( In CNF)
variable A, we get
S AX1, where
A a (In CNF)
S AX1 | BX2 | a | b
A a, B b
X1 SA, Y2 SB.
Example:
Let
be
the
grammar
with
productions
S AACD
A aAb |
C aC | a
D aDa | bDb | .
D aDa | bDb | aa | bb
A XaAXb | Xa Xb
C XaC | Xa
S AT1 T1 AT2 T2 CD
S AT2
S AU1 U1 AC
A XaW1 W1 AXb
A XaXb
C XaC | a
D XaY1
D XbZ1
Y1 DXa
Z1 DXb
D XaXb | XaXb
Xa a
Xb b
A2 A3A1 | b
A3 A1A2 | a
A4 aA1A3A2A1A1A3A4| aA1A3A2A1A1A3
Example: E E + T | T,
A4 A2A1A3A4| A2A1A3
not in GNF)
(In GNF)
A4 bA3A4A2A1A1A3A4| bA3A4A2A1A1A3
A4 bA3A2A1A1A3A4| bA3A2A1A1A3
A4 aA1A3A4A2A1A1A3A4| aA1A3A4A2A1A1A3
Solution:
E E + T | T F |( E ) | a,
E E + T | T F |( E ) | a,
T T F | ( E ) | a,
F ( E ) | a,
E EAT | TBF |( EC | a,
T TBF |( EC | a,
F ( E ) | a,
A +,
B ,
C)
(In GNF)
A2 (In GNF)
A3 )
(In GNF)
(In GNF)
(In GNF)
A6 A5A2A4
(Not in GNF)
(In GNF)
A7 A2A4A7 | A2A4
A6 A5A2A4
(Not in GNF)
A6 (A6A3A8 |(A6A3
A8 A1A5A8 | A1A5
A6 aA8 | a
A6 (A6A3A2A4A8 |(A6A3A2A4
A6 (A6A3A7A2A4A8 |(A6A3A7A2A4
(S 0S1 | 01 } generates L.
Top of
Stack
Initial
Symbol
Z0
Stack
Removing Direction
Finite
State
Control
Storing Direction
Reading
Head
DPDA
NDPDA
(q1, aZ0 )
(q1, bZ0 )
(q1, aa )
(q1, )
(q1, )
(q1, bb )
(qf, a )
(q1, aZ0 )
(q1, aa )
(q2, a )
(q2, )
(qf, Z0 )
Solution:
Solution
( q0, a, Z0 )
( q1, a, a )
( q1, b, a )
( q2, b, a )
( q2, , Z0 )
(q1, aZ0 )
(q1, aa )
(q2, )
(q2, )
(qf, Z0 )
( q0, a, Z0 )
( q0, b, Z0 )
( q1, a, a )
( q1, b, a )
( q1, a, b )
( q1, b, b )
( q1, c, a )
( q1, c, b )
( q2, a, a )
( q2, b, b )
( q2, , Z0 )
(q1, aZ0 )
(q1, bZ0 )
(q1, aa )
(q1, ba )
(q1, ab )
(q1, bb )
(q2, a )
(q2, b )
(q2, )
(q2, )
(qf, Z0 )
( q0, a, Z0 )
( q1, a, a )
( q1, c, a )
( q2, b, a )
( q2, , Z0 )
(q1, aaZ0 )
(q1, aaa )
(q2, a )
(q2, )
(qf, Z0 )
( q0, a, Z0 )
( q1, a, a )
( q1, c, a )
( q2, b, a )
( q3, b, a )
( q2, , Z0 )
(q1, aZ0 )
(q1, aa )
(q2, a )
(q3, a )
(q2, )
(qf, Z0 )
Solution 2:
( q0, a, Z0 )
( q1, a, a )
( q1, b, a )
( q2, b, a )
( q2, a, a )
( q3, a, a )
( q3, , Z0 )
(q1, aZ0 )
(q1, aa )
(q2, a )
(q2, a )
(q3, )
(q3, )
(qf, Z0 )
(q1, aZ0 )
(q1, aa )
(q2, a )
(q3, a )
(q3, )
(qf, Z0 )
P3
Symbols
a
P1
P4
b
P2
P5
Bottom-up parsing: The basic idea of a bottomup parser is that we start with input string and
replace the terminal(s) by respective variable
such that these replacements lead to the starting
symbol of the grammar So, in each step, we
reduce the input string to reach the starting
symbol. This approach is reverse of the top-down
approach.
Bottom-up parser reads the input symbol from left
and uses rightmost derivation in reverse order like
predictive parsing, with most tables, have we use
stack to push symbols. If the first few symbol
(sentential form) at the top of the stack match the
RHS of some variable, then we pop out these
symbols from the stack and we push that variable
(left-hand-side of production on the stack). This is
called a reduction. another name for bottom-up
parsers is shift-reduce(SR) parsers.
Handles are the sequences (terminals and
variables or both) on the stack that match with
some RHS in the given grammar productions and
after replacement lead to the start symbol. there
are three actions in the bottom-up parsing.
1. Shift the current input(token) on the
stack and read the next token.
2. Reduce by some suitable LHS of
production rule.
3. Acceptance Final reduction, which
leads to starting symbol.
Context Free Grammar is G = {VN, T, P, S },
design the parsing table and parse the string
babbba and check L(G)?
P1: SbAB
Parsing Table
Variables
Symbols
P2: SaAB
b
P3: AaA
a
S
P2
P1
P4: BbB
A
P3
P5
P5: A b
B
P
P4
P6: B a
6
Suppose: = babbba L(G) ?
P1
P3
P5
P4
P4
S bAB
baAB
babB
babbB
P6babbba.
babbbB
transition function.
(q, , X ) (q, )
productions are:
P1 : S F + S
function.
P2 : S F S
(q, a, a ) (q, )
P3: S F
P4 : F a
Parsing Table
a
P3
P4
a+
+a
P1
P2
P4
P4
aa
++
given productions.
EE+T
ET
TTF
F
a
F
b
TF
F(E)
F x1
F x2
Solution:
Remove Left Recursion and Left Factoring.
P1
P4
P2
P
P3
S
F+S
a+S
a + F S 4 a + a S
P
a + a F 4 a + a a.
P1: E +TE
P5: T FT
P2: E TE
P3: E
P4: T FT
P6: T
P7: F ( E )
P8: F xN
P9: N 1
P10: N 2
T
F
Parsing Table:
V/T
E
E
T
T
F
N
P3
P6
x
P2
P5
P8
P9
P10
(
P2
P5
P7
P1
P4
Parser:
Rule
R0
Input
(q0, , Z0 )
R1
R2
R3
R4
R5
R6
R7
R8
R9
R10
(q, , E)
(q, , E)
(q, , E)
(q, , T)
(q, , T)
(q, , T)
(q, , F)
(q, , F)
(q, , N)
(q, , N)
R11
R12
R13
R14
R15
R16
R17
(q, 1, 1 )
(q, 2, 2 )
(q, (, ( )
(q, ), ) )
(q, x, x )
(q, +, + )
(q, , )
R18
(q, $, Z0)
output
(q, EZ0 )
Step
4.1
(q, TE )
(q, +TE )
(q, )
(q, FT )
(q, FT )
(q, )
(q, ( E ))
(q, xN)
(q, 1)
(q, 2)
4.2
4.2
4.2
4.2
4.2
4.2
4.2
4.2
4.2
4.2
(q, )
(q, )
(q, )
(q, )
(q, )
(q, )
(q, )
4.3
4.3
4.3
4.3
4.3
4.3
4.3
(qf, $Z0)
4.4
Unread Input
q0
q
q
q
q
q
q
q
q
q
x1 + (x2) $
x1 + (x2) $
x1 + (x2) $
x1 + (x2) $
x1 + (x2) $
1 + (x2) $
1 + (x2) $
+ (x2) $
+ (x2) $
+ (x2) $
PushDown
store
Z0
EZ0
TEZ0
FTEZ0
xNTEZ0
NTEZ0
1TEZ0
TEZ0
EZ0
+TEZ0
Rule
used
R0
R1
R4
R8
R15
R9
R11
R6
R2
q
q
q
q
q
q
q
q
q
q
q
q
q
q
q
qf
(x2) $
(x2) $
(x2) $
x2) $
x2) $
x2) $
x2) $
2) $
)$
)$
)$
)$
$
$
$
TEZ0
FTEZ0
(E)TEZ0
E)TEZ0
TE)TEZ0
FTE)TEZ0
xNTE)TEZ0
NTE)TEZ0
2TE)TEZ0
TE)TEZ0
E)TEZ0
)TEZ0
TEZ0
EZ0
Z0
$Z0
R16
R4
R7
R13
R1
R4
R8
R15
R10
R12
R6
R3
R14
R6
R3
R18
TTF
TF
F(E)
F x1
F x2
Solution:
Input
(q, a, Z0)
(q, a, )
Output
R
ul
e
(q,aZ0)
(q, a)
1
2
P3
P4
P5
P6
P7
P8
P9
P10
(q, ,
T+E)
(q, , T)
(q, ,
FT)
(q, , F)
(q, ,)E()
(q, , 1x)
(q, , 2x)
(q, , E)
(q, E)
(q, E)
(q, T)
3
3
(q, T)
(q, F)
(q, F)
(q, F)
(qf, )
3
3
3
3
4
EE+T
ET
TTF
TF
F(E)
F x1
F x2
Rest of
Input
x1 + ( x2)
1 + (x2)
+ (x2)
+( x2)
+ (x2)
+ (x2)
(x2)
x2)
2)
)
)
)
)
Stack
Z0
x Z0
1x Z0
F Z0
T Z0
E Z0
+E Z0
(+E Z0
x(+E Z0
2x(+E Z0
F(+E Z0
T(+E Z0
E(+E Z0
)E(+E Z0
F+E Z0
T+EZ0
EZ0
Z0
action
Shift
Shift
Reduce
Reduce
Reduce
Shift
Shift
Shift
Shift
Reduce
Reduce
Reduce
Shift
Reduce
Reduce
Reduce
Reduce
Produ
ction
P1
P2
P8
P6
P4
P2
P2
P2
P2
P9
P6
P4
P2
P7
P6
P3
P10
S BCD
B 0B1 |
D 1D0 |
C 1 | 1C
Turing Machine
1 2
Read/ Write
Head
Finite state
Control
n
Infinite Tape
Halting State(h)
Non-Halting State( non-final)
Loop (finite or infinite)
Crash and
Hang State (Undefined Transition)
Process
TM Always Halts
Process
a
q1, a, R
q1, a, R
q1, a, R
q1, a, R
___
b
q0, b, R
q2, b, R
q3, b, R
q0, b, R
___
___
___
___
qf, , L
___
Transition Graph:
b, b, R
q0
a, a, R
a, a, R
q1
a, a, R
, , L
b, b, R
b, b, R
q2
q3
q4
a, a, R
b, b, R
TM for L = (a + b )*abb
Problem: Design a TM for finding n mod 2.
Solution:
State
q0
q1
q2
q3
*qf
0
q0, 0 , R
q2, , L
q1, , L
__
__
q1, , L
qf, , L
q3, , R
qf, 0, L
__
, , L
0, , L
0, , L
, , L
, , R
, 0, L
to accept
__
__
1, 1, R
*q1
0
(q1, , R )
(q1, 0, R )
__
(q3, 0, L )
(q1, , R )
__
Input Symbol
1
__
__
(q1, 1, R ) (q2, , L )
(q3, , L )
(q3, 1, L ) (q4, , R )
__
(qf, , R )
__
__
Input Symbol
0
(q1, 0 , R)
__
(q0, 0 , R)
__
Input Symbol
0
(q1, 0 , R)
(q2, 0 , R)
(q0, 0 , R)
__
__
__
Current
State
q0
q1
q2
q3
q4
q5
q6
*qf
0
(q1, , R)
(q1, 0, R)
__
__
__
(q5, 0, L)
__
__
Input Symbol
1
__
(q1, 1, R)
(q3, , L)
(q4, , L)
(q5, 1, L)
(q5, 1, L)
__
__
__
(q2, , L)
__
__
(q6, , R)
(q0, , R)
(qf, , R)
__
0
(q1, , R)
(q1, 0, R)
(q2, 0, R)
(q4, , L)
(q4, 0, L)
(q5, 0, L)
(q1, , R)
__
Input Symbol
1
__
__
(q1, 1, R)
__
__
(q3, , L)
__
(qf, 0, L)
(q5, 1, L)
__
__
(q6, , R)
__
__
__
__
(q
,x,
R)
(q
,1,L
)
__
__
__
input string. If the last two symbols in the q0
1
9
q1
(q1,0,R) (q2,1,R)
__
__
__
string are 11, then the match occurs.
q
(q
,y,R)
(q
,1,L)
__
__
__
2
3
7
a. If the match occurs, then the r/w
q3
(q3,0,R) (q4,1,R)
__
__
__
head converts both 1 symbols to
q4
(q4,0,R)
__
__
__
(q5,0,L)
blanks one by one. The r/w head
traverses back to the beginning of the
q5
(q5,0,L) (q6,1,L)
__
__
__
remaining string and perform the
q
(q
,0,L)
__
__
(q
,y,R)
__
6
6
2
matching and cancellation of one 0 by
q7
__
(q8,1,L)
__
(q7,0,L)
__
11. This process is repeated till all 0s
q8
(q8,0,L)
__
(q0,x,R)
__
__
are matched by the corresponding 11
q9
__
__
(q9,0,L)
__
(qf. , R)
pair. If this occurs, then it indicates
the acceptance of the string.
b. If the match does not occur at any
*qf
__
__
__
__
__
stage, the process stops in a non-final
state and indicates the rejection of the
string.
The transition table is:
a
(q1, , R )
(q1, a, R )
(q3, , L )
(q3, a, L )
(q4, a, R )
__
__
Input Symbol
b
(q4, , R ) (qf, , R )
(q1, b, R ) (q2, , L )
(q3, b, L )
(q4, b, R )
(q3, , L )
__
(q0, , R )
(q5, , R )
__
__
0 0
0
(q0, 0, R )
(q1, 0, R )
(qf, , L )
__
0 0
0 0
Input Symbol
1
(q1, 0, R )
__
__
(q2, , R )
__
__
__
__
b
u1
b
u1
ba
u3
b3
v1
ba
v2
b3
v1
1
10
101
2
01
100
3
0
10
4
100
0
5
1
010
Solution:
10
101
1
1
010
5
01
100
2
0
10
3
100
0
4
100
0
4
0
10
3
100
0
4
a
v3
with || | |
or
cons a(x) = ax
cons b(x) = bx
Note: we note that cons a(x) and cons b(x)
simply denote the concatenation of the
constant string a and x, and concatenation of
the constat string b and x.
definition: If f1, f2, . . . , fn are partial
functions of n variables and g is a partial
function of k variables then the composition
of g with f1, f2, . . . , fk is a partial function of n
variables defined by g(f1(x1, x2, . . . ,xn),f2(x1,
x2, . . . ,xn), . . . , fk(x1, x2, . . . , xn)).
Example: let f1(x, y ) = x + y,
f2( x, y ) = 2x,
f3 ( x, y ) = xy and
g(x, y, z) = x + y + z be a function over N.
then g(f1(x, y ), f2(x, y ), f3(x, y) ) = g(x + y,
2x, xy ) = x + y + 2x + xy.
Example:
let f1 (x1, x2) = x1x2,
f2(x1, x2 ) =
f3 (x1, x2 ) = x1 and
g(x1, x2, x3 ) = x2x3 be the function over .
Example:
Define n! by recursion
Solution:
f(0) = 1 and f(n+1) = h(n, f(n))
where h(x,y) = S(x) y.
= f1( f2 ( x, y ), x)
solution:
we have defined concatenation as
f(, w2 ) = w2
f( a1, a2, . . . ,an, w2 ) = a1f1(a2, . . . ,an, w2 ) where
w1, w2 *.
we defined f using initial functions a follows:
f(, w1 ) = cons (w1) = w1 and