Automata Theory-Notes
Automata Theory-Notes
1. Introduction........................................................................................................................................... 1
Automata – What is it?.....................................................................................................................................1
Related Terminologies......................................................................................................................................1
5. DFA Minimization................................................................................................................................. 11
DFA Minimization using Myphill-Nerode Theorem........................................................................................11
DFA Minimization using Equivalence Theorem..............................................................................................13
CLASSIFICATION OF GRAMMARS...............................................................................20
7. Introduction to Grammars.................................................................................................................... 21
Grammar.........................................................................................................................................................21
Derivations from a Grammar..........................................................................................................................22
REGULAR GRAMMAR...................................................................................................... 28
CONTEXT-FREE GRAMMARS........................................................................................ 47
PUSHDOWN AUTOMATA................................................................................................. 66
TURING MACHINE.............................................................................................................76
iii
Automata Theory
DECIDABILITY....................................................................................................................86
Related Terminologies
Alphabet
Definition: An alphabet is any finite set of symbols.
Example: Σ = {a, b, c, d} is an alphabet set where ‘a’, ‘b’, ‘c’, and ‘d’ are
symbols.
String
Definition: A string is a finite sequence of symbols taken from Σ.
Length of a String
Definition : It is the number of symbols present in a string. (Denoted by |S|).
Examples:
o If S=‘cabcad’, |S|= 6
1
Automata Theory
Kleene Star
*
Definition: The Kleene star, Σ , is a unary operator on a set of symbols or
strings, Σ, that gives the infinite set of all possible strings of all possible lengths
over Σ including λ.
*
Representation: Σ = Σ0 U Σ1 U Σ2 U……. where Σp is the set of all possible
strings of length p.
*
Example: If Σ = {a, b}, Σ = {λ, a, b, aa, ab, ba, bb,………..}
+
Representation: Σ = Σ1 U Σ2 U Σ3 U…….
+
Σ =Σ*−{λ}
+
Example: If Σ = { a, b } , Σ ={ a, b, aa, ab, ba, bb,………..}
Language
Definition : A language is a subset of Σ* for some alphabet Σ. It can be finite or
infinite.
Example : If the language takes all possible strings of length 2 over Σ = {a, b},
then L = { ab, bb, ba, bb}
2
2. Deterministic Finite AutomatonAutomata Theory
Example
Let a deterministic finite automaton be
Q = {a, b, c},
Σ = {0, 1},
q0={a},
F={c}, and
3
Automata Theory
1 0
a b
1 0
1
0
4
3. Non-deterministic Finite AutomatonAutomataTheory
In NDFA, for a particular input symbol, the machine can move to any combination of the
states in the machine. In other words, the exact state to which the machine moves
cannot be determined. Hence, it is called Non-deterministic Automaton. As it has
finite number of states, the machine is called Non-deterministic Finite Machine or
Non-deterministic Finite Automaton.
Σ is a finite set of symbols called the alphabets.
Q
δ is the transition function where δ: Q × Σ → 2
Q
(Here the power set of Q (2 ) has been taken because in case of NDFA, from a
state, transition can occur to any combination of Q states)
q0 is the initial state from where any input is processed (q0 ∈ Q).
F is a set of final state/states of Q (F ⊆ Q).
Example
Let a non-deterministic finite automaton be
Q = {a, b, c}
Σ={0,1}
q0 = {a}
F={c}
5
Automata Theory
1 0
a b
0, 1 0, 1
0 0, 1
DFA vs NDFA
The following table lists the differences between DFA and NDFA.
DFA NDFA
The transition from a state is to a single The transition from a state can be to
particular next state for each input multiple next states for each input symbol.
symbol. Hence it is called deterministic. Hence it is called non-deterministic.
6
Automata Theory
Acceptor (Recognizer)
An automaton that computes a Boolean function is called an acceptor. All the states of
an acceptor is either accepting or rejecting the inputs given to it.
Classifier
A classifier has more than two final states and it gives a single output when it terminates.
Transducer
An automaton that produces outputs based on current input and/or previous state is
called a transducer. Transducers can be of two types:
Mealy Machine The output depends both on the current state and the current
input.
δ*(q0, S) ∈ F
{S | S ∈ Σ* and δ*(q0, S) ∈ F}
The language L′ not accepted by DFA/NDFA (Complement of accepted language L) is {S | S ∈ Σ* and δ*(q0, S) ∉ F}
7
Automata Theory
Example
Let us consider the DFA shown in Figure 1.3. From the DFA, the acceptable strings can
be derived.
0
1
a c
0 1
1
d
0
Strings accepted by the above DFA: {0, 00, 11, 010, 101, ...........}
Strings not accepted by the above DFA: {1, 011, 111, ........}
8
4. NDFA to DFA Conversion Automata Theory
Problem Statement
Let X = (Qx, Σ, δx, q0, Fx) be an NDFA which accepts the language L(X). We have to
design an equivalent DFA Y = (Qy, Σ, δy, q0, Fy) such that L(Y) = L(X). The following
procedure converts the NDFA to its equivalent DFA:
Algorithm
Input: An NDFA
Step 2 Create a blank state table under possible input alphabets for the equivalent
DFA.
Step 3 Mark the start state of the DFA by q0 (Same as the NDFA).
Step 4 Find out the combination of States {Q0, Q1,... , Qn} for each possible input
alphabet.
Step 5 Each time we generate a new DFA state under the input alphabet columns,
we have to apply step 4 again, otherwise go to step 6.
Step 6 The states which contain any of the final states of the NDFA are the final
states of the equivalent DFA.
Example
Let us consider the NDFA shown in the figure below.
q δ(q,0) δ(q,1)
a {a,b,c,d,e} {d,e}
b {c} {e}
c ∅ {b}
d {e} ∅
e ∅ ∅
9
Automata Theory
Using the above algorithm, we find its equivalent DFA. The state table of the DFA is
shown in below.
q δ(q,0) δ(q,1)
[d,e] [e] ∅
[e] ∅ ∅
[c,e] ∅ [b]
[c] ∅ [b]
1 0
[a,b,c,d,e] [b,d,e] [c,e]
0 1
1
[a] [b]
1 1 1
0
0
[d,e] [e] [c]
10
5. DFA Minimization Automata Theory
Algorithm
Input DFA
Step 1 Draw a table for all pairs of states (Q i, Qj) not necessarily connected
directly [All are unmarked initially]
Step 2 Consider every state pair (Qi, Qj) in the DFA where Qi ∈ F and Qj ∉ F or vice versa and mark them. [Here F is the set of final states]
If there is an unmarked pair (Qi, Qj), mark it if the pair {δ(Qi, A), δ (Qi,
A)} is marked for some input alphabet.
Step 4 Combine all the unmarked pair (Qi, Qj) and make them a single state in
the reduced DFA.
Example
Let us use Algorithm 2 to minimize the DFA shown below.
0, 1
1 1
b f
0
0 0 1
1
a e
1 0 0
11
Automata Theory
a b c d e f
a
b
c
d
e
f
a b c d e f
a
b
c ✓ ✓
d ✓ ✓
e ✓ ✓
f ✓ ✓ ✓
Step 3 : We will try to mark the state pairs, with green colored check mark, transitively.
If we input 1 to state ‘a’ and ‘f’, it will go to state ‘c’ and ‘f’ respectively. (c, f) is already
marked, hence we will mark pair (a, f). Now, we input 1 to state ‘b’ and ‘f’; it will go to
state ‘d’ and ‘f’ respectively. (d, f) is already marked, hence we will mark pair (b, f).
a b c d e f
a
b
c ✓ ✓
d ✓ ✓
e ✓ ✓
f ✓ ✓ ✓ ✓ ✓
After step 3, we have got state combinations {a, b} {c, d} {c, e} {d, e} that are
unmarked.
12
Automata Theory
So the final minimized DFA will contain three states {f}, {a, b} and {c, d, e}
0 0, 1
1
(a, (c, d, e)
(f
0, 1
Algorithm 3
Step 1: All the states Q are divided in two partitions: final states and non-final
th
states and are denoted by P0. All the states in a partition are 0
equivalent. Take a counter k and initialize it with 0.
Step 2: Increment k by 1. For each partition in P k, divide the states in Pk into two
partitions if they are k-distinguishable. Two states within this partition X
and Y are k-distinguishable if there is an input S such that δ(X, S) and
δ(Y, S) are (k-1)-distinguishable.
13
Automata Theory
Example
Let us consider the following DFA:
0, 1
q δ(q,0) δ(q,1)
1 1
b f a b c
0 b a d
0 0 1
1 c e f
d e f
a e
1 0 e e f
0
f f f
P0 = {(c,d,e), (a,b,f)}
P1 = {(c,d,e), (a,b),(f)}
P2 = {(c,d,e), (a,b),(f)}
Hence, P1 = P2.
There are three states in the reduced DFA. The reduced DFA is as follows:
Q δ(q,0) δ(q,1) 0
0, 1
1
(a, b) (a, b) (c,d,e) (a, (c, d, e)
(c,d,e) (c,d,e) (f)
1
(f) (f) (f) 0,1 (f
14
6. Moore and Mealy MachinesAutomata Theory
Finite automata may have outputs corresponding to each transition. There are two types
of finite state machines that generate output:
Mealy Machine
Moore machine
Mealy Machine
A Mealy Machine is an FSM whose output depends on the present state as well as the
present input.
Next state
Present
state input = 0 input = 1
State Output State Output
a b 1
c 1
b b 2
d 3
c d 3
c 1
d d 3
d 2
15
Automata Theory
0
0 /x3, 1
b
0 1
Moore Machine
Moore machine is an FSM whose outputs depend on only the present state.
Next State
Present State Output
Input = 0 Input = 1
a b c 2
b b d 1
c c d 2
d d d 3
16
Automata Theory
0
b/x1 0, 1
0
1 d
/x
a/x2
1 c/x2 0
Output depends both upon present Output depends only upon the present
state and present input. state.
Generally, it has fewer states than Generally, it has more states than Mealy
Moore Machine. Machine.
Algorithm 4
Input: Moore Machine
Step 2 Copy all the Moore Machine transition states into this table format.
Step 3 Check the present states and their corresponding outputs in the Moore
Machine state table; if for a state Q i output is m, copy it into the output
columns of the Mealy Machine state table wherever Q i appears in the next
state.
17
Automata Theory
Example
Let us consider the following Moore machine:
a d b 1
b a d 0
c c c 0
d b a 1
State table of a Moore Machine
Step 1 & 2:
Next State
Present State a=0 a=1
State Output State Output
a d b
b a d
c c c
d b a
Step 3:
Next State
Present State a=0 a=1
State Output State Output
=> a d 1 b 0
b a 1 d 1
c c 0 c 0
d b 0 a 1
18
Automata Theory
Algorithm 5:
Input: Mealy Machine
Step 1 Calculate the number of different outputs for each state (Q i) that are
available in the state table of the Mealy machine.
Step 2 If all the outputs of Qi are same, copy state Qi. If it has n distinct outputs,
break Qi into n states as Qin where n = 0, 1, 2.......
Step 3 If the output of the initial state is 1, insert a new initial state at the
beginning which gives 0 output.
Example
Let us consider the following Mealy Machine:
Next State
Present
a=0 a=1
State
Next Next
Output Output
State State
a d 0 b 1
b a 1 d 0
c c 1 c 0
d b 0 a 1
State table of a Mealy Machine
Here, states ‘a’ and ‘d’ give only 1 and 0 outputs respectively, so we retain states ‘a’ and
‘d’. But states ‘b’ and ‘c’ produce different outputs (1 and 0). So, we divide b into b0, b1
and c into c0, c1.
19
Automata Theory
Classification of Grammars
20
7. Introduction to GrammarsAutomata Theory
In the literary sense of the term, grammars denote syntactical rules for conversation in
natural languages. Linguistics have attempted to define grammars since the inception of
natural languages like English, Sanskrit, Mandarin, etc.
The theory of formal languages finds its applicability extensively in the fields of
Computer Science. Noam Chomsky gave a mathematical model of grammar in 1956
which is effective for writing computer languages.
Grammar
A grammar G can be formally written as a 4-tuple (N, T, S, P) where
P is Production rules for Terminals and Non-terminals. A production rule has the form → , where and are strings on ∪ Σ
and least one symbol of belongs to VN.
Example
Grammar G1:
Here,
Example:
Grammar G2:
Here,
21
Automata Theory
ε is an empty string.
S is the Start symbol, S ∈ N
Example
Let us consider the grammar:
22
8. Language Generated by a Grammar Automata
Theory
The set of all strings that can be derived from a grammar is said to be the language
generated from that grammar. A language generated by a grammar G is a subset
formally defined by
Σ∗
()={ |∈ , ⇒ }
Example
If there is a grammar
Here S produces AB, and we can replace A by a, and B by b. Here, the only accepted
string is ab, i.e.,
L(G) = {ab}
Example
Suppose we have the following grammar:
Example
m n
Problem Suppose, L (G) = {a b | m ≥ 0 and n > 0}. We have to find out the
grammar G which produces L(G).
Solution
m n
Since L(G) = {a b | m ≥ 0 and n > 0}
the set of strings accepted can be rewritten as:
Here, the start symbol has to take at least one ‘b’ preceded by any number of ‘a’
including null.
23
Automata Theory
To accept the string set {b, ab, bb, aab, abb, …….}, we have taken the productions:
S →B→ b (Accepted)
S →B→ bB → bb (Accepted)
Thus, we can prove every single string in L(G) is accepted by the language generated by
the production set.
Example
m n
Problem: Suppose, L (G) = {a b | m> 0 and n ≥ 0}. We have to find out the
grammar G which produces L(G).
Solution:
m n
Since L(G) = {a b | m> 0 and n ≥ 0}, the set of strings accepted can be rewritten as:
L(G) = {a, aa, ab, aaa, aab ,abb, …….}
Here, the start symbol has to take at least one ‘a’ followed by any number of ‘b’
including null.
To accept the string set {a, aa, ab, aaa, aab, abb, …….}, we have taken the productions:
S → aA, A → aA , A → B, B → bB ,B → λ
Thus, we can prove every single string in L(G) is accepted by the language generated by
the production set.
24
9. Chomsky Classification of GrammarsAutomataTheory
According to Noam Chomosky, there are four types of grammars: Type 0, Type 1, Type
2, and Type 3. The following table shows how they differ from each other:
Recursively enumerable
Type 0 Unrestricted grammar Turing machine
language
Context-sensitive Linear-bounded
Type 1 Context-sensitive grammar
language automaton
Pushdown
Type 2 Context-free grammar Context-free language
automaton
Finite state
Type 3 Regular grammar Regular language
automaton
Take a look at the following illustration. It shows the scope of each type of grammar:
Recursively Enumerable
Context-Sensitive
Context - Free
Regular
25
Automata Theory
Type - 3 Grammar
Type-3 grammars generate regular languages. Type-3 grammars must have a single
non-terminal on the left-hand side and a right-hand side consisting of a single terminal
or single terminal followed by a single non-terminal.
and a ∈ T (Terminal)
The rule S → ε is allowed if S does not appear on the right side of any rule.
Example
X → ε
X → a | aY
Y →b
Type - 2 Grammar
Type-2 grammars generate context-free languages.
*
and γ ∈ (T∪N) (String of terminals and non-terminals).
Example
S → X a
X → a
X → aX
X → abc
X → ε
26
Automata Theory
Type - 1 Grammar
Type-1 grammars generate context-sensitive languages. The productions must be in
the form
αAβ→αγβ
where A ∈ N (Non-terminal)
*
and α, β, γ ∈ (T ∪ N) (Strings of terminals and non-terminals)
The rule S → ε is allowed if S does not appear on the right side of any rule. The
languages generated by these grammars are recognized by a linear bounded automaton.
Example
AB → AbBc
A → bcA
B → b
Type - 0 Grammar
Type-0 grammars generate recursively enumerable languages. The productions have
no restrictions. They are any phase structure grammar including all formal grammars.
The productions can be in the form of α→ β where α is a string of terminals and non-
terminals with at least one non-terminal and α cannot be null. β is a string of terminals
and non-terminals.
Example
S → ACaB
Bc → acB
CB→DB
aD → Db
27
Automata Theory
Regular Grammar
28
10. Regular Expressions Automata Theory
Some RE Examples
Regular
Regular Set
Expression
Set of strings of a’s and b’s of any length including the null string.
(a+b)*
So L= { ε, a, b, aa , ab , bb , ba, aaa…….}
(a+b)*abb Set of strings of a’s and b’s ending with the string abb.
So L = {abb, aabb, babb, aaabb, ababb, …………..}
29
Automata Theory
30
11. Regular Sets Automata Theory
Any set that represents the value of the Regular Expression is called a Regular Set.
Proof:
So, L1= {a, aaa, aaaaa,.....} (Strings of odd length excluding Null)
and L2={ ε, aa, aaaa, aaaaaa,.......} (Strings of even length including Null)
L1 ∪ L2 = { ε, a, aa, aaa, aaaa, aaaaa, aaaaaa,.......}
RE (L1 ∪ L2) = a*
(which is a regular expression itself)
Hence, proved.
Proof:
So, L1 = { a,aa, aaa, aaaa, ....} (Strings of all possible lengths excluding Null)
Hence, proved.
Proof:
RE = (aa)*
31
Automata Theory
So, L = {ε, aa, aaaa, aaaaaa, .......} (Strings of even length including Null)
So, L’ = {a, aaa, aaaaa, .....} (Strings of odd length excluding Null)
Hence, proved.
Proof:
So, L1= {a,aa, aaa, aaaa, ....} (Strings of all possible lengths excluding Null)
Hence, proved.
Proof:
R
We have to prove L is also regular if L is a regular set.
Let, L= {01, 10, 11, 10}
RE(L)=01+10+11+10
R
L = {10, 01, 11, 01}
R
RE (L )= 01+ 10+ 11+10 which is regular
Hence, proved.
32
Automata Theory
Proof:
L*= {a, aa, aaa, aaaa , aaaaa,……………} (Strings of all lengths excluding Null)
RE (L*) = a (a)*
Hence, proved.
Proof:
Here, L1 = {0, 00, 10, 000, 010, ......} (Set of strings ending in 0)
Then, L1 L2 = {001,0010,0011,0001,00010,00011,1001,10010,.............}
Hence, proved.
1. Ø*=ε
2. ε*=ε
3. RR* = R*R
4. R*R* = R*
5. (R*)* = R*
6. RR* = R*R
7. (PQ)*P =P(QP)*
33
Automata Theory
15. ε+RR*=ε+R*R=R*
34
12. Arden’s Theorem Automata Theory
In order to find out a regular expression of a Finite Automaton, we use Arden’s Theorem
along with the properties of regular expressions.
Statement:
If P does not contain null string, then R = Q + RP has a unique solution that is R
= QP*
Proof:
=Q+QP+RPP
When we put the value of R recursively again and again, we get the following equation:
2 3
R = Q + QP + QP + QP …..
2 3
R=Q(є+P+P +P +….)
2 3
R = QP* [As P* represents (є + P + P + P + ….) ]
Hence, proved.
Method
Step 1: Create equations as the following form for all the states of the DFA having
n states with initial state q1.
……………………………
qn = q1R1n + q2R2n + … + qnRnn
35
Automata Theory
Rij represents the set of labels of edges from qi to qj, if no such edge exists, then Rij = Ø
Step 2: Solve these equations to get the equation for the final state in terms of Rij
Problem
Construct a regular expression corresponding to the automata given below:
b a
q2
b q3
b a
q1
a
Finite automata
Solution
The equations for the three states q1, q2, and q3 are as follows:
q1 = q1a + q3a + є
36
Automata Theory
Problem
Construct a regular expression corresponding to the automata given below:
0 0, 1
q1 q3
1 1
q
0
Finite automata
Solution:
q1 = q10 + є
q2 = q11 + q20
q3 = q21 + q30 + q31
q1 = є0* [As, εR = R]
So, q1 = 0*
q2 = 0*1 + q20
So, q2 = 0*1(0)* [By Arden’s theorem]
37
13. Construction of an FA from anAutomataRE Theory
We can use Thompson's Construction to find out a Finite Automaton from a Regular
Expression. We will reduce the regular expression into smallest regular expressions and
converting these to NFA and finally to DFA.
Case 1: For a regular expression ‘a’, we can construct the following FA:
q1 q
a
Case 2: For a regular expression ‘ab’, we can construct the following FA:
q1 q1 q
a b
Case 3: For a regular expression (a+b), we can construct the following FA:
q1 q
a
38
Automata Theory
Case 4: For a regular expression (a+b)*, we can construct the following FA:
a,b
Method:
Step 1 Construct an NFA with Null moves from the given regular expression.
Step 2 Remove Null transition from the NFA and convert it into its equivalent DFA.
Problem
Convert the following RA into its equivalent DFA: 1 (0 + 1)* 0
Solution:
0, 1
q0 q1 q2 q3 q
1 є є 0
Now we will remove the є transitions. After we remove the є transitions from the NDFA,
we get the following:
0, 1
q0 q2 q
1 0
39
Automata Theory
a b
ε ε
0 1
40
Automata Theory
Problem
Convert the following NFA-ε to NFA without Null move.
0
ε 0, 1
q q
1
0
q
1
Solution
Step 1:
Step 2:
Now we will Copy all these edges from q1 without changing the edges from qf and
get the following FA:
0
0, 1
0, 1
q q
1
0
q
1
41
Automata Theory
Step 3:
So the FA becomes -
0
0, 1
q q 0, 1
1
0
q
1
Step 4:
So the FA becomes -
0
0, 1
q q 0, 1
1 0
q
1
42
44
Automata Theory
Context-Free Grammars
47
16. Context-Free Grammar IntroductionAuomataTheory
Example
The grammar ({A}, {a, b, c}, P, A), P : A → aA, A → abc.
The grammar ({S, a, b}, {a, b}, P, S), P: S → aSa, S → bSb, S → ε
The grammar ({S, F}, {0, 1}, P, S), P: S → 00S | 11F, F → 00F | ε
Representation Technique:
1. Root vertex: Must be labeled by the start symbol.
2. Vertex: Labeled by a non-terminal symbol.
3. Leaves: Labeled by a terminal symbol or ε.
If S → x1x2 …… xn is a production rule in a CFG, then the parse tree / derivation tree will
be as follows:
x1 x2 xn
48
Automata Theory
1. Top-down Approach:
(a) Starts with the starting symbol S
(b) Goes down to tree leaves using productions
2. Bottom-up Approach:
(a) Starts from tree leaves
(b) Proceeds upward to the root which is the starting symbol S
Example
Let a CFG {N,T,P,S} be
S S
a S b a S b
ε a S b
49
Automata Theory
Example
If in any CFG the productions are:
A B
If a partial derivation tree contains the root S, it is called a sentential form. The above
sub-tree is also in sentential form.
Example
Let any set of production rules in a CFG be
50
Automata Theory
Step 1:
Step 2:
X X
X + X X + X
a
Step 3:
X
Step 4:
X
X + X
X + X
X * X
a
X * X
a
Step 5:
X a
X + X
X * X
a
a a
The rightmost derivation for the above string "a+a*a" may be:
51
Automata Theory
Step 1:
Step 2:
X X
X * X X * X
X
Step 3: Step 4:
X
X * X
X * X
X + X
a
X + X
a
Step 5: X a
X * X
X + X
a
a a
52
17. Ambiguity in Context-Free GrammarsAutomtaTheory
If a context free grammar G has more than one derivation tree for some string w ∈
L(G), it is called an ambiguous grammar. There exist multiple right-most or left-most
derivations for some string generated from that grammar.
Problem
Check whether the grammar G with production rules:
is ambiguous or not.
Solution
Let’s find out the derivation tree for the string "a+a*a". It has two leftmost derivations.
Parse tree 1:
X + X
X * X
a
a a
53
Automata Theory
Parse tree 2:
X * X
X + X
a
a a
Since there are two parse trees for a single string "a+a*a", the grammar G is ambiguous.
54
18. CFL Closure Property Automata Theory
Union
Concatenation
Kleene Star operation
Union
Let L1 and L2 be two context free languages. Then L1 L2 is also context free.
Example:
n n
Let L1 = { a b , n>0}. Corresponding grammar G1 will have P: S1 aAb|ab
m m
Let L2 = { c d , n≥0}. Corresponding grammar G2 will have P: S2 cBb| ε
n n m m
Union of L1 and L2, L = L1 L2 = { a b } { c d }
The corresponding grammar G will have the additional production S S1 | S2
Concatenation
If L1 and L2 are context free languages, then L1L2 is also context free.
Example:
n n m m
Union of the languages L1 and L2, L = L1L2 = { a b c d }
The corresponding grammar G will have the additional production S S1 S2
Kleene Star
If L is a context free language, then L* is also context free.
Example:
n n
Let L = { a b , n≥0}. Corresponding grammar G will have P: S aAb|
n n
ε Kleene Star L1 = { a b }*
The corresponding grammar G1 will have additional productions S1 SS1 | ε
Complement : If L1 is a context free language, then L1’ may not be context free. 55
19. CFG Simplification Automata Theory
In a CFG, it may happen that all the production rules and symbols are not needed for the
derivation of strings. Besides, there may be some null productions and unit productions.
Elimination of these productions and symbols is called simplification of CFGs.
Simplification essentially comprises of the following steps:
Reduction of CFG
Removal of Unit Productions
Removal of Null Productions
Reduction of CFG
CFGs are reduced in two phases:
Phase 1: Derivation of an equivalent grammar, G’, from the CFG, G, such that each
variable derives some terminal string.
Derivation Procedure:
Step 1: Include all symbols, W1, that derive some terminal and initialize i=1.
Phase 2: Derivation of an equivalent grammar, G”, from the CFG, G’, such that each
symbol appears in a sentential form.
Derivation Procedure:
Step 2: Include all symbols, Yi+1, that can be derived from Yi and include all
production rules that have been applied.
56
Automata Theory
Problem
Find a reduced grammar equivalent to the grammar G, having production rules, P: S
AC | B, A a, C c | BC, E aA | e
Solution
Phase 1:
T = { a, c, e }
W2 = { A, C, E } U { S } from rule S AC
W3={A,C,E,S}U
G’ = { { A, C, E, S }, { a, c, e }, P, {S}}
where P: S AC, A a, C c , E aA | e
Phase 2:
Y1={S}
Y2 = { S, A, C } from rule S AC
Y4 = { S, A, C, a, c }
G” = { { A, C, S }, { a, c }, P, {S}}
where P: S AC, A a, C c
Removal Procedure:
Step 1: To remove A→B, add production A→x to the grammar rule whenever B→x occurs in the grammar. [x ∈ Terminal, x can be Null]
Step 3: Repeat from step 1 until all unit productions are removed.
57
Automata Theory
Problem
Remove unit production from the following:
S → XY, X → a, Y → Z | b, Z → M, M → N, N → a
Solution:
Y → Z, Z → M, and M→N
S → XY, X → a, Y → Z | b, Z → M, M → a, N → a
S → XY, X → a, Y → Z | b, Z → a, M → a, N → a
S → XY, X → a, Y → a | b, Z → a, M → a, N → a
S → XY, X → a, Y→a|b
ϵ: A → .......… → ϵ
58
Automata Theory
Removal Procedure:
Step1 Find out nullable non-terminal variables which derive ϵ.
Step3 Combine the original productions with the result of step 2 and remove ϵ-
productions.
Problem
Remove null production from the following:
S→ASA | aB | b, A → B, B → b | ϵ
Solution:
S→ASA | aB | b | a, A → B| b | ϵ, B → b
S→ASA | aB | b | a | SA | AS | S, A → B| b, B→b
59
20. Chomsky Normal Form Automata Theory
A CFG is in Chomsky Normal Form if the Productions are in the following forms:
A→a
A→BC
S→ϵ
Step 2 Remove Null productions. (Using the Null production removal algorithm
discussed earlier)
Step 3 Remove unit productions. (Using the Unit production removal algorithm
discussed earlier)
Step 4 Replace each production A → B1…Bn where n > 2 with A → B1C where C → B2
…Bn. Repeat this step for all productions having two or more symbols in the
right side.
Step 5 If the right side of any production is in the form A→ aB where a is a terminal
and A, B are non-terminal, then the production is replaced by A→ XB and X→
a. Repeat this step for every production which is in the form A→ aB.
Problem:
Convert the following CFG into CNF
Solution:
(1) Since S appears in R.H.S, we add a new state S0 and S0→S is added to the
production set and it becomes:
60
Automata Theory
S0→S, S→ ASA | aB | a, A → B | S | ϵ, B → b
S0→S, S→ ASA | aB | a | AS | SA | S, A → B | S, B → b
A → B | S, B→b
A→S|b
B→b
A → b |ASA | aB | a | AS | SA, B → b
(4) Now we will find out more than two variables in the R.H.S
Hence we will apply step 4 and step 5 to get the following final production set which is in
CNF:
S0→ AX | aB | a | AS | SA
S→ AX | aB | a | AS | SA
A → b |AX | aB | a | AS | SA
B→b
X→ SA
61
Automata Theory
S0→ AX | YB | a | AS | SA
S→ AX | YB | a | AS | SA
A → b |AX | YB | a | AS |
SA B → b
X→ SA
Y→a
62
21. Greibach Normal Form Automata Theory
A CFG is in Greibach Normal Form if the Productions are in the following forms:
A→b
A → bD1…Dn
S→ϵ
Step 2 Remove Null productions. (Using the Null production removal algorithm
discussed earlier)
Step 3 Remove unit productions. (Using the Unit production removal algorithm
discussed earlier)
Problem:
Convert the following CFG into CNF
S→ XY | Xn | p
X → mX | m
Y → Xn | o
Solution:
Here, S does not appear on the right side of any production and there are no unit or null
productions in the production rule set. So, we can skip Step 1 to Step 3.
Step 4:
X in S → XY | Xo | p
with
mX | m
63
Automata Theory
we obtain
S → mXY | mY | mXo | mo | p.
X in Y→ Xn | o
X → mX | m
we obtain
Y→ mXn | mn | o.
Two new productions O→ o and P → p are added to the production set and then we
came to the final GNF as the following:
S → mXY | mY | mXC | mC | p
X→ mX | m
Y→ mXD | mD | o
O→o
P→p
64
Automata Theory
Pushdown Automata
66
23. Pushdown Automata IntroductionAutmata Theory
an input tape,
a control unit, and
a stack with infinite size.
A PDA may or may not read an input symbol, but it has to read the top of the stack in
every transition.
Takes
input Finite control
Accept or reject
unit
Push or Pop
Input tape
Stack
67
Automata Theory
The following diagram shows a transition in a PDA from a state q1 to state q2, labeled as
a,b → c :
Push
Input Stack top
Symbol
Symbol Symbol
a,b→c
q q
This means at state q1, if we encounter an input string ‘a’ and top symbol of the stack is
‘b’, then we pop ‘b’, push ‘c’ on top of the stack and move to state q2.
Instantaneous Description
The instantaneous description (ID) of a PDA is represented by a triplet (q, w, s) where
q is the state
w is unconsumed input
s is the stack contents
Turnstile Notation
The "turnstile" notation is used for connecting pairs of ID's that represent one or many moves of a PDA. The process of transition is denoted by the turnstile symbol "⊢".
68
Automata Theory
This implies that while taking a transition from state p to state q, the input symbol ‘a’ is
consumed, and the top of the stack ‘T’ is replaced by a new string ‘α’.
Note: If we want zero or more moves of a PDA, we have to use the symbol (⊢*) for it.
69
24. Pushdown Automata AcceptanceAutomata Theory
For a PDA (Q, Σ, S, δ, q0, I, F), the language accepted by the set of final states F is:
L(PDA) = {w | (q0, w, I) ⊢* (q, ε, x), q ∈ F} for any input stack
string x.
For a PDA (Q, Σ, S, δ, q0, I, F), the language accepted by the empty stack is:
L(PDA) = {w | (q0, w, I) ⊢* (q, ε, ε), q ∈ Q}
Example
n n
Construct a PDA that accepts L= {0 1 | n ≥
0} Solution
0,ε→0 1,0→ε
n n
PDA for L= {0 1 | n≥0}
This language accepts L = {ε, 01, 0011, 000111, ............................. }
Here, in this example, the number of ‘a’ and ‘b’ have to be same.
Then at state q2, if we encounter input 0 and top is Null, we push 0 into stack.
This may iterate. And if we encounter input 1 and top is 0, we pop this 0.
Then at state q3, if we encounter input 1 and top is 0, we pop this 0. This may
also iterate. And if we encounter input 1 and top is 0, we pop the top element.
70
Automata Theory
If the special symbol ‘$’ is encountered at top of the stack, it is popped out and it
finally goes to the accepting state q4.
Example
R
Construct a PDA that accepts L= { ww | w =
(a+b)* } Solution
a, ε →a a,a → ε
b, ε →b b, b→ ε
R
PDA for L= {ww | w = (a+b)*}
Initially we put a special symbol ‘$’ into the empty stack. At state q2, the w is being
read. In state q3, each 0 or 1 is popped when it matches the input. If any other input is
given, the PDA will go to a dead state. When we reach that special symbol ‘$’, we go to
the accepting state q4.
71
25. PDA & Context-Free GrammarAutomata Theory
L(G) = L(P)
In the next two topics, we will discuss how to convert from PDA to CFG and vice versa.
Step 3 The start symbol of CFG will be the start symbol in the PDA.
Step 4 All non-terminals of the CFG will be the stack symbols of the PDA and all
the terminals of the CFG will be the input symbols of the PDA.
Step 5 For each production in the form A→ aX where a is terminal and A, X are
combination of terminal and non-terminals, make a transition δ (q, a, A).
Problem
Construct a PDA from the following CFG.
S → XS | , A → aXb | Ab | ab
Solution
where δ:
72
Automata Theory
δ(q, a, a) = {(q, )}
δ(q, 1, 1) = {(q, )}
Step 1 For every w, x, y, z ∈ Q, m ∈ S and a, b ∈ Σ, if δ (w, a, ) contains (y, m) and (z, b, m) contains (x,
), add the production rule Xwx → a Xyzb in grammar G.
Step 2 For every w, x, y, z ∈ Q, add the production rule Xwx → XwyXyx in grammar G.
Step 3 For w ∈ Q, add the production rule Xww→ in grammar G.
73
26. Pushdown Automata and ParsingAutomata Theory
Parsing is used to derive a string using the production rules of a grammar. It is used to
check the acceptability of a string. Compiler is used to check whether or not a string is
syntactically correct. A parser takes the inputs and builds a parse tree.
Top-Down Parser: Top-down parsing starts from the top with the start-symbol
and derives a string using a parse tree.
Bottom-Up Parser: Bottom-up parsing starts from the bottom with the string
and comes to the start symbol using a parse tree.
Pop the non-terminal on the left hand side of the production at the top of the
stack and push its right-hand side string.
If the top symbol of the stack matches with the input symbol being read, pop it.
If the input string is fully read and the stack is empty, go to the final state ‘F’.
Example
Design a top-down parser for the expression "x+y*z" for the grammar G with the
following production rules:
Solution
If the PDA is (Q, Σ, S, δ, q0, I, F), then the top-down parsing is:
(x+y*z, I) ⊢(x +y*z, SI) ⊢ (x+y*z, S+XI) ⊢(x+y*z, X+XI)
⊢(x+y*z, Y+X I) ⊢(x+y*z, x+XI) ⊢(+y*z, +XI) ⊢ (y*z, XI)
⊢(y*z, X*YI) ⊢(y*z, y*YI) ⊢(*z,*YI) ⊢(z, YI) ⊢(z, zI) ⊢(ε, I)
74
Automata Theory
Replace the right-hand side of a production at the top of the stack with its left-
hand side.
If the top of the stack element matches with the current input symbol, pop it.
If the input string is fully read and only if the start symbol ‘S’ remains in the
stack, pop it and go to the final state ‘F’.
Example
Design a top-down parser for the expression "x+y*z" for the grammar G with the
following production rules:
Solution
If the PDA is (Q, Σ, S, δ, q0, I, F), then the bottom-up parsing is:
(x+y*z, I) ⊢ (+y*z, xI) ⊢ (+y*z, YI) ⊢ (+y*z, XI) ⊢ (+y*z, SI)
⊢ (y*z, +SI) ⊢ (*z, y+SI) ⊢ (*z, Y+SI) ⊢ (*z, X+SI) ⊢ (z, *X+SI)
⊢ (ε, z*X+SI) ⊢ (ε, Y*X+SI) ⊢ (ε, X+SI) ⊢ (ε, SI)
75
Automata Theory
Turing Machine
76
27. Turing Machine IntroductionAutomata Theory
Definition
A Turing Machine (TM) is a mathematical model which consists of an infinite length tape
divided into cells on which input is given. It consists of a head which reads the input
tape. A state register stores the state of the Turing machine. After reading an input
symbol, it is replaced with another symbol, its internal state is changed, and it moves
from one cell to the right or left. If the TM reaches the final state, the input string is
accepted, otherwise rejected.
77
Automata Theory
q0= {q0}
B = blank symbol
F = {qf }
δ is given by:
Here the transition 1Rq1 implies that the write symbol is 1, the tape moves right, and
the next state is q1. Similarly, the transition 1Lq2 implies that the write symbol is 1, the
tape moves left, and the next state is q2.
S(n) = O(n)
78
28. Accepted Language & Decided LanguageAutomataThory
A TM accepts a language if it enters into a final state for any input string w. A language
is recursively enumerable (generated by Type-0 grammar) if it is accepted by a Turing
machine.
A TM decides a language if it accepts it and enters into a rejecting state for any input not
in the language. A language is recursive if it is decided by a Turing machine.
There may be some cases where a TM does not stop. Such TM accepts the language, but
it does not decide it.
Example 1
Design a TM to recognize all strings consisting of an odd number of α’s.
Solution
From the above moves, we can see that M enters the state q1 if it scans an even number of α’s, and it enters the state q2 if it scans an odd number of α’s. Hence
q2 is the only accepting state.
Hence,
M = {{q1, q2}, {1}, {1, B}, δ, q1, B, {q2}} where δ is given by:
Tape alphabet
Present State ‘q1’ Present State ‘q2’
symbol
α BRq2 BRq1
79
Automata Theory
Example 2
Design a Turing Machine that reads a string representing a binary number and erases all
leading 0’s in the string. However, if the string comprises of only 0’s, it keeps one 0.
Solution
Let us assume that the input string is terminated by a blank symbol, B, at each end of
the string.
If M is in q1, on reading 0, it moves right and erases 0, i.e., it replaces 0’s by B’s.
On reaching the leftmost 1, it enters q2 and moves right. If it reaches B, i.e., the
string comprises of only 0’s, it moves left and enters the state q3.
If M is in q3, it replaces B by 0, moves left and reaches the final state qf.
Hence,
M = {{q0, q1, q2, q3, q4, qf}, {0,1, B}, {1, B}, δ, q0, B, {qf}} where δ is given by:
Tape
Present Present Present Present Present
alphabet
State ‘q0’ State ‘q1’ State ‘q2’ State ‘q3’ State ‘q4’
symbol
80
29. Multi-tape Turing MachineAutomata Theory
Multi-tape Turing Machines have multiple tapes where each tape is accessed with a
separate head. Each head can move independently of the other heads. Initially the input
is on tape 1 and others are blank. At first, the first tape is occupied by the input and the
other tapes are kept blank. Next, the machine reads consecutive symbols under its
heads and the TM prints a symbol on each tape and moves its heads.
En
En
En
Head
k k
δ: Q ×X →Q× (X× {Left_shift, Right_shift, No_shift })
Note: Every Multi-tape Turing machine has an equivalent single-tape Turing machine.
81
30. Multi-track Turing MachineAutomata Theory
δ(Qi, [a1, a2, a3,....]) = (Qj, [b1, b2, b3,....], Left_shift or Right_shift)
82
31. Non-Deterministic Turing machineAutomata Theory
In a Non-Deterministic Turing Machine, for every state and symbol, there are a group of
actions the TM can have. So, here the transitions are not deterministic. The computation
of a non-deterministic Turing Machine is a tree of configurations that can be reached
from the start configuration.
An input is accepted if there is at least one node of the tree which is an accept
configuration, otherwise it is not accepted. If all branches of the computational tree halt
on all inputs, the non-deterministic Turing Machine is called a Decider and if for some
input, all branches are rejected, the input is also rejected.
δ is a transition function;
83
32. Semi-infinite Tape Turing MachineAutomata Theory
A Turing Machine with a semi-infinite tape has a left end but no right end. The left end is
limited with an end marker.
En
Head
It is a two-track tape:
1. Upper track: It represents the cells to the right of the initial head position.
2. Lower track: It represents the cells to the left of the initial head position in reverse
order.
The infinite length input string is initially written on the tape in contiguous tape cells.
The machine starts from the initial state q0 and the head scans from the left end marker
‘End’. In each step, it reads the symbol on the tape under its head. It writes a new
symbol on that tape cell and then it moves the head either into left or right one tape
cell. A transition function determines the actions to be taken.
It has two special states called accept state and reject state. If at any point of time it
enters into the accepted state, the input is accepted and if it enters into the reject state,
the input is rejected by the TM. In some cases, it continues to run infinitely without
being accepted or rejected for some certain input symbols.
Note: Turing machines with semi-infinite tape are equivalent to standard Turing
machines.
84
33. Linear Bounded AutomataAutomata Theory
Here,
The computation is restricted to the constant bounded area. The input alphabet contains
two special symbols which serve as left end markers and right end markers which mean
the transitions neither move to the left of the left end marker nor to the right of the right
end marker of the tape.
A linear bounded automaton can be defined as an 8-tuple (Q, X, Σ, q0, ML, MR, δ, F) where:
δ is a transition function which maps each pair (state, tape symbol) to (state,
tape symbol, Constant ‘c’) where c can be 0 or +1 or -1
End End
85
Automata Theory
Decidability
86
Automata Theory
34. Language Decidability
Turing acceptable
languages
Decidable
languages
For a decidable language, for each input string, the TM halts either at the accept or the
reject state as depicted in the following diagram:
Decision on Halt
Rejected
Input
Accepted
Turing Machine
Example 1
Find out whether the following problem is decidable or not:
Solution
87
Automata Theory
Divide the number ‘m’ by all the numbers between ‘2’ and ‘√m’ starting from ‘2’.
If any of these numbers produce a remainder zero, then it goes to the “Rejected
state”, otherwise it goes to the “Accepted state”. So, here the answer could be
made by ‘Yes’ or ‘No’.
Example 2
Given a regular language L and string w, how can we check if w∈ L?
Solution
w∉ L
Qr
Input
string w
Qi
Qf w∈ L
DFA
Note:
88
35. Undecidable Languages Automata Theory
For an undecidable language, there is no Turing Machine which accepts the language and
makes a decision for every input string w (TM can make decision for some input string
though). A decision problem P is called “undecidable” if the language L of all yes
instances to P is not decidable. Undecidable languages are not recursive languages, but
sometimes, they may be recursively enumerable languages.
Undecidable languages
Decidable
languages
Example:
89
36. Turing Machine Halting ProblemAutoata Theory
Problem: Does the Turing machine finish computing of the string w in a finite number
of steps? The answer must be either yes or no.
Proof: At first, we will assume that such a Turing machine exists to solve this problem
and then we will show it is contradicting itself. We will call this Turing machine as a
Halting machine that produces a ‘yes’ or ‘no’ in a finite amount of time. If the halting
machine finishes in a finite amount of time, the output comes as ‘yes’, otherwise as ‘no’.
The following is the block diagram of a Halting machine:
Infinite loop
Yes
Qi Qj
Input Halting
string Machine
No
90
37. Rice Theorem Automata Theory
Theorem:
L= {<M> | L (M) ∈ P} is undecidable when p, a non-trivial property of the Turing machine, is undecidable.
Property 2: For some M1 and M2 such that <M1> ∈ L and <M2> ∉ L Proof:
If X accepts w, then
91
38. Post Correspondence ProblemAutomata Theory
Example 1
Find whether the lists
Solution
x1 x2 x3
M Abb aa aaa
N Bba aaa aa
Here,
x2 x1x3 = ‘aaabbaaa’
x2 x1x3 = y2y1 y3
92
Automata Theory
Example 2
Find whether the lists M = (ab, bab, bbaaa) and N = (a, ba, bab) have a Post
Correspondence Solution?
Solution
x1 x2 x3
M ab bab bbaaa
N a ba bab
93