Formal Definition
Formal Definition
PART - A
UNIT – 1
7 Hours
Introduction to Finite Automata: Introduction to Finite
Automata; Thecentral concepts of Automata theory; Deterministic
finite automata; Nondeterministic finite automata
UNIT – 2
7 Hours
Finite Automata, Regular Expressions: An application of finite
automata;Finite automata with Epsilon-transitions; Regular expressions;
Finite Automata and Regular Expressions; Applications of Regular
Expressions
UNIT – 3
6 Hours
Regular Languages, Properties of Regular Languages:
Regularlanguages; Proving languages not to be regular languages;
Closure properties of regular languages; Decision properties of
regular languages; Equivalence and minimization of automata
UNIT – 4
6 Hours
Context-Free Grammars And Languages : Context –free grammars;
Parsetrees; Applications; Ambiguity in grammars and Languages .
PART – B
UNIT – 5
7 Hours
Pushdown Automata: Definition of the Pushdown automata; the
languagesof a PDA; Equivalence of PDA‟s and CFG‟s; Deterministic
Pushdown Automata
UNIT – 6
6 Hours
Properties of Context-Free Languages: Normal forms for CFGs;
Thepumping lemma for CFGs; Closure properties of CFLs
UNIT – 7
7 Hours
Introduction To Turing Machine: Problems that Computers cannot
solve;The turning machine; Programming techniques for Turning Machines;
Extensions to the basic Turning Machines; Turing Machine and Computers.
UNIT – 8
6 Hours
Undecidability: A that is not recursively enumerable;
AnUndecidable problem that is RE; Post‟s Correspondence
problem; Other undecidable problems.
1
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
Text Books:
1. John E. Hopcroft, Rajeev Motwani, Jeffrey D.Ullman:
Introduction to Automata Theory, Languages and Computation,
3rd Edition, Pearson Education, 2007.
(Chapters: 1.1, 1.5, 2.2 to 2.5, 3.1 to 3.3, 4, 5, 6, 7,
8.1 to 8.4, 8.6, 9.1, 9.2, 9.4.1, 9.5)
Reference Books:
1. K.L.P. Mishra: Theory of Computer Science, Automata,
Languages, and Computation, 3rd Edition, PHI, 2007.
2. Raymond Greenlaw, H.James Hoover: Fundamentals of the Theory
of Computation, Principles and Practice, Morgan Kaufmann, 1998.
2
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
3
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
4
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
5
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
Formal definition
Automaton
An automaton is represented formally by a 5-tuple(Q,Σ,δ,q0,F), where:
Run called an input word. The set of all words is denoted by Σ*. �
A run of the automaton on an input word w = a 1,a2,...., an Σ*, is a sequence of
states q0,q1,q2,...., qn, where qi state and q i = δ(q i-1 ,a )
�
Q s uch that q0 is the start� i
the automaton is at the s tart state q , and then the
Recognized
An automaton can recognize a formal language. The language L Σ* recognized
by an automaton is the set of all the words that are accepted by �
the automaton.
Recognizable languages
The recognizable languages are the set of languages that are
recognized by some automaton. For the above definition of automata the
recognizable languages are regular languages. For different definitions
of automata, the recognizable languages are different.
6
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
For example, the following questions are studied about a given type of automata.
Automata theory also studies if there exist any effective algorithm or not
to solve problems similar to the following list.
ω-limit languages ω-
regular languages
ω-regular languages
ω-regular languages
ω-regular languages
ω-regular languages
7
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
So, the DFA which accepts strings of a‟s and b‟s starting with the string ab is given by
Obtain a DFA to accept strings of a’s and b’s starting with the string ab
a,b
a b
q q q
b a
q
a,b
Fig.1.1 Transition diagram to accept string ab(a+b)*
M = (Q, , , q0, A) where
Q = {q0, q1, q2,
q3} = {a, b}
q0 is the start state
A = {q2}.
is shown the transition table 2.4.
a b
q0 q1 q3
q1 q3 q2
States
q2 q2 q2
q3 q3 q3
8
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
Draw a DFA to accept string of 0’s and 1’s ending with the string 011.
1 0
q0 q1 0 1 q2 1 q3
0
0
1
Obtain a DFA to accept strings of a’s and b’s having a sub string aa
b a,b
a a
q0 q1 q2
b
Obtain a DFA to accept strings of a’s and b’s except those containing the substring aab.
b a a,b
a b
q0 q1 a q2 q3
b
Obtain DFAs to accept strings of a’s and b’s having exactly one a,
b b a,b
a a
q0 q1 q2
b a, b
q0 a q1
b b b b a, b
a a a
q0 q1 q2 a q3 q4
9
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
Obtain a DFA to accept strings of a’s and b’s having even number of a’s and b’s
The machine to accept even number of a‟s and b‟s is shown in
fig.2.22. a
q q
a
b
b b a b
q q
a
Fig.2.22 DFA to accept even no. of a’s and b’s
a
q0 q1
bbbba
q2
q3a
q0 q1
b b b b
a
q2 q3
a
q0 q1
b b b b
a
q2 q3
Regular language
Definition: Let M = (Q, , , q0, A) be a DFA. The language L is regular if there exists a
machine M such that L = L(M).
10
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
11
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
n n
Obtain an NFA to accept the following language L = {w | w abab or aba where n 0}
n n
The machine to accept either abab or aba where n 0 is shown below:
b
a
q1 a q2 b q3 q4
q0
a
q5 a q6 b q7
Step1:
The start state of NFA MN is the start state of DFA MD. So, add q0(which is
the start state of NFA) to QD and find the transitions from this state. The
q0 0,1 q1 0, 1 q2
Step1: q0 is the start of DFA (see step1 in the conversion procedure).
Step2: Find the new states from each state in QD and obtain the corresponding transitions.
Since the states obtained in (2.8) and (2.9) are not in QD(2.7), add these two
states to QD so that
0 1
[q0] [q0, q1] [q1]
Q [q0, q1]
[q1]
0 1
[q0] [q0, q1] [q1]
[q0, q1] [q0, q1, q2] [q1, q2]
Q [q1]
[q0, q1,
q2]
[q1, q2]
D([q1], 0) = N([q1], 0)
= [q2]
(2.14)
When a = 1
D([q1], 1) = N([q1], 1)
=
14
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
[q2]
(2.15)
Since the states obtained in (2.14) and (2.15) are same and the state q 2 is not in
QD(see 2.13), add the state q2 to QD so that
QD = {[q0], [q0, q1], [q1], [q0, q1, q2], [q1, q2], [q2]} (2.16)
0 1
[q0] [q0, q1] [q1]
[q0, q1] [q0, q1, q2] [q1, q2]
Q [q1] [q2] [q2]
[q0, q1,
q2]
[q1, q2]
[q2]
When a = 0
D([q0,q1,q2], = N([q0,q1,q2], 0)
0) = N 0 , 0) U N 1, 0) U
(q (q
N(q2, 0)
= {q0,q1} U {q2} U { }
= [q0,q1,q2]
(2.17)
When a = 1
D([q0,q1,q2], = N([q0,q1,q2], 1)
1) = N 0 , 1) U N 1, 1) U
(q (q
N(q2, 1)
= {q1} U {q2} U {q2}
= [q1, q2]
(2.18)
Since the states obtained in (2.17) and (2.18) are not new states (are already in
QD, see 2.16), do not add these two states to QD. But, the transitions on a = 0
and a = 1 should be added to the transitional table as shown below:
0 1
[q0] [q0, q1] [q1]
[q0, q1] [q0, q1, q2] [q1, q2]
15
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
When a = 0
D([q1,q2], 0) = N([q1,q2], 0)
= N(q1, 0) U N(q2, 0)
= {q2} U { }
= [q2]
(2.19)
When a = 1
D([q1,q2], 1) = N([q1,q2], 1)
= N(q1, 1) U N(q2, 1)
= {q2} U {q2}
= [q2]
(2.20)
Since the states obtained in (2.19) and (2.20) are not new states (are already in
QD see 2.16), do not add these two states to Q D. But, the transitions on a = 0 and
a = 1 should be added to the transitional table as shown below:
0 1
[q0] [q0, q1] [q1]
[q0, q1] [q0, q1, q2] [q1, q2]
Q [q1] [q2] [q2]
[q0, q1, [q0,q1,q2] [q1, q2]
q2]
[q1, q2] [q2] [q2]
[q2]
When a = 0
D([q2], 0) = N([q2], 0)
= {}
(2.21)
When a = 1
D([q2], 1) = N([q2], 1)
= [q2]
16
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
(2.22)
Since the states obtained in (2.21) and (2.22) are not new states (are already in
QD, see 2.16), do not add these two states to QD. But, the transitions on a = 0
and a = 1 should be added to the transitional table. The final transitional table is
shown in table 2.14. and final DFA is shown in figure 2.35.
0 1
[q0] [q0, q1] [q1]
[q0, q1, q2] [q1, q2]
[q0,q1]
[q2] [q2]
[q1]
[q0,q1,q2] [q1, q2]
[q0,q1,q2]
[q1,q2] [q2] [q2]
[q2] [q2]
[q 0 ]
0 1
[q 0 , q 1 ] [q 1 ]
0 1 0, 1
1 0, 1
[q 0 , q 1 , q 2 ] [q 1 , q 2 ] [q 2 ]
0 1
17
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
a
4 5
a b
0 1 2 38 9 6b7
(A, a) = N(0, a)
= {1}
(B)
When input is b:
( A, b) = N(0, b)
= { }
Consider the state [B]:
When input is a:
= N(1, a)
= { }
(B, a)
= N(1, b)
When input is b: = {2} =
( B, b) {2,3,4,6,9}
(C)
This is because, in state 5 due to -transitions, the states reachable are {8, 9, 3,
4, 6}. Therefore,
(C, a) = {3, 4, 5, 6, 8, 9} = D
When input is b:
( C, b) = N({2, 3, 4, 6, 9}, b)
= {7}
= {7, 8, 9, 3, 4, 6}
= {3, 4, 6, 7, 8, 9}(ascending order)
(E)
This is because, from state 7 the states that are reachable without any input (i.e.,
-transition) are {8, 9, 3, 4, 6}. Therefore,
(C, b) = {3, 4, 6, 7, 8, 9} = E
Consider the state [D]:
When input is a:
(D, a) = N({3,4,5,6,8,9}, a)
= {5}
= {5, 8, 9, 3, 4, 6}
= {3, 4, 5, 6, 8, 9} (ascending
order) (D)
When input is b:
(D, b) = N({3,4,5,6,8,9}, b)
= {7}
= {7, 8, 9, 3, 4, 6}
= {3, 4, 6, 7, 8, 9} (ascending
order) (E)
(E, b) = N({3,4,6,7,8,9}, b)
= {7}
= {7, 8, 9, 3, 4, 6}
= {3, 4, 6, 7, 8, 9}(ascending order)
(E)
Since there are no new states, we can stop at this point and the transition table
for the DFA is shown in table 2.15.
19
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
a b
A B -
B - C
Q C D E
D D E
E D E
The states C,D and E are final states, since 9 (final state of NFA) is present in C,
D and E. The final transition diagram of DFA is shown in figure 2.36
a
a b a
A B C D
a
b
b
Eb
20
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
1. Obtain a DFA to accept strings of a‟s and b‟s starting with the string ab
2. Draw a DFA to accept string of 0‟s and 1‟s ending with the string 011.
3. Obtain a DFA to accept strings of a‟s and b‟s having a sub string aa
4. Obtain a DFA to accept strings of a‟s and b‟s except those containing the
substring aab.
5. Obtain DFAs to accept strings of a‟s and b‟s having exactly one a,
6. Obtain a DFA to accept strings of a‟s and b‟s having even number of a‟s and b‟s
δ a b
p {r} {q} {p,r}
q {p}
*r {p,q} {r} {p}
12. Convert above automaton to a DFA
13. Write a note on Application of automaton.
21
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
22
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
String Processing
Consider finding all occurrences of a short string (pattern string) within a
Long string (text string).This can be done by processing the text through
a DFA: the DFA for all strings that end with the pattern string. Each time the
accept state is reached, the current position in the text is output
Finite-State Machines
A finite-state machine is an FA together
with actions on the arcs.
State charts
State charts model tasks as a set of states and actions. They extend FA
.
diagrams Here is a simplified state chart for a stopwatch
Lexical Analysis
In compiling a program, the first step is lexi-cal analysis. This isolates
keywords,identifiersetc., while eliminating irrelevant symbols.A token is a
category, for example “identifier”,“relation operator” or specific keyword.
For example,
token RE keyword
then then
variable name [a-zA-Z][a-zA-Z0-9]* where latter RE says it is any
string of alphanumeric
characters starting with a letter.
A lexical analyzer takes source code as a string,and outputs sequence
of tokens. For example,
for i = 1 to max
do x[i] = 0;
might have token sequence
for id = num to id do id [ id ] = num sep
As a token is identified, there may be an action.
For example, when a number is identified, itsvalue is calculated
24
in multiple states at once. Whenever we take an edge, we must fork off a new
made NFA's more convenient to represent some problems than DFA's but
were not more powerful, the same applies to NFA's. While more expressive,
anything we can represent with an NFA we can represent with a DFA that has
no transitions.
Epsilon Closure
Epsilon Closure of a state is simply the set of all states we can reach by following
thetransition function from the given state that are labeled . Generally speaking, a
collection of objects is closed under some operation if applying that operation to
members of the collection
returns an object still in the collection.
In the above example:
(q) = { q }
(r) = { r, s}
let us define the extended transition function for an NFA.
For a regular, NFA we said for the induction step:
Let ^(q,w) = {p1, p2, ... pk}
Eliminating Transitions
Transitions are a convenience in some cases, but do not increase the power of the NFA.
To eliminate them we can convert a NFA into an equivalent DFA, which is quite similar
to the steps we took for converting a normal NFA to a DFA, except we must now
follow all Transitions and add those to our set of states.
1. Compute for the current state, resulting in a set of
states S. 2. (S,a) is computed for all a in by
a. Let S = {p1, p2, ... pk}
b. Compute I=1k (pi,a) and call this set {r1, r2, r3... rm}. This set is achieved by
following input a,
not by following any transitions
c. Add the transitions in by computing (S,a)= I=1m (r1)
3. Make a state an accepting state if it includes any final states in the -NFA.
Note :The ε (epsilon) transition refers to a transition from one state to
anotherwithout the reading of an input
symbol (ie without the tape containing the input string moving).
Epsilon transitions can be inserted between
25
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
any states. There is also a conversion algorithm from a NFA with epsilon
transitions to a NFA without
epsilon transitions.
a b C
q0 {q0} {q1}
q1 {q2} {q2} Consider the NFA-epsilon move machine M = { Q, ,
q2 {q2} , q0, F}
Q = { q0, q1, q2 }
= { a, b, c } and moves
q0 = q0
F = { q2 }
The language acc epted by the above N FA wi th epsilon moves is the set of s trings over { a,b,c} i ncl udi ng the null stri ng and
all strings with any number of a's followed by any number
of b's followed by any number of c's.
Now convert the NFA with epsilon moves to a NFA M = ( Q', , ', q0', F')
First determine the states of the new machine, Q' = the epsilon closure
of the states in the NFA with epsilon moves. There will be the same number of
states but the names can be constructed by writing the state name as
the set of states in the epsilon closure. The epsilon closure is the
initial state and all states that can be reached by one or more epsilon moves. Thus
q0 in the NFA-epsilon becomes {q0,q1,q2} because the machine can move from q0
to q1 by an epsilon move, then check q1 and find that it can move
from q1 to q2 by an epsilon move.
26
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
q2 in the NFA-epsilon becomes {q2} just to keep the notation the same.
q2 can go nowhere except q2, that is what phi means, on an epsilon
move. We do not show the epsilon transition of a state to itself here, but,
beware, we will take into account the state to itself epsilon
transition when converting NFA's to regular expressions.
The initial state of our new machine is {q0,q1,q2} the epsilon closure of q0
The final state(s) of our new machine is the new state(s) that
contain a state symbol that was a final state in the original machine.
The new machine accepts the same language as the old machine, thus same sigma.
inputs
a b c
qx or{q0,q1,q2}
qy or{q1,q2}
qz or{q2}
Now we fill in the transitions. Remember that a NFA has transition entries that are sets.
Further, the names in the transition entry sets must be only the state names from Q'.
Very carefully consider each old machine transitions in the first row.
You can ignore any entries and ignore the column.
In the old machine (q0,a)=q0 thus in the new machine
'({q0,q1,q2},a)={q0,q1,q2} this is just because the new machine
accepts the same language as the old machine and must at least have the
the same transitions for the new state names.
inputs
a b c
qx or{q0,q1,q2} {qx} or{{q0,q1,q2}}
qy or{q1,q2}
qz or{q2}
27
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
machine was because out new state has symbols q0, q1 and q2 in the
new state name from the epsilon closure. Since q1 is in {q0,q1,q2} and
(q1,b)=q1 then '({q0,q1,q2},b)={q1,q2}. WHY {q1,q2} ?, because {q1,q2} is the new machines name for the old machines name q1. Just
Now, because our new qx state has a symbol q2 in its name and
compare the zeroth column of to '. So we
have inputs
a b c
qx or{q0,q1,q2} {qx} or{{q0,q1,q2}} {qy} or{{q1,q2}}
qy or{q1,q2}
qz or{q2}
(q2,c)=q2 is in the old machine, the new name for the old q2,
which is qz or {q2} is put into the input c transition in row 1.
Inputs
a b c
qx or{q0,q1,q2} {qx} or{{q0,q1,q2}} {qy} or{{q1,q2}} {qz} or{{q2}}
qy or{q1,q2}
qz or{q2}
Now, tediously, move on to row two, ... .
You are considering all transitions in the old machine, delta,
for all old machine state symbols in the name of the new machines states.
Fine the old machine state that results from an input and translate
the old machine state to the corresponding new machine state name
and put the new machine state name in the set in delta'. Below are the
"long new state names" and the renamed state names in delta'.
Inputs
a b c
qx or{q0,q1,q2} {qx} or{{q0,q1,q2}} {qy} or{{q1,q2}} {qz} or{{q2}}
qy or{q1,q2} {qy} or{{q1,q2}} {qz} or{{q2}}
qz or{q2} {qz} or{{q2}}
inputs
a b c \
qx {qx} {qy} {qz} \ Q
qy {qy} {qz} /
qz {qz} /
28
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
The figure above labeled NFA shows this state transition table.
The table 3.1 shows some examples of regular expressions and the language
corresponding to these regular expressions.
Regular Meaning
expressions
(a+b)* Set of strings of a‟s and b‟s of any length
including the NULL string.
(a+b)*abb Set of strings of a‟s and b‟s ending with the
string abb
ab(a+b)* Set of strings of a‟s and b‟s starting with the
string ab.
(a+b)*aa(a+b) Set of strings of a‟s and b‟s having a sub string
* aa.
a*b*c* Set of string consisting of any number of
a‟s(may be empty string also) followed by any
number of b‟s(may include empty string)
followed by any number of c‟s(may include
empty string).
+ + +
a b c Set of string consisting of at least one „a‟
followed by string consisting of at least one „b‟
followed by string consisting of at least one „c‟.
aa*bb*cc* Set of string consisting of at least one „a‟
29
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
String of a‟s and b‟s of even length can be obtained by the combination of the strings aa,
ab, ba and bb. The language may even consist of an empty string denoted by .
So, the regular expression can be of the form
(aa + ab + ba + bb)*
30
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
Proof: By definition, , and a are regular expressions. So, the corresponding machines to
recognize these expressions are shown in figure 3.1.a, 3.1.b and 3.1.c respectively.
a
q0 qf q0 qf q0 qf
(a) (b) (c)
Fig 3.1 NFAs to accept , and a
In the definition of a regular expression it is clear that if R and S are regular expression,
then R+S and R.S and R* are regular expressions which clearly uses three operators „+‟, „-„
and „.‟. Let us take each case separately and construct equivalent machine. Let M1 =
(Q1, 1 , 1, q1, f1) be a machine which accepts the language L(R1) corresponding
to the regular expression R1. Let M2 = (Q2, 2, 2, q2, f2) be a machine which
accepts the language L(R2) corresponding to the regular expression R2.
Case 1: R = R1+ R2. We can construct an NFA which accepts either L(R1)
or L(R2)which can be represented as L(R1 + R2) as shown in figure 3.3.
L(R1)
q1 M1
q0 qf
q2 M2
L(R2)
31
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
L(R1) L(R2)
M1
It is clear from figure 3.4 that the machine after accepting L(R1) moves from state q1 to
It is clear from figure 3.3 that the machine can either accept L(R1) or L(R2). Here, q0 is
the start state of the combined machine and qf is the final state of combined machine M.
q1 q2 M2
f1. Since there is a -transition, without any input there will be a transition from state f 1
to state q2. In state q2, upon accepting L(R2), the machine moves to f2 which is the
final state. Thus, q1 which is the start state of machine M1 becomes the start state of
the combined machine M and f2 which is the final state of machine M2, becomes the
final state of machine M and accepts the language L(R1.R2).
* *
Case 3: R = (R1) . We can construct an NFA which accepts either L(R1) ) as
shown infigure 3.5.a. It can also be represented as shown in figure 3.5.b.
q0 q1 M1 qf
(a)
q0 q1 M1 qf
(b)
*
Fig. 3.5 To accept the language L(R1)
It is clear from figure 3.5 that the machine can either accept or any number of L(R1)s
*
thus accepting the language L(R1) . Here, q0 is the start state qf is the final state.
Obtain an NFA which accepts strings of a‟s and b‟s starting with the string ab.
32
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
4 a 5
6 b 7
Step 3: The machine to accept (a + b) is shown below.
a
4 5
3 8
6 7
b
Step 4: The machine to accept (a+b)* is shown below.
a
4 5
2 3 8 9
6 7
b
a b
0 1 2
4 a 5
0 a 1 b 2 3 8 9
6 b 7
33
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
r1 r r
q0 q1
r
where r1, r2, r3 and r4 are the regular expressions and correspond to the labels for
the edges. The regular expression for this can take the form:
* * *
r = r1 r2 (r4 + r3r1 r2) (3.1)
Note:
1. Any graph can be reduced to the graph shown in figure 3.9. Then substitute the
regular expressions appropriately in the equation 3.1 and obtain the final regular
expression.
2. If r3 is not there in figure 3.9, the regular expression can be of the form
* *
r = r1 r2 r4 (3.2)
3. If q0 and q1 are the final states then the regular expression can be of the form
* *
r = r1* + r1 r2 r4 (3.3)
0 1 1 0
q2 q3 0,1
1
34
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
It is clear from this figure that the machine accepts strings of 01‟s and 10‟s of any
length and the regular expression can be of the form
*
(01 + 10)
0 1 0,
0
q0 q1 q2
1
Since, state q2 is the dead state, it can be removed and the following FA is obtained.
0 1
q01 q1
The state q0 is the final state and at this point it can accept any number of 0‟s
which can be represented using notation as
*
0
q1 is also the final state. So, to reach q1 one can input any number of 0‟s followed by 1 and followed by any number of 1‟s and can be represented as
It is clear from the regular expression that language consists of any number of
/acb*c/
35
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
then the editor searches for a string which starts with ac followed by zero or more b‟s
and followed by the symbol c. Note that the editor ed accepts the regular expression
and searches for that particular pattern in the text. As the input can vary dynamically,
it is challenging to write programs for string patters of these kinds.
36
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
Assignment questions:
n n
1. Obtain an NFA to accept the following language L = {w | w abab or aba where n 0}
2. Convert the following NFA into an equivalent DFA.
0 1
q0 0,1 q1 0, 1 q2
3. Convert the following NFA to its equivalent DFA.
a
4 5
a b
0 1 2 8 9
7
6 b
4. P.T. Let R be a regular expression. Then there exists a finite automaton M = (Q,
, , q0, A) which accepts L(R).
5. Obtain an NFA which accepts strings of a‟s and b‟s starting with the string ab.
37
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
38
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
3.1:Regular languages
In theoretical computer science and formal language theory, a regular language is a
formal language that can be expressed using a regular expression. Note that the
"regular expression" features provided with many programming languages are
augmented with features that make them capable of recognizing languages that can
not be expressed by the formal regular expressions (as formally defined below).
Formal definition
The collection of regular languages over an alphabet Σ is defined recursively as follows:
If A and B
�
A* (Kleene star) are regular languages.
No other languages over Σ are regular.
See regular expression for its syntax and semantics. Note that the above
cases are in effect the defining rules of regular expression
Examples
All finite languages are regular; in particular the empty string language {ε} = Ø*
is regular. Other typical examples include the language consisting of all strings
over the alphabet {a, b} which contain an even number of as, or the language
consisting of all strings of the form: several as followed by several bs.
• Pumping Lemma
n n
Used to prove certain languages like L = {0 1 | n ≥ 1} are not regular.
• Closure properties of regular languages
Used to build recognizers for languages that are constructed from other
languages by certain operations.
Ex. Automata for intersection of two regular languages
39
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
0 0 1 1
1 2 3 4 5
1 0
0
1
6
0,1
0,1
State 6 is a trap state, state 3 remembers that two 0‟s have come and from there
state 5 remembers that two 1‟s are accepted.
This implies DFA has no memory to remember arbitrary „n‟. In other words if we have to
remember n, which varies from 1 to we have to have infinite states, which is not
possible with a finite state machine, which has finite number of states.
|w| = n ≥ n. Let the start state be P1. Let w = xyz where x= a1,a2 ,a3 -----an-1 , y=an and z =
.
k k
Therefore xy z = a1 ------ an-1 (an)
k=0 a1 ------ an-1 is accepted
k=1 a1 ------ an is accepted
k=2 a1 ------ an+1 is accepted
k=10 a1 ------ an+9 is accepted and so on.
that, certain languages are not regular. It should never be used to show that some
language is regular. If you want to show that language is regular, write separate
41
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
Proof:
Let L be regular. Let n is the constant (PL Definition). Consider a word w in
n n
L. Let w = a b , such that |w|=2n. Since 2n > n and L is regular it must satisfy PL.
k
from the definition of PL , w=xy z, where k=0,1,2,------ , should belong
n-l lk n
to L. That is a (a ) b L, for all k=0,1,2,------
n-l n
Put k=0. we get a b L.
Contradiction. Hence the Language is not
regular. Example 2.
To prove that L={w|w is a palindrome on {a,b}*} is not regular. i.e.,
L={aabaa, aba, abbbba,…}
Proof:
Let L be regular. Let n is the constant (PL Definition). Consider a word w in L.
n n, Since 2n+1 > n and L is regular it must satisfy PL.
Let w = a ba such that |w|=2n+1.
k
from the definition of PL w=xy z, where k=0,1,2,------ , should belong to L.
n-l l
That is a (a )k ban L, for all k=0,1,2,------ .
42
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
Put k=0. we get an-l b an L, because, it is not a palindrome. Contradiction, hence the
language is not regular
.
Example 3.
To prove that L={ all strings of 1‟s whose length is prime} is not regular. i.e.,
2 3 5 7 11
L={1 , 1 ,1 ,1 ,1 ,----}
(p-m) ≥ 2 since m ≤n
Example 4.
i2 2 4
To prove that L={ 0 | i is integer and i >0} is not regular. i.e., L={0 , 0
9 16 25
,0 ,0 ,0 ,----}
Proof: Let L be regular. Let w = 0n2where |w| =
2 k
n ≥ nby PL xy z L, for all k = 0,1,---
Select k = 2
2
| xy z |= | xyz | + | y |
2
= n + Min 1 and Max n
2 2 2
Therefore n < | xy z | ≤ n + n
2 2 2
n < | xy z | < n + n + 1+n adding 1 + n ( Note that less than or equal
2 2 2 to replaced by less than sign)
is n < | xy z | < (n + 1)
Say n = 5 this implies that string can have length > 25
i2
and < 36 which is not of the form 0 .
a) Show that following languages are not regular
43
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
44
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
Consider a DFA, A that accepts all and only the strings of 0‟s and 1‟s
*
that end in 01. That is L(A) = (0+1) 01. The complement of L(A) is therefore all
string of 0‟s and 1‟s that do not end in 01
45
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
46
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
seen neither 0 nor 1. Then state qr means that we have seen only once 0‟s, while
state ps represents the condition that we have seen only 1‟s. The accepting state
qs represents the condition where we have seen both 0‟s and 1‟s.
Ex 4 (on intersection)
Write a DFA to accept the intersection of L1=(a+b)*a and L2=(a+b)*b that is for L1
L2.
DFA for L1 L2
R R R R R
Where Q = Q, = , q0 =F,F =q0, and (p,a)-> q, iff (q,a) -> p
R
The FA for L can be derived from FA for L by swapping initial and final states
and changing the direction of each edge. It is shown in the following figure.
49
FORMAL LANGUAGES AND AUTOMATA THEORY
Homomorphism
Resulting :
h1(L) = (01 + 11)* 01 (01 + 11)* h2(L)
= (101 + 010)* 101 (101 + 010)* h3(L)
= (01 + 101)* 01 (01 + 101)*
Inverse Homomorphism
Theorem : If h is a homomorphism from alphabet S to alphabet T, and L is a
-1 (
regular language over T, then h L) is also a regular language.
*
Ex.Let L be the language of regular expression (00+1) .
-1
Let h be the homomorphism defined by h(a)=01 and h(b)=10. Then h (L) is the
*
language of regular expression (ba) .
50
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
1. R = R1 + R2. Then L(R) is empty if and only if both L(R1) and L(R2) are
empty.
2. R= R1R2. Then L(R) is empty if and only if either L(R1) or L(R2) is
empty. 3. R=R1* Then L(R) is not empty. It always includes at least
4. R=(R1) Then L(R) is empty if and only if L(R1) is empty since they
are the same language.
51
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
List all unordered pair of states (p,q) for which p ≠ q. Make a sequence of passes
through these pairs. On first pass, mark each pair of which exactly one element is in F.
On each subsequent pass, mark any pair (r,s) if there is an a for which (r,a)
= p, (s,a) = q, and (p,q) is already marked. After a pass in which no new
pairs are marked, stop. The marked pair (p,q) are distinguishable.
2 4 6
1. Let L = { , a , a , a , ….} be a regular language over = {a,b}. The
FA is shown in Fig 1.
Fig 2. gives the list of all unordered pairs of states (p,q) with p ≠ q.
52
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
The boxes (1,2) and (2,3) are marked in the first pass according to the algorithm 1.
In pass 2 no boxes are marked because, (1,a) and (3,a) 2. That is (1,3) ( ,2),
where and 3 are non final states.
�(1,b) and � (3,b) . That is (1,3) ( , ), where is a non-final state. This
implies that (1,3) are equivalent and can replaced by a single state A.
Consider set {1,3}. (1,3) (2,2) and (1,3) ( , ). This implies state 1 and 3 are
equivalent and can not be divided further. This gives us two states 2,A. The
resultant FA is shown is Fig 3.
Example 2. (Method1):
Let r= (0+1)*10, then L(r) = {10,010,00010,110, ---}. The FA is given below
53
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
The pairs marked 1 are those of which exactly one element is in F; They are marked
on pass 1. The pairs marked 2 are those marked on the second pass. For example
(5,2) is one of these, since (5,2) (6,4), and the pair (6,4) was marked on pass 1.
From this we can make out that 1, 2, and 4 can be replaced by a single
state 124 and states 3, 5, and 7 can be replaced by the single state 357. The
resultant minimal FA is shown in Fig. 6
Example 2. (Method1):
(2,3) (4,6) this implies that 2 and 3 belongs to different group hence they are split in
level 2. similarly it can be easily shown for the pairs (4,5) (1,7) and (2,5) and so on.
54
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
Assignment questions
0 1 1 0
q2 q3 0,1
1
0 1 0,1
0
q0 q1 q2
1
5. Obtain a DFA to accept strings of a‟s and b‟s starting with the string ab
a,b
a b
q0 q1 q2
b aq 3
a,b
8. prove that L={ all strings of 1‟s whose length is prime} is not regular. i.e.,
2 3 5 7 11
L={1 , 1 ,1 ,1 ,1 ,----}
9. Show that following languages are not regular
55
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
n m
L={a b | n, m 0 and n<m }
n m
L={a b | n, m 0 and n>m }
n m m n
L={a b c d | n, m 1 }
n
L={a | n is a perfect square }
n
L={a | n is a perfect cube }
10. Apply pumping lemma to following languages and understand why we cannot
complete proof
n
L={a aba | n 0 }
n m
L={a b | n, m 0 }
*
12. P.T. If L is a regular language over alphabet S, then L = - L is also a regular
language.
*
13. P.T. - If L is a regular language over alphabet , then, L = - L is also a
regular language
14. Write a DFA to accept the intersection of L1=(a+b)*a and L2=(a+b)*b that is for
L1 L2.
15. Find the DFA to accept the intersection of L1=(a+b)*ab (a+b)* and L2=(a+b)*ba
(a+b)* that is for L1 L2
16. P.T. If L and M are regular languages, then so is L – M.
R
17. P.T. If L is a regular language, so is L
18. If L is a regular language over alphabet , and h is a homomorphism on , then h
(L) is also regular.
19. If h is a homomorphism from alphabet S to alphabet T, and L is a regular
-1 (
language over T, then h L) is also a regular language.
20. Design context-free grammar for the following cases
a) L={ 0n1n | n≥l }
b) L={aibjck| i≠j or j≠k}
21. Generate grammar for RE 0*1(0+1)*
56
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
57
4.1: Context free grammar
S -> SS | (S) | ()
E E * E I * E a * E
a * (E) a * (E + E) a * (I + E) a * (a + E)
a * (a + I) a * (a + I0) a * (a + I00) a * (a + b00)
59
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
There is a rightmost derivation that uses the same replacements for each variable,
although it makes the replacements in different order. This rightmost derivation is:
E E * E E * (E) E * (E + E)
E * (E + I) E * (E + I0) E * (E + I00) E * (E + b00)
E * (I + b00) E * (a + b00) I * (a + b00) a * (a + b00)
This derivation allows us to conclude E a * (a + b00)
Leftmost Derivation
ETT*FF*F (E)*F(E+T)*F(T+T)*F(F+T)*F (a+T)*F
(a+F)*F (a+b)*F(a+b)*c
Rightmost derivation
ETT*FT*cF*c(E)*c(E+T)*c(E+F)*c
(E+b)*c(T+b)*c(F+b)*c(a+b)*c
Example 2:
Consider the Grammar for string (a,a)
S->(L)|a
L->L,S|S
Leftmost derivation
S(L)(L,S)(S,S)(a,S)(a,a)
Rightmost Derivation
S(L)(L,S)(L,a)(S,a)(a,a)
60
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
Sentential Forms
Derivations from the start symbol produce strings that have a special role called
“sentential forms”. That is if G = (V, T, P, S) is a CFG, then any string in (V T)*
such that S is a sentential form. If S , then is a left – sentential form, and if
S , then is a right – sentential form. Note that the language L(G) is those
sentential forms that are in T*; that is they consist solely of terminals.
E E+E | E*E |
(E)|id %{ #include
<stdio.h> %}
%token ID id
%%
Exp : id { $$ = $1 ; printf ("result is %d\n", $1);}
| Exp „+‟ Exp {$$ = $1 + $3;}
| Exp „*‟ Exp {$$ = $1 * $3; }
| „(„ Exp „)‟ {$$ = $2; }
;
%%
61
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
Example 2:
%{
#include <stdio.h>
%}
%start line
%token <a_number> number
%type <a_number> exp term factor
%%
line : exp ';' {printf ("result is %d\n", $1);}
;
exp : term {$$ = $1;}
| exp '+' term {$$ = $1 + $3;}
| exp '-' term {$$ = $1 - $3;}
term : factor {$$ = $1;}
| term '*' factor {$$ = $1 * $3;}
| term '/' factor {$$ = $1 / $3;}
;
factor : number {$$ = $1;}
| '(' exp ')' {$$ = $2;}
;
%%
int main (void) {
return yyparse ( );
}
void yyerror (char *s) {
fprintf (stderr, "%s\n", s);
}
%{
#include "y.tab.h"
%}
%%
62
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
Markup Languages
Functions
•Creating links between documents
•Describing the format of the document
Example
The Things I hate
1. Moldy bread
2. People who drive too
slow In the fast lane
HTML Source
HTML Grammar
•Char a|A|…
•Text e | Char Text
•Doc e | Element Doc
•Element Text |
<EM> Doc </EM>|
<p> Doc |
<OL> List </OL>| …
5. List-Item <LI> Doc
6. List e | List-Item List Start symbol
63
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
BE1
CE2
2. AE1 | E2.
AE1
AE2
3. A(E1)*
ABA
A
BE1
4. A(E1)+
ABA
AB
BE1
5. A(E1)?
A
AE1
4.4:Ambiguity
A context – free grammar G is said to be ambiguous if there exists some w L(G) which
has at least two distinct derivation trees. Alternatively, ambiguity implies the
existence of two or more left most or rightmost derivations.
Ex:-
Consider the grammar G=(V,T,E,P) with V={E,I}, T={a,b,c,+,*,(,)}, and
productions. EI,
EE+E,
EE*E,
E(E),
Ia|b|c
Consider two derivation trees for a + b * c.
64
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
Inherent Ambiguity
65
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
ASSIGNMENT QUESTIONS
4) Suppose h is the homomorphism from the alphabet {0,1,2} to the alphabet { a,b}
defined by h(0) = a; h(1) = ab &
h(2) = ba
a) What is h(0120) ?
b) What is h(21120) ?
c) If L is the language L(01*2), what is h(L) ?
d) If L is the language L(0+12), what is h(L) ?
e) If L is the language L(a(ba)*) , what is h-1(L) ?
66
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
67
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
68
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
That is the PDA accepts the word if there is any sequence of IDs starting from
That is the PDA accepts the word if there is any sequence of IDs starting from
We can always turn a PDA which use one acceptance method into one which uses
the other. Hence, both acceptance criteria specify the same class of languages.
70
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
The aim is to prove that the following three classes of languages are same:
PDA by
CFG PDA by
empty stack
Final state
Given a CFG G, we construct a PDA P that simulates the leftmost derivations of G. The
stack symbols of the new PDA contain all the terminal and non-terminals of the CFG.
There is only one state in the new PDA; all the rest of the information is encoded in the
stack. Most transitions are on �, one for each production. New transitions are added,
each one corresponding to terminals of G. For every intermediate sentential form uA� in
the leftmost derivation of w (initially w = uv for some v), M will have A� on its stack after
reading u. At the end (case u = w) the stack will be empty.
Let G = (V, T, Q, S) be a CFG. The PDA which accepts L(G) by empty stack is given by:
71
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
Example:
Convert the grammar with following production to PDA accepted by empty stack:
S � 0S1 | A
A � 1A0 | S | �
Solution:
Example:
Construct PDA to accept if-else of a C program and convert it to CFG. (This does
not accept if –if –else-else statements).
Let the PDA P = ({q}, {i, e}, {X,Z}, δ, q, Z), where δ is given by:
S � [qZq]
[qZq] � i[qXq][qZq]
72
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
[qXq] � e
[qZq] ��
If [qZq] is renamed to A and [qXq] is renamed to B, then the CFG can be defined by:
Example:
Convert PDA to CFG. PDA is given by P = ({p,q}, {0,1}, {X,Z}, δ, q, Z)), Transition
Solution:
73
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
5.4:Deterministic PDA
NPDA provides non-determinism to PDA. Deterministic PDA‟s (DPDA) are very useful
for use in programming languages. For example Parsers used in YACC are DPDA‟s.
Definition:
DPDA is less powerful than nPDA. The Context Free Languages could be recognized by
nPDA. The class of language DPDA accept is in between than of Regular language and
CFL. NPDA can be constructed for accepting language of palindromes, but not by
DPDA.
74
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
Example:
R
Construct DPDA which accepts the language L = {wcw | w � {a, b}*, c � Σ}.
0,0/
0, Z0/0Z0 1,1/
1, Z0/1Z0
0,0/00
1,1/11 c,0/0
0,1/ 01 c,1/1
c, Z / Z
1,0/ 10 q0 0 0
q1 q2
, Z0 / Z0
R}
Figure 2: DPDA L = {wcw
If L is a regular language, L=L(P) for some DPDA P. PDA surely includes a stack, but
the DPDA used to simulate a regular language does not use the stack. The stack is
inactive always. If A is the FA for accepting the language L, then δP(q,a,Z)={(p,Z)} for
all p, q � Q such that δA(q,a)=p.
Every regular language is not N(P) for some DPDA P. A language L = N(P) for some
DPDA P if and only if L has prefix property. Definition of prefix property of L states that if
x, y � L, then x should not be a prefix of y, or vice versa. Non-Regular language
R
L=WcW Rcould be accepted by DPDA with empty stack, because if you take any x, y�
L(WcW ), x and y satisfy the prefix property. But the language, L={0*} could be
accepted by DPDA with final state, but not with empty stack, because strings of
this language do not satisfy the prefix property. So N(P) are properly included in
CFL L, ie. N(P) � L
75
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
If L = L(P) for DPDA P, then L has unambiguous CFG. To convert L(P) to N(P) to have
prefix property by adding an end marker $ to strings of L. Then convert N(P) to CFG G‟.
From G‟ we have to construct G to accept L by getting rid of $ .So add a new production
$�� as a variable of G.
76
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
ASSIGNMENT QUESTIONS
1. A � aAA, A � aS | bS | a
2. S � SS | (S) | �
3. S � aAS | bAB | aB, A � bBB | aS | a, B � bA | a
77
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
78
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
The goal is to take an arbitrary Context Free Grammar G = (V, T, P, S) and perform
transformations on the grammar that preserve the language generated by the grammar
but reach a specific format for the productions. A CFG can be simplified by eliminating
How to simplify?
• Simplify CFG by eliminating
– Useless symbols
• Those variables or terminals that do not appear in any derivation of a
terminal string starting from Start variable
– �- productions
• A ��, where A is a variable
– Unit production
• A �B, A and B are variables
• Sequence to be followed
1. Eliminate �- productions from G and obtain G1
2. Eliminate unit productions from G1 and obtain G2
3. Eliminate useless symbols from G2and obtain G3
• Example
79
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
• Theorem:
• Example : S �AB|a, A �a
– 1 followed by 2 gives S �a
– 2 followed by 1 gives S �a, A �a
• A is still useless
• Not completely all useless symbols eliminated
• Eliminate non generating symbols
– Every symbol of T is generating
– If A ��and �is already generating, then A is generating
• Example
C D
80
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
C �xDy
– If no edge reaching a variable X from
Start symbol, X is non reachable
• Example
1. G= ({S,A}, {a}, {S �a, A �a},S)
S A
• Example
– S �AB | CA, B �BC|AB, A �a, C �AB|b
1. Eliminate non generating symbols V1 = {A,C,S} P1 = {S �CA, A �a, C �b }
2. Eliminate symbols that are non reachable
V2 = {A,C,S}
P2 = {S �CA, A �a, C �b
Exercises
• Eliminate useless symbols from the grammar
1. P= {S �aAa, A �Sb|bCC, C �abb, E �aC}
2. P= {S �aBa|BC, A �aC|BCC,C �a, B �bcc, D �E, E �d }
3. P= {S �aAa, A �bBB, B �ab, C �aB }
4. P= {S �aS|AB, A �bA,B�AA }
Eliminate �- productions
• Most theorems and methods about grammars G assume L(G) does not contain �
81
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
N0 {A | A in V, production A
�} repeat
Ni Ni-1U{A| A in V, A , in Ni-1}
until Ni = Ni-1
• Step 2:For each production of the form A�w, create all possible productions of
theform A �w‟, where w‟ is obtained from w by removing one or more occurrences
of nullable variables
• Example:
S ABA | BA | AA | AB | A | B | �
A aA ||
aB bB | �| b
• Step 3: The desired grammar consists of the original productions together with
the productions constructed in step 2, minus any productions of the form A ��
• Example:
S ABA | BA | AA | AB | A | B
A aA | a
B bB | b
PROBLEM:
82
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
into derivations that technically need not be there. The algorithm for eliminating unit
• Algorithm
1. Add all non unit productions to P1
2. For each unit production A �B, add to P1 all p roductions A �, where B �is a non-
unit production in P.
3. Delete all the unit productions
B bB | b
– Remove unit productions
S ABA | BA | AA | AB | aA | a | bB
| b A aA | a
B bB | b
Example (2): S Aa | B, A a | bc | B, B A |
bb Solution – Unit productions are
S B, A B, B A, A and B are derivable
– Add productions from derivable and eliminate unit
productions S bb | a | bc
A a| bc | bb
B bb | a | bc
S A B D
84
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
Simplified Grammar:
If you have to get a grammar without � - productions, useless symbols and unit
productions, follow the sequence given below:
85
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
Exercises:
• Convert to CNF:
1. S -->aSa|bSb|a|b|aa|bb
2. S -->bA|aB, A -->bAA|aS|a, B -->aBB|bS|b
3. S-->Aba, A -->aab, B -->AC
4. S -->0A0|1B1|BB, A -->C, B -->S|A, C -->S| �
5. S -->aAa|bBb| �, A -->C|a, B -->C|b, C -->CDE|�, D -->A|B|ab
The pumping lemma for CFL’s states that there are always two short sub-strings close
together that can be repeated, both the same number of times, as often as we like.
86
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
.
is given in figure 1 Both leftmost derivation and rightmost derivation have same
parse tree because the grammar is unambiguous.
4 4
Figure : Parse tree for a b
Extend the tree by duplicating the terminals generated at each level on all lower levels.
4 4
The extended parse tree for the string a b
is given in figure 2. Number of symbols at each level is at most twice of previous level. 1
i n
symbols at level 0, 2 symbols at 1, 4 symbols at 2 …2 symbols at level i. To have 2
symbols at bottom level, tree must be having at least depth of n and level of at least n+1.
87
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
of same variable along the path. Say X has 2 occurrences. Break z into uvwxy
such that w is the string of terminals generated at the lower occurrence of X and
vwx is the string generated by upper occurrence of X.
For the above example S has repeated occurrences, and the parse tree is shown in
figure 3. w = ab is the string generated by lower occurrence of S and vwx = aabb is
the string generated by upper occurrence of S. So here u=aa, v=a, w=ab, x=b, y=bb.
4 4
Figure 3: Parse tree for a b
Figure 4: sub- trees
with repeated occurrences of S
Let T be the subtree rooted at upper occurrence of S and t be subtree rooted at lower
2 2
occurrence of S. These parse trees are shown in figure 4. To get uv wx y �L, cut out t
2 2
and replace it with copy of T. The parse tree for uv wx y �L is given in figure 5.
Cutting out t and replacing it with copy of T as many times to get a valid parse tree for
i i
uv wx y for i � 1.
To get uwy � L, cut T out of the original tree and replace it with t to get a parse
0 0
tree of uv wx y = uwy as shown in figure 6.
88
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
Example:
i ii
Show that L = {a b c | i �1} is not CFL
Solution:
n nn
Assume L is CFL. Choose an appropriate z = a b c = uvwxy. Since |vwx| � n
then vwx can either consists of
2 22 22 2 2
If z = a b c and u = �, v = a, w = �, x = a and y = b c then, uv wx y will
4 22
be a b c �L
Case 2: vwx consists of some a‟s and some b‟s
2 22 2 2 2 3 32
If z = a b c and u = a, v = a, w = �, x = b, y = bc , then uv wx y will be a b c �L
2 22 2 2 2 2 32
If z = a b c and u = a b, v = b, w = c, x = �, y = c, then uv wx y will be a b c �L
2 2
If you consider any of the above 3 cases, uv wx y will not be having an equal number of
2 2
a‟s, b‟s and c‟s. But Pumping Lemma says uv wx y �L. Can‟t contradict the
pumping lemma! Our original assumption must be wrong. So L is not context-free.
Example:
Solution:
89
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
m n m n
Assume L is CFL. It is sufficient to show that L1= {0 1 0
n � 0}, where n is
n n1 n| m,n
pumping lemma constant, is a CFL. Pick any z = 0 1 0 1 = uvwxy, satisfying
the conditions |vwx| � n and vx ��.
|uwy| = 4n-k. If uwy is some repeating string tt, then |t| =2n-k/2. t does end in 0
but tt ends with 1. So second t is not a repetition of first t.
3 3 3 3 2 3 3 3 3
Example: z = 0 1 0 1 , vx = 0 then uwy = tt = 01 0 1 , so first t = 01 0
2 3.
and second t = 0 1 Both t‟sstare not same.
Suppose vwx consists of 1 block of 0’s and first block of 1’s: vx consists
of only 0‟sif x= �, then uwy is not in the form tt. If vx has at least one 1, then |t| is
at least 3n/2 and first t ends with a 0, not a 1.
Very similar explanations could be given for the cases of vwx consists of first block of
st nd
1‟s and vwx consists of 1 block of 1‟s and 2 block of 0‟s. In all cases uwy is
expected to be in the form of tt. But first t and second t are not the same string.
So uwy is not in L and L is not context free.
90
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
Example:
i j i j
Show that L={0 1 2 3 | i � 1, j � 1} is not CFL
Solution:
n n n n
Assume L is CFL. Pick z = uvwxy = 0 1 2 3 where |vwx| � n and vx ��
. vwx can consist of a substring of one of the symbols or straddles of two
adjacent symbols.
I. Substitution:
By substitution operation, each symbol in the strings of one language is replaced
by an entire CFL language
.
Example:
n n
S(0) = {a b | n �1}, S(1)={aa,bb} is a substitution on alphabet � ={0, 1}.
Theorem:
91
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
Proof:
Let G = (V, �, P, S) be grammar for the CFL L. Let Ga = (Va, Ta, Pa, Sa) be the
grammar corresponding to each terminal a �� and V � Va = �. Then G�=
(V�, T�, P�, S) is a grammar for s(L) where
V� = V � Va
T�= union of Ta‟s all for a ��
P� consists of
o
o
o All productions in any Pa for a��
o
o
o
o The productions of P, with each terminal a is replaced by Sa
everywhere a occurs.
Example:
n n n m
L = {0 1 | n � 1}, generated by the grammar S � 0S1 | 01, s(0) = {a b | m
� n}, generated by the grammar S � aSb | A; A � aA | ab, s(1) = {ab, abc},
generated by the grammar S � abA, A � c |�
. Rename second and third S‟s to S0 and S1, respectively. Rename second A to
B. Resulting grammars are:
S � 0S1 | 01
S0 � aS0b | A; A � aA | ab
S1 � abB; B � c | �
In the first grammar replace 0 by S0 and 1 by S1. The resulted grammar after substitution
is:
S � S0SS1 | S0S1
S0� aS0b | A; A �aA | ab S1�abB; B� c | �
92
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
How t
o get grammar for L1 � L2 ?
Example:
n n n n
L1 = {a b | n � 0}, L2 = {b a | n � 0}. Their corresponding
Example:
n n n n n {n+m} m
L1 = {a b | n � 0}, L2 = {b a | n � 0} then L1L2 = {a b a | n, m � 0}
Their corresponding grammars are
G1: S1 � aS1b | �, G2 : S2 � bS2a | �
IV. Reversal:
R
L is a CFL, so L is a CFL. It is enough to reverse each production of a CFL for
R
L, i.e., to substitute each production A�� by A�� .
IV. Intersection:
Example:
n n n n n i
The language L = {0 1 2 | n � 1} is not context-free. But L1 = {0 1 2 | n � 1, i
i n n
� 1} is a CFL and L2 = {0 1 2 | n � 1, i � 1} is also a CFL. But L = L1� L2.
Corresponding grammars for L1: S�AB; A�0A1 | 01; B�2B | 2 and
corresponding grammars for L2: S �AB; A�0A | 0; B�1B2 | 12.
94
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
Intersection of
a. CFL and Regular Language:
FA Accept/
AND Reject
PDA
P = (QP, �, �, �P, qP, Z0, FP) be PDA to accept L by final state. Let A = (QA, �, �A, qA, FA)
for DFA to accept the Regular Language R. To get L � R, we have to run a Finite
Stack
That is for each move of PDA P, we make the same move in PDA P� and also we carry
along the state of DFA A in a second component of P�. P� accepts a string w if and only if
both P and A accept w. ie w is in L � R. The moves ((qp, qA), w, Z) |-*P� ((q, p), �,
�) are possible if and only if (qp, w, Z) |-*P (q, �,�) moves and p = �*(qA, w)
transitions are possible.
Theorem: The following are true about CFL‟s L, L1, and L2, and a regular language R.
CFL. Proof:
95
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
C
R is regular and regular language is closed under complement. So R is also regular.
C
We know that L - R = L � R . We have already proved the closure of
intersection of a CFL and a regular language. So CFL is closed under set
difference with a Regular language.
C
L is not necessarily a
CFL Proof:
C
Assume that CFLs were closed under complement. C ie if LCis a CFL then L is
a CFL. Since CFLs
C areC closed
C L1 � C
under union, C L2C is a CFL. By our
assumption (L1 � L2 ) is a CFL. But (L1 � L2 ) = L1 � L2, which
we just showed isn‟t necessarily a CFL. Contradiction! . So our assumption is
false. CFL is not closed under complementation.
ie
L1 - L2 is not necessarily a CFL.
Proof:
C
Let L1 = �* - L. �* is regular and is also CFL. But �* - L = L . If CFLs were
C
closed under set difference, then �* - L = L would always be a CFL. But CFL‟s are
not closed under complementation. So CFLs are not closed under set-difference.
96
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
Assignment questions
1.Using pumping lemma for CFL prove that below languages are not context free
p
1. {0 | p is a prime}
n ni
2. {a b c | i � n}
98
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
L : Left R: Right
input symbols on infinite length tape
10101111110
head
The Turing machine model uses an infinite tape as its unlimited memory. (This is
important because it helps to show that there are tasks that these machines cannot
perform, even though unlimited memory and unlimited time is given.) The input
symbols occupy some of the tape‟s cells, and other cells contain blank symbols.
Solved examples:
TM Example 1:
99
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
Input : #111100000000…….
�
Output : #1111100000000……….
Initially the TM is in Start state S0. Move right as long as the input symbol is 1.
When a 0 is encountered, replace it with 1 and halt.
Transitions:
(S0, 1) (S0, 1, R)
(S0, 0) ( h , 1, STOP)
TM Example 2 :
TM: X-Y
Given two unary numbers x and y, compute |x-y| using a TM. For purposes of
simplicity we shall be using multiple tape symbols.
(S0, 1) (S1, _, R)
(S0, b) (h, b, STOP)
(S1, 1) (S1, 1, R)
(S1, b) (S2, b, R)
b) Once the first 1 of y is reached, stamp it out. If instead the input ends, then y
has finished. But in x, we have stamped out one extra 1, which we should
replace. So, go to some state s5 which can handle this.
(S2, 1) (S3, _, L)
(S2,_) (S2, _, R)
(S2, 0) (S5, 0, L)
(S3, _) (S3, _, L)
(S3,b) (S4, b, L)
100
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
(S4, 1) (S4, 1, L)
(S4, _) (S0, _, R)
(S4, #) (h, #, STOP)
d) State s5 is when y ended while we were looking for a 1 to stamp. This means
we have stamped out one extra 1 in x. So, go back to x, and replace the blank
character with 1 and stop the process.
(S5, _) (S5, _, L)
(S5,b) (S6, b, L)
(S6, 1) (S6, 1, L)
(S6, _) (h, 1, STOP)
Solved examples:
Step 1: Stamp the first 0 with X, then seek the first 1 and stamp it with Y, and
then seek the first 2 and stamp it with Z and then move left.
S0, 0 S1,X,R
S1, 0 S1, 0,R
S1, 1 S2,Y,R
S2, 1 S2, 1,R
S2, 2 S3,Z,L
S0 = Start State, seeking 0, stamp it with X
S1 = Seeking 1, stamp it with Y
S2 = Seeking 2, stamp it with Z
Step 2: Move left until an X is reached, then move one step right.
S3, 1 S3, 1,L
S3,Y S3,Y,L
S3, 0 S3, 0,L
S3,X S0,X,R
101
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
Step 3: Move right until the end of the input denoted by blank( _ ) is reached
passing through X Y Z s only, then the accepting state SA is reached.
S0,Y S4,Y,R
S4,Y S4,Y,R
S4,Z S4,Z,R
S4, SA,
, STOP
S4 = Seeking blank
S0, 1 h, 1,STOP
S0, 2 h, 2,STOP
S1, 2 h, 2,STOP
S2, h, ,STOP
S0, 0 S1, ,R
S0, 1 S2, ,R
S1, S3, ,L
S3, 1 h, 1,STOP
S2, S5,,L
S5, 0 h, 0,STOP
102
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
Step 2: If the last character is 0/1 accordingly, then move left until a blank is reached to
start the process again.
S 3, 0 S4, ,L
S 4, S4, 1,L 1
S 4, S4, 0,L
0
S 4, S0, ,R
S 5, 1 S6,,L
S 6, 1 S6, 1,L
S6, 0 S6, 0,L
S6, S0, ,R
S3, SA,
,STOP
S5, SA,
,STOP
#s010101_ _ _
#_s20101_ _ _
#_0s2101_ _ _
....
#_0101s5_ _ _
#_010s6_ _ _ _
#_s60101_ _ _
#_s00101_ _ _
....
#_ _ _ _ s5 _ _ _ _ _ _
#_ _ _ _ sA _ _ _ _ _ _
103
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
Modularization of TMs
n nn 0-Stamper 1-Seeker
TM: 0 1 2
1-Stamper
2-Seeker
2-Stamper
0-Seeker
Load Decode Execute Store
TM = (S,S0,H,T,d)
Suppose, S={a,b,c,d}, S0=a, H={b,d} T={0,1}
: (a,0) (b,1,R) , (a,1) (c,1,R) ,
(c,0) (d,0,R) and so on
then TM spec:
$abcd$a$bd$01$a0b1Ra1c1Rc0d0R…….
where $ is delimiter
This spec along with the actual input data would be the input to the UTM.
This can be encoded in binary by assigning numbers to each of the characters
appearing in the TM spec.
104
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
$ : 0000 0 : 0101
a : 0001 1 : 0110
b : 0010 L : 0111
c : 0011 R : 1000
d : 0100
So the TM spec given in previous slide can be encoded as:
0000.0001.0010.0011.0100.0000.0001.0000.0010.0100 ……
Hence TM spec can be regarded just as a number.
Track 0 011010100…
001111110…
Track 1
A composite tape consists of many tracks which can be read or written simultaneously.
A composite tape TM (CTM) contains more than one tracks in its tape.
105
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
T = {a, b, c, d}
T‟ = {00, 01, 10, 11}
Turing Machines with stay option has a third option for movement of the TM
head:
left, right or stay.
�: S x T à S x T x {L, R, S}
STM = TM:
Just don‟t use the S option…
TM = STM:
TM = STM:
106
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
… -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 …
0 –1 1 –2 2 –3 3 –4 4 –5 5 …
MTM = TM:
Use just the first tape…
TM = MTM:
Reduction of multiple tapes to a single tape.
A 01234567…
B 01234567…
C 01234567…
TM A0 B0 C0 A1 B1 C1 A2 B2 C2 A3 B3 ..
107
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
Non-deterministic TM
SxTx{L,R}
where �: S x T à2
(s2,a) à (s4,a,R)
108
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
Unit-8:Undesirability
8.1: A language that is not recursively enumerable
8.2: a un decidable problem that is RE
8.3: Posts correspondence problem
8.4: other undecidable problem
109
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
M
Accept Accept
Input M
Rejec Rejec
Complements of RE Languages
Theorem: If both a language L and its complement L are RE, L is
recursive. Proof: Let M1 and M2 be TM for L and L respectively. We
can construct a TMM from M1 and M2 for L that always halt as follows:
M
M1 Accept Accept
Input
M2 Accept Reject
A Non-recursive RE Language
110
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
Ld
We will now
Recursive look at an
example in
this region.
Recursively
Enumerable (RE)
Non-recursively
Enumerable (Non-RE)
A Non-recursive RE Language
• Recall that we can encode each TM uniquely as a binary number and
enumerate all TM‟s as T1, T2, …, Tk, … where the encoded value of the
kth TM, i.e., Tk, is k.
• Consider the language Lu:
Lu = {(k, w) | Tk accepts input w} This
is called the universal language.
Universal Language
• Note that designing a TM to recognize Lu is the same as solving the
problem of given k and w, decide whether Tk accepts w as its input.
• We are going to show that Lu is RE but non-recursive, i.e., Lu can be
accepted by a TM, but there is no TM for Lu that always halt.
111
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
k separator w
U will move back and forth to mimic Tk on input w.
Accept Accept
(k, w) Tk
w
i.e., k1111110w
U
112
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
Universal Language
• Since there is a TM that accepts Lu,
Lu is RE. We are going to show that
Lu is non-recursive.
• If Lu is recursive, there is a TM M for
Lu that always halt. Then, we can
construct a TM M‟ for Ld as follows:
Accept Reject
k Copy k1111110k M Reject Accept
M‟
A Non-recursive RE Language
• Since we have already shown that Ld is non-recursively enumerable, so
M‟ does not exist and there is no such M.
• Therefore the universal language is recursively enumerable but non-
recursive. Halting Problem
Consider the halting problem:
Given (k,w), determine if Tk halts on w.
It‟s corresponding language
is: Lh = { (k, w) | Tk halts on input w}
The halting problem is also undecidable, i.e., Lh is non-recursive. To
show this, we can make use of the universal language problem.
We want to show that if the halting problem can be solved
(decidable), the universal language problem can also be solved.
So we will try to reduce an instance (a particular problem) in Lu to an
instancein Lh in such a way that if we know the answer for the latter,
we will know the answer for the former.
Class Discussion
Consider a particular instance (k,w) in Lu, i.e., we want to determine if Tk
will accept w. Construct an instance I=(k‟,w‟) in Lh from (k,w) so that if we
know whether Tk‟ will halt on w‟, we will know whether Tk will accept w.
Halting Problem
Therefore, if we have a method to solve the halting problem, we can also solve the
universal language problem. (Since for any particular instance I of the universal
language problem, we can construct an instance of the halting problem, solve it and
get the answer for I.) However, since the universal problem is undecidable, we can
conclude that the halting problem is also undecidable.
Modified Post Correspondence Problem
113
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
• We have seen an undecidable problem, that is, given a Turing machine M and
an input w, determine whether M will accept w (universal language problem).
• We will study another undecidable problem that is not related to Turing machine
directly.
Given two lists A and B:
A = w1, w2, …, wk B = x1, x2, …, xk
The problem is to determine if there is a sequence of one or more
integers i1, i2, …, im such that:
w1wi1wi2…wim = x1xi1xi2…xim
Example
A B
w
i i xi
1 11 1
2 1 111
3 0111 10
4 10 0
This MPCP instance has a solution: 3, 2, 2, 4:
w1w3w2w2w4 = x1x3x2x2x4 = 1101111110
114
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
Undecidability of PCP
To show that MPCP is undecidable, we will
reduce the universal language problem
(ULP) to MPCP:
Universal
Language A mapping MPCP
Problem (ULP)
115
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
Construct an
Given: MPCP instance Two lists:
(T,w) A and B
116
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
q0 0/0, L q1
1/0, R
(q0,1)=(q0,0,R) (q0,0)=(q1,0,L)
• Consider input w=110.
118
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
Class Discussion
Consider the input w = 101. Construct the corresponding MPCP
instance I and show that T will accept w by giving a solution to I.
119
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
• Using this mapping, we can prove that the original ULP instance has a
solution if and only if the mapped MPCP instance has a solution.
(Textbook, p.402, Theorem 9.19)
120
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
Example:
A = 1; 10111; 10
B = 111; 10; 0
121
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
122
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
Accept M Accept
Input M
Rejec Rejec
Complements of RE Languages
Theorem: If both a language L and its complement L are RE, L is
recursive. Proof: Let M1 and M2 be TM for L and L respectively. We
can construct a TMM from M1 and M2 for L that always halt as follows:
M
M1 Accept Accept
Input
M2 Accept Reject
123
FORMAL LANGUAGES AND AUTOMATA THEORY 10CS56
ASSIGNMENT QUESTIONS
Unit 8:
124