Automata Theory
Automata Theory
An automaton with a finite number of states is called a Finite Automaton FA or Finite State
Machine FSM.
q0 is the initial state from where any input is processed (q0 Q).
Related Terminologies
Alphabet
Definition An alphabet is any finite set of 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
If S=cabcad, |S|= 6
Kleene Star
Definition The set * is the infinite set of all possible strings of all possible lengths over
including .
Representation * = 0 1 2 .
Kleene Closure/Plus
Definition The set + is the infinite set of all possible strings of all possible lengths
over excluding .
Representation + = 0 1 2 .
+ = * { }
Language
Example If the language takes all possible strings of length 2 over = {a, b}, then L = {
ab, bb, ba, bb}
Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js
DETERMINISTIC FINITE AUTOMATON
http://www.tutorialspoint.com/automata_theory/deterministic_finite_automaton.htm Copyright tutorialspoint.com
Example
Q = {a, b, c},
= {0, 1},
q0 ={a},
F={c}, and
Transition function as shown by the following table
Present State Next State for Input 0 Next State for Input 1
A a b
B c a
C b c
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 the transition function where : Q { } 2Q (Here the power set of Q (2Q) 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).
Example
Q = {a, b, c}
= {0, 1}
q0 = {a}
F={c}
The transition function as shown below
Present State Next State for Input 0 Next State for Input 1
a a, b b
b c a, c
c b, c c
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
Moore Machine The output depends both on the current input as well as the current state.
*(q0, S) F
{S | S * and *(q0, S) F}
*(q0, S) F
{S | S * and *(q0, S) F}
Example
Let us consider the DFA shown in Figure 1.3. From the DFA, the acceptable strings can be derived.
Strings accepted by the above DFA {0, 00, 11, 010, 101, ...........}
Strings not accepted by the above DFA {1, 011, 111, ........}
Loading [MathJax]/jax/output/HTML-CSS/jax.js
NDFA TO DFA CONVERSION
http://www.tutorialspoint.com/automata_theory/ndfa_to_dfa_conversion.htm Copyright tutorialspoint.com
Problem Statement
Let X = (Qx, , x, q0, F x) be an NDFA which accepts the language LX. We have to design an
equivalent DFA Y = (Qy, , y, q0, F y) such that LY = LX. 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 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
The NDFA table is as follows
q q, 0 q, 1
a {a,b,c,d,e} {d,e}
b {c} {e}
c {b}
d {e}
q q, 0 q, 1
a {a,b,c,d,e} {d,e}
{d,e} e D
{b,d,e} {c,e} E
d e
{c,e} B
b c E
c B
Loading [MathJax]/jax/output/HTML-CSS/jax.js
DFA MINIMIZATION
http://www.tutorialspoint.com/automata_theory/dfa_minimization.htm Copyright tutorialspoint.com
Algorithm
Input: DFA
Step 1 Draw a table for all pairs of states (Qi, 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].
Step 3
Repeat this step until we cannot mark anymore 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 above algorithm to minimize the DFA shown below.
a b c d e f
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
After step 3, we have got state combinations {a, b} {c, d} {c, e} {d, e} that are unmarked.
So the final minimized DFA will contain three states {f}, {a, b} and {c, d, e}
DFA Minimization using Equivalence Theorem
If X and Y are two states in a DFA, we can combine these two states into {X, Y} if they are not
distinguishable. Two states are distinguishable, if there is at least one string S, such that one of
X, S and Y, S is accepting and another is not accepting. Hence, a DFA is minimal if and only if all
the states are distinguishable.
Algorithm
Step 1 All the states Q are divided in two partitions final states and non-final states and
are denoted by P0. All the states in a partition are 0th equivalent. Take a counter k
and initialize it with 0.
Step 2 Increment k by 1. For each partition in Pk, 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.
Step 4 Combine kth equivalent sets and make them the new states of the reduced DFA.
Example
Let us consider the following DFA
q q, 0 q, 1
a b c
b a d
c e f
d e f
e e f
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
a, b a, b c, d, e
c, d, e c, d, e f
f f f
Loading [MathJax]/jax/output/HTML-CSS/jax.js
MOORE & MEALY MACHINES
http://www.tutorialspoint.com/automata_theory/moore_and_mealy_machines.htm Copyright tutorialspoint.com
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.
q0 is the initial state from where any input is processed (q0 Q).
Moore Machine
Moore machine is an FSM whose outputs depend on only the present state.
q0 is the initial state from where any input is processed (q0 Q).
The state diagram of a Moore Machine is shown below
Output depends both upon present Output depends only upon the present state.
state and present input.
Generally, it has fewer states than Generally, it has more states than Mealy Machine.
Moore Machine.
Output changes at the clock edges. Input change can cause change in output change as soon
as logic is done.
Mealy machines react faster to In Moore machines, more logic is needed to decode the
inputs. outputs since it has more circuit delays.
Algorithm 4
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 Qi output is m, copy it into the output columns of the Mealy
Machine state table wherever Qi appears in the next state..
Example
Let us consider the following Moore machine
Next State
Present State Output
Present State Output
a=0 a=1
a d b 1
b a d 0
c c c 0
d b a 1
Step 1 & 2
Next State
a d b
B a d
c c c
d b a
Step 3
Next State
a d 1 b 0
b a 1 d 1
c c 0 c 0
d b 0 a 1
Algorithm 5
Step 1 Calculate the number of different outputs for each state (Qi) 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
a d 0 b 1
b a 1 d 0
c c 1 c 0
d b 0 a 1
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 1and0. So, we divide b into b0, b1 and c into c0, c1.
Next State
Present State Output
a=0 a=1
a d b1 1
b0 a d 0
b1 a d 1
c0 c1 C0 0
c1 c1 C0 1
d b0 a 0
Loading [MathJax]/jax/output/HTML-CSS/jax.js
INTRODUCTION TO GRAMMARS
http://www.tutorialspoint.com/automata_theory/introduction_to_grammars.htm Copyright tutorialspoint.com
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
Example
Grammar G1
S, A, B, a, b, S, S AB, A a, B b
Here,
Productions, P : S AB, A a, B b
Example
Grammar G2
S, A, a, b, S, S aAb, aA aaAb, A
Here,
is an empty string.
xyGxy
Example
Let us consider the grammar
G2 = S, A, a, b, S, S aAb, aA aaAb, A
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
LG={W|W *, S G W}
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.,
LG = {ab}
Example
Suppose we have the following grammar
LG = {ab, a 2 b, ab2 , a 2 b2 , }
Example
Problem Suppose, L G = {a m bn | m 0 and n > 0}. We have to find out the grammar G which
produces LG.
Solution
Here, the start symbol has to take at least one b preceded by any number of a including null.
To accept the string set {b, ab,bb, aab, abb, .}, we have taken the productions
S aS , S B, B b and B bB
S B b Accepted
S B bB bb Accepted
S aS aB ab Accepted
Thus, we can prove every single string in LG is accepted by the language generated by the
production set.
G: S, A, B, a, b, S, S aS | B, B b | bB
Example
Problem Suppose, L G = {a m bn | m> 0 and n 0}. We have to find out the grammar G which
produces LG.
Solution
Since LG = {a m bn | m> 0 and n 0}, the set of strings accepted can be rewritten as
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
S aA aB a a Accepted
S aA aB abB ab ab Accepted
Thus, we can prove every single string in LG is accepted by the language generated by the
production set.
G: S, A, B, a, b, S, S aA, A aA | B, B | bB
Processing math: 100%
CHOMSKY CLASSIFICATION OF GRAMMARS
http://www.tutorialspoint.com/automata_theory/chomsky_classification_of_grammars.htm
Copyright tutorialspoint.com
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
Take a look at the following illustration. It shows the scope of each type of grammar
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.
where X, Y N Nonterminal
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
X aY
Type - 2 Grammar
Type-2 grammars generate context-free languages.
where A N Nonterminal
Example
S X a
X a
X aX
X abc
X
Type - 1 Grammar
Type-1 grammars generate context-sensitive languages. The productions must be in the form
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
aD Db
Loading [MathJax]/jax/output/HTML-CSS/jax.js
REGULAR EXPRESSIONS
http://www.tutorialspoint.com/automata_theory/regular_expressions.htm Copyright tutorialspoint.com
Regular Expressions
A Regular Expression can be recursively defined as follows
If we apply any of the rules several times from 1 to 5, they are Regular Expressions.
Some RE Examples
a + b*abb Set of strings of as and bs ending with the string abb, So L =
{abb, aabb, babb, aaabb, ababb, ..}
aa + ab + ba + bb* String of as and bs of even length can be obtained by
concatenating any combination of the strings aa, ab, ba and bb
including null, so L= {aa, ab, ba, bb, aaab, aaba, ..}
Loading [MathJax]/jax/output/HTML-CSS/jax.js
REGULAR SETS
http://www.tutorialspoint.com/automata_theory/regular_sets.htm Copyright tutorialspoint.com
Regular Sets
Any set that represents the value of the Regular Expression is called a Regular Set.
Proof
StringsofallpossiblelengthsexcludingNull
RE (L1 L2 ) = a * whichisaregularexpressionitself
Hence, proved.
Proof
Hence, proved.
Proof
RE = aa*
Proof
StringsofalloddlengthsexcludingNull
Hence, proved.
Proof
Hence, proved.
Proof
i.e., RE L = a aa*
RE (L*) = a a*
Hence, proved.
Proof
Then, L1 L2 = {001,0010,0011,0001,00010,00011,1001,10010,.............}
Set of strings containing 001 as a substring which can be represented by an RE: 0 + 1 *001
0 + 1 *
Hence, proved.
* =
* =
R*R* = R*
(R*)* = R*
RR* = R*R
PQ*P =PQP *
Arden's Theorem
In order to find out a regular expression of a Finite Automaton, we use Ardens 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
When we put the value of R recursively again and again, we get the following equation
R = Q ( + P + P2 + P3 + . )
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 .
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
Solution
Here the initial state is q2 and the final state is q1.
q2 = q1 b + q2 b + q3 b
q3 = q2 a
q2 = q1 b + q2 b + q3 b
= q1 b + q2 b + ab
q1 = q1 a + q3 a +
= q1 a + q2 aa + (Substituting value of q3 )
= q1 a + b(b + ab*aa) +
= a + b(b + ab*aa)*
= a + b(b + ab*aa)*
Problem
q1 = q1 0 +
q2 = q1 1 + q2 0
q3 = q2 1 + q3 0 + q3 1
q1 = 0* [As, R = R]
So, q1 = 0*
q2 = 0*1 + q20
Construction of an FA from an RE
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.
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
Now we will remove the transitions. After we remove the transitions from the NDFA, we get the
following
It is an NDFA corresponding to the RE: 1 0 + 1* 0. If you want to convert it into a DFA, simply apply
the method of converting NDFA to DFA discussed in Chapter 1.
a transition function : Q 2Q
q0 an initial state q0 Q
Problem
Convert the following NFA- to NFA without Null move.
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
Step 3
So the FA becomes
Step 4
So the FA becomes
Loading [MathJax]/jax/output/HTML-CSS/jax.js
PUMPING LEMMA FOR REGULAR GRAMMARS
http://www.tutorialspoint.com/automata_theory/pumping_lemma_for_regular_grammar.htm
Copyright tutorialspoint.com
Theorem
Let L be a regular language. Then there exists a constant c such that for every string w in L
|w| c
|y| > 0
|xy| c
Problem
Solution
Number of as = p + 2q + r = p + q + r + q = n + q
Complement of a DFA
If (Q, , , q0 , F) be a DFA that accepts a language L, then the complement of the DFA can be
obtained by swapping its accepting states with its non-accepting states and vice versa.
= {a, b}
So, RE = a +.
Now we will swap its accepting states with its non-accepting states and vice versa and will get the
following
= { , b, ab ,bb,ba, ............... }
over the alphabet
= {a, b}
Note If we want to complement an NFA, we have to first convert it to DFA and then have to swap
states as in the previous method.
Loading [MathJax]/jax/output/HTML-CSS/jax.js
DFA COMPLEMENT
http://www.tutorialspoint.com/automata_theory/dfa_complement.htm Copyright tutorialspoint.com
If (Q, , , q0 , F) be a DFA that accepts a language L, then the complement of the DFA can be
obtained by swapping its accepting states with its non-accepting states and vice versa.
= {a, b}
So, RE = a +.
Now we will swap its accepting states with its non-accepting states and vice versa and will get the
following
= { , b, ab ,bb,ba, ............... }
= {a, b}
Note If we want to complement an NFA, we have to first convert it to DFA and then have to swap
states as in the previous method.
CONTEXT-FREE GRAMMAR INTRODUCTION
http://www.tutorialspoint.com/automata_theory/context_free_grammar_introduction.htm
Copyright tutorialspoint.com
P is a set of rules, P: N N T *, i.e., the left-hand side of the production rule P does have
any right context or left context.
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
Root vertex Must be labeled by the start symbol.
If S x1 x2 xn is a production rule in a CFG, then the parse tree / derivation tree will be as
follows
Top-down Approach
Bottom-up Approach
Example
Example
Example
If a context free grammar G has more than one derivation tree for some string w LG, it is called
an ambiguous grammar. There exist multiple right-most or left-most derivations for some string
generated from that grammar.
Problem
is ambiguous or not.
Solution
Lets find out the derivation tree for the string "a+a*a". It has two leftmost derivations.
Derivation 1
Parse tree 1
Derivation 2
Parse tree 2
As there are two parse trees for a single string "a+a*a", the grammar G is ambiguous.
Loading [MathJax]/jax/output/HTML-CSS/jax.js
CFL CLOSURE PROPERTY
http://www.tutorialspoint.com/automata_theory/cfl_closure_properties.htm Copyright tutorialspoint.com
Union
Concatenation
Kleene Star operation
Intersection
Intersection with Regular Language
Complement
CFG SIMPLIFICATION
http://www.tutorialspoint.com/automata_theory/cfg_simplification.htm Copyright tutorialspoint.com
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.
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 } { S } from rule S AC
W3 = { A, C, E, S }
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 }
Y 2 = { S, A, C } from rule S AC
Y 4 = { S, A, C, a, c }
G = { { A, C, S }, { a, c }, P, {S}}
where P: S AC, A a, C c
Removal Procedure
Step 3 Repeat from step 1 until all unit productions are removed.
Problem
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
Now we will remove Y Z.
S XY, X a, Y a | b, Z a, M a, N a
S XY, X a, Y a | b
Removal Procedure
Step 3 Combine the original productions with the result of step 2 and remove -
productions.
Problem
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
A CFG is in Chomsky Normal Form if the Productions are in the following forms
Aa
A BC
S
Step 1 If the start symbol S occurs on some right side, create a new start symbol S and
a new production S S.
Step 4 Replace each production A B1Bn 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
S ASA | aB, A B | S, B b |
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
S0 S, S ASA | aB, A B | S, B b |
B and A
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
S0 S, S ASA | aB | a | AS | SA, A B | S, B b
After removing S0 S, the production set becomes
A B | S, B b
AS|b
Bb
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
Bb
X SA
S0 AX | YB | a | AS | SA
S AX | YB | a | AS | SA
A b |AX | YB | a | AS | SA
Bb
X SA
Ya
Loading [MathJax]/jax/output/HTML-CSS/jax.js
GREIBACH NORMAL FORM
http://www.tutorialspoint.com/automata_theory/greibach_normal_form.htm Copyright tutorialspoint.com
A CFG is in Greibach Normal Form if the Productions are in the following forms
Ab
A bD1 Dn
Step 1 If the start symbol S occurs on some right side, create a new start symbol S and
a new production S S.
Step 5 Do proper substitutions of productions to convert it into the proper form of GNF.
Problem
Convert the following CFG into CNF
S XY | X n | 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
Now after replacing
X in S XY | Xo | p
with
mX | m
we obtain
S mXY | mY | mXo | mo | p.
X in Y X n | o
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
Oo
Pp
Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js
PUMPING LEMMA FOR CFG
http://www.tutorialspoint.com/automata_theory/pumping_lemma_for_cfg.htm Copyright tutorialspoint.com
Lemma
If L is a context-free language, there is a pumping length p such that any string w L of length
p can be written as w = uvxyz, where vy , |vxy| p, and for all i 0, uvixyiz L.
Problem
Solution
|vwx| n and vx .
Hence vwx cannot involve both 0s and 2s, since the last 0 and the first 2 are at least n + 1 positions
apart. There are two cases
Case 1 vwx has no 2s. Then vx has only 0s and 1s. Then uwy, which would have to be in L, has
n 2s, but fewer than n 0s or 1s.
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.
is input alphabet
S is stack symbols
The following diagram shows a transition in a PDA from a state q1 to state q2 , labeled as a,b c
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
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 "".
Consider a PDA (Q, , S, , q0 , I, F). A transition can be mathematically represented by the following
turnstile notation
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.
Loading [MathJax]/jax/output/HTML-CSS/jax.js
PUSHDOWN AUTOMATA ACCEPTANCE
http://www.tutorialspoint.com/automata_theory/pushdown_automata_acceptance.htm Copyright tutorialspoint.com
For a PDA (Q, , S, , q0 , I, F), the language accepted by the set of final states F is
LPDA = {w | (q0 , w, I) * q, , x, q F}
For a PDA (Q, , S, , q0 , I, F), the language accepted by the empty stack is
LPDA = {w | (q0 , w, I) * q, , , q Q}
Example
Solution
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.
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
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.
Loading [MathJax]/jax/output/HTML-CSS/jax.js
PDA & CONTEXT-FREE GRAMMAR
http://www.tutorialspoint.com/automata_theory/pda_context_free_grammar.htm Copyright tutorialspoint.com
If a grammar G is context-free, we can build an equivalent nondeterministic PDA which accepts the
language that is produced by the context-free grammar G. A parser can be built for the grammar
G.
LG = LP
In the next two topics, we will discuss how to convert from PDA to CFG and vice versa.
Input A CFG, G= V, T, P, S
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
G = S, X, a, b, P, S
S XS | , A aXb | Ab | ab
Solution
P = q, a, b, a, b, X, S, , q, S
where
q, , S = {q, XS, q, }
q, a, a = {q, }
q, 1, 1 = {q, }
Input A CFG, G= V, T, P, S
Output Equivalent PDA, P = (Q, , S, , q0 , I, F) such that the non- terminals of the
grammar G will be {X wx | w,x Q} and the start state will be A q0 ,F.
Loading [MathJax]/jax/output/HTML-CSS/jax.js
PUSHDOWN AUTOMATA & PARSING
http://www.tutorialspoint.com/automata_theory/pda_and_parsing.htm Copyright tutorialspoint.com
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
P: S S+X | X, X X*Y | Y, Y S | id
Solution
x + y z, I x + y z, SI x + y z, S + XI x + y z, X + XI
x + y z, Y + XI 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
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
P: S S+X | X, X X*Y | Y, Y S | id
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
, z X + SI , Y X + SI , X + SI , SI
Loading [MathJax]/jax/output/HTML-CSS/jax.js
TURING MACHINE INTRODUCTION
http://www.tutorialspoint.com/automata_theory/turing_machine_introduction.htm Copyright tutorialspoint.com
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.
Q = {q0 , q1 , q2 , qf}
X = {a, b}
= {1}
q0 = {q0 }
B = blank symbol
F = {qf }
is given by
Tape alphabet symbol Present State q0 Present State q1 Present State q2
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 .
Tn = Onlogn
Sn = On
Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js
ACCEPTED LANGUAGE & DECIDED LANGUAGE
http://www.tutorialspoint.com/automata_theory/accepted_and_decided_language.htm Copyright tutorialspoint.com
A TM accepts a language if it enters into a final state for any input string w. A language is
recursively enumerable generatedbyType 0grammar 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
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,
where is given by
a BRq2 BRq1
Example 2
Design a Turing Machine that reads a string representing a binary number and erases all leading
0s in the string. However, if the string comprises of only 0s, 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 q0, on reading 0, it moves right, enters the state q1 and erases 0. On reading 1, it
enters the state q2 and moves right.
If M is in q1, on reading 0, it moves right and erases 0, i.e., it replaces 0s by Bs. On reaching
the leftmost 1, it enters q2 and moves right. If it reaches B, i.e., the string comprises of only
0s, it moves left and enters the state q3.
If M is in q2, on reading either 0 or 1, it moves right. On reaching B, it moves left and enters
the state q4. This validates that the string comprises only of 0s and 1s.
If M is in q3, it replaces B by 0, moves left and reaches the final state qf.
If M is in q4, on reading either 0 or 1, it moves left. On reaching the beginning of the string,
i.e., when it reads B, it reaches the final state qf.
Hence,
where is given by
Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js
MULTI-TAPE TURING MACHINE
http://www.tutorialspoint.com/automata_theory/multi_tape_turing_machine.htm Copyright tutorialspoint.com
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.
Note Every Multi-tape Turing machine has an equivalent single-tape Turing machine.
Loading [MathJax]/jax/output/HTML-CSS/jax.js
MULTI-TRACK TURING MACHINE
http://www.tutorialspoint.com/automata_theory/multi_track_turing_machine.htm Copyright tutorialspoint.com
Multi-track Turing machines, a specific type of Multi-tape Turing machine, contain multiple tracks
but just one tape head reads and writes on all tracks. Here, a single tape head reads n symbols
from n tracks at one step. It accepts recursively enumerable languages like a normal single-track
single-tape Turing Machine accepts.
Note For every single-track Turing Machine S, there is an equivalent multi-track Turing Machine
M such that LS = LM.
Loading [MathJax]/jax/output/HTML-CSS/jax.js
NON-DETERMINISTIC TURING MACHINE
http://www.tutorialspoint.com/automata_theory/non_deterministic_turing_machine.htm
Copyright tutorialspoint.com
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;
: Q X PQ X Leftshift, Rightshift.
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.
It is a two-track tape
Upper track It represents the cells to the right of the initial head position.
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.
LINEAR BOUNDED AUTOMATA
http://www.tutorialspoint.com/automata_theory/linear_bounded_automata.htm Copyright tutorialspoint.com
A linear bounded automaton is a multi-track non-deterministic Turing machine with a tape of some
bounded finite length.
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, tapesymbol to (state, tape symbol, Constant
c) where c can be 0 or +1 or -1
A deterministic linear bounded automaton is always context-sensitive and the linear bounded
automaton with empty language is undecidable.
Loading [MathJax]/jax/output/HTML-CSS/jax.js
LANGUAGE DECIDABILITY
http://www.tutorialspoint.com/automata_theory/language_decidability.htm Copyright tutorialspoint.com
A language is called Decidable or Recursive if there is a Turing machine which accepts and halts
on every input string w. Every decidable language is Turing-Acceptable.
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
Example 1
Find out whether the following problem is decidable or not
Is a number m prime?
Solution
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.
Hence, it is a decidable problem.
Example 2
Given a regular language L and string w, how can we check if w L?
Solution
Note
For an undecidable language, there is no Turing Machine which accepts the language and makes
a decision for every input string w TMcanmakedecisionforsomeinputstringthough. 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.
Example
The halting problem of Turing machine
The mortality problem
The mortal matrix problem
The Post correspondence problem, etc.
Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js
TURING MACHINE HALTING PROBLEM
http://www.tutorialspoint.com/automata_theory/turing_machine_halting_problem.htm Copyright tutorialspoint.com
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
Theorem
L= {<M> | L M P} is undecidable when p, a non-trivial property of the Turing machine, is
undecidable.
If X accepts w, then
The Post Correspondence Problem PCP, introduced by Emil Post in 1946, is an undecidable
decision problem. The PCP problem over an alphabet is stated as follows
M= (x1 , x2 , x3 ,, xn )
N= (y1 , y2 , y3 ,, yn )
We can say that there is a Post Correspondence Solution, if for some i1 , i2 , ik, where 1 ij
n, the condition xi1.xik = yi1....yik satisfies.
Example
Find whether the lists
Solution
x1 x2 x3
M Abb aa aaa
N Bba aaa aa
Here,
x2x1x3 = aaabbaaa
x2x1x3 = y2y1y3
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