Automata Compiler Desinger Notes
Automata Compiler Desinger Notes
========================================================================
Introduction to Finite Automata _Fundamentals – Terminology
1. Symbol
Something that has some meaning.
Can’t be further divided.
Eg: { a, b, 0, 1, +, ?, :, +, ++ }
2. Alphabet
Has finite set of symbols?
Denoted by ∑
Eg : ∑ = { 0, 1 } ∑ bin = { 0, 1}
∑ = { a, b } ∑ dec = { 0, 1 . . .9 }
∑ = {[ #, @ } ∑ empty = Ø
∑ { 0. . .9 }
∑ eng = { A, B, …..Z, a, b . . .z }
DR. V SREENIVASULU 1
3. String / Word / sentence
Finite sequence of symbols over ∑
Denoted by w.
∑ = { 0, 1 }
W = 101, w = 1100, w = 111 00 11
∑ { a, b }
W = aba, w = bab, w = aabbaa
4. String operations
Empty string is denoted by ε / л / λ
String length: * # of symbols in the word.
Denoted by |w|
5. Concatenation: with Є
Є. W = w. Є = w.
Eg : w = 110110 w = Є 011100 Є
|w|= 6 |w| =6
w = Є 00 ЄЄ 011 Є 00
|w| = 7
Don’t consider Є while calculating length of a string.
6. Prefix
The first letter of each string is fixed. But ending may be anywhere.
Eg: w = abcd
Prefix(w) = {Є, a, ab, abc, abcd}
7. Suffix
The last letter of each string is fixed. But start may be anywhere.
Eg: w = abcd
Suffix(w) = {Є, d, cd, bcd, abcd}
8. Subsequence
Starting and ending is anywhere.
Eg: w = abcd
Subsequence(w)= { Є, a, b, c, d, ab, ac, ad, bc, bd, cd, abc, abd, bcd,
abcd}
9. Substring
Letters of consecutive symbols.
Eg: w = abcd
Substrings(w) = {Є, a, b, c, d, ab, bc, cd, abc, bcd, abcd}
DR. V SREENIVASULU 2
10. Concatenation
Concatenation of two strings w1 and w2
W1 is followed by w2 without any space b/w them
Eg : w1 = Success w2 = fully
w1.w2 = successfully
12.Palindrome
Can be read w either side identically.
Eg : w = { 0, 1 }
w = 0110, w = 10001, w = 111000111
∑ *∑ *= ∑ *
= ∑ ++ ∑ 0 = ∑ *
∑++Є=∑*
DR. V SREENIVASULU 3
16. Set operation:
Set is a collection of distinct objects.
Set is denoted by S.
20. Intersection
Common elements from the given sets.
A = {1, 2, 3, 4, 5} B = {2, 6, 7, 8} A ∩ B = {2}
21. Union
Unique elements from the given sets.
A U B = {1, 2, 3, 4, 5, 6, 7, 8}
DR. V SREENIVASULU 4
Infinite Language: Infinite number of elements in the set.
Eg: All strings of starts with a.
{a, ab, aa, aab, aab, aba,… ...... }
∑ = {a, b } → L 1 = { a, aa, ab, aab, abb ........... }
L2= { } → Ф
L3= { ε }
L 4 = {a, b } → { ε, ab, aabb, aaabb .......}
L 5 = {an | n>= 0 } { a0, a1, a2, a3, ....... }
L 6 = {an bm | n, m >= 1 }
= {ab, aab, abb, aaab, ......... }
23.Automata
Mathematical model of computation.
Abstract machine to perform computation.
Eg : Can generate primes
Can check primes.
25. Grammar
Generates valid strings using rules.
Practice Questions
DR. V SREENIVASULU 5
3. Let ∑ = {0, 1, 2} How many strings of length 3 or less are possible?
A) 8 B) 9 C) 18 D) None
L3 = 33
L2 = 32
L1 = 31 27+9+3+1 = 40
L0 = 30
DR. V SREENIVASULU 6
4. Let ∑ = {0, 1}, Let L = {01, 110, 100}
The graph consists of vertices (nodes) and edges. In an FA, the vertices
represent the states of the machine and the edges represent the transitions
between states based on input symbols.
The FA has one initial state, represented by an arrow pointing to the state. The
initial state is where the machine begins processing input symbols.
The machine may have one or more accepting states, indicated by a double
circle around the state. An accepting state is where the machine accepts the
input string.
The edges of the graph represent transitions between states. Each edge is
labeled with an input symbol, indicating the symbol that causes the machine
to transition from one state to another. For a deterministic finite automaton
(DFA), each state has exactly one edge leaving it for each input symbol in the
alphabet.
Finite Automata
o Finite automata are used to recognize patterns.
o It takes the string of symbol as input and changes its state accordingly. When the
desired symbol is found, then the transition occurs.
o At the time of transition, the automata can either move to the next state or stay in
the same state.
DR. V SREENIVASULU 7
o Finite automata have two states, Accept state or Reject state. When the input
string is processed successfully, and the automata reached its final state, then it will
accept.
State Diagram
→ can also be called transition diagram | transition system.
DR. V SREENIVASULU 8
A FA consists of Input tape, read only head and Finite Control.
The i/p tape consists of set of cells each hols one i/p symbol.
The read only head moves left to right and scans one symbol at a time.
FC determines the next state of the system using symbol under read only head and
the present state of the system.
Applications Applications
1. Used to check spellings 1. Computing1’s complement
In Editor
2. Lexical analysis in compilers 2. Computing 2’s compliment
3. General Applications 3. Adder | Subtractor
4. To model the behavior of 4. Counter
electrical ckts
5. To model the behaviour of 5. Sequence Generator/detector
digital ckts
6. To model the behaviour of
mechanical devices
7. To solve puzzles.
Equality
All types of FAS are equal. Equivalence means not the same but they perform the same
task.
L (FA) = L (DFA) = L (NFA) = L (ε- NFA) = RL.
FA DFA NFA ε- NFA = RL.
FA without o/p
DR. V SREENIVASULU 8
FA Representation
State Diagram
Transition Table
Staes a b
P Q R
Q Q R
R R R
String Acceptance by FA
If there Exists a path from initial state to final state the string w is said to be
accepted.
In other words, w is accepted by FA
iff δ (q0, w) → for some P in F.
Language Accept
A FA is said to be accepted a lang if all the strings in the lang are accepted.
All the strings that are not in the lang are rejected by the FA.
DR. V SREENIVASULU 9
DFA PROBLEMS
DR. V SREENIVASULU 10
DR. V SREENIVASULU 11
DR. V SREENIVASULU 12
Practice Questions MODEL :1
(start | End | contain)
DR. V SREENIVASULU 13
Exercise 1: Construct DFA for the RE: (a+b) * abb ( a+b)* → contain
Exercise 2: Construct DFA for the RE: ( a+b )* aaa → Ends with aaa Need 3+1 states.
Model : 2
( Language over 1 Symbol )
2. L = ( aaa )* over ∑ = { a, b }
DR. V SREENIVASULU 14
Exercise 2: L = { am, bn, cL | m, n, L >= 0 }
Model: 3
(Length and Divisible problems)
MODEL: 4
( Multiple conditions )
a b
DR. V SREENIVASULU 15
Exercise 1: L = {[ strings start and end with different symbols} over ∑ = {a, b}
Exercise: 2: L = ( aa+bb ) ( a+b)* ..................... starts with aa or bb.
MODEL: 5
(Position Based problems)
MODEL : 6
( Sequence Based problems )
5. L = { a+ b+ }
DR. V SREENIVASULU 16
Union of the above FA (starting and ending with different Symbols) L1 U L2
DR. V SREENIVASULU 17
8. Cross product
Even no of a’s Even no of b’s
L1 = { aa, aba, aab . . . } L2 = { bb, bab, bba . . . }
9. Compliment of language L1
L1 = {containing a } 𝐿̅1 = { does not contain a }
= { a, aa, ba, bab, . . . } = { Є, b, bb, bbb, . . . }
i) Final to non-final
ii) Non- final to final
L1
==========================================================================
DR. V SREENIVASULU 18
10. Reversal: start with a
L = { a, aa, ab, aaa . . . } LR = {Reverse of strings}
= { a, aaa, ba, aaa . . . }
Since no transition q0
DR. V SREENIVASULU 19
(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).
F is a set of final state/states of Q (F ⊆ Q).
Graphical Representation of an NDFA: (same as DFA)
An NDFA is represented by digraphs called state diagram.
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
DR. V SREENIVASULU 20
DR. V SREENIVASULU 21
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.
Empty string transitions are not seen in NDFA permits empty string transitions.
DFA.
In NDFA, backtracking is not always
Backtracking is allowed in DFA
possible.
Requires more space. Requires less space.
A string is accepted by a NDFA, if at least
A string is accepted by a DFA, if it transits one of all possible transitions ends in a
to a final state. final state.
DR. V SREENIVASULU 22
NDFA to DFA Conversion
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
Output: An equivalent DFA
Step 1 Create state table from the given 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.
DR. V SREENIVASULU 23
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.
Using the above algorithm, we find its equivalent DFA. The state table of the DFA is shown in
below.
Transition Table for DFA
q δ(q,0) δ(q,1)
[a] [a, b, c, d, e] [d,e]
[a, b, c, d, e ] [a, b, c, d, e] [b, d, e]
[d, e] [e] ∅
[b, d, e] [c, e] [e]
[e] ∅ ∅
[c, e] ∅ [b]
[b] [c] [e]
[c] ∅ [b]
0
[a,b,c,d,e] 1 [b,d,e] 0
[c,e]
0 1
[a] 1 [b]
1 1
1
0
0
DR. V SREENIVASULU[d,e] [e] [c] 18
DR. V SREENIVASULU 19
DR. V SREENIVASULU 20
DR. V SREENIVASULU 21
DR. V SREENIVASULU 22
Conversion of Epsilon-NFA to NFA
Non-deterministic Finite Automata (NFA) is a finite automata having zero, one or more than
one moves from a given state on a given input symbol. Epsilon NFA is the NFA which
contains epsilon move(s)/Null move(s). To remove the epsilon, move/Null move from epsilon-
NFA and to convert it into NFA, we follow the steps mentioned below.
Step-1:
Consider the two vertexes having the epsilon move. Here in Fig.1 we have vertex v1
and vertex v2 having epsilon move from v1 to v2.
Step-2:
DR. V SREENIVASULU 23
Now find all the moves to any other vertex that start from vertex v2 (other than the
epsilon move that is considering). After finding the moves, duplicate all the moves
that start from vertex v2, with the same input to start from vertex v1 and remove
the epsilon move from vertex v1 to vertex v2.
Step-3:
See that if the vertex v1 is a start state or not. If vertex v1 is a start state, then we
will also make vertex v2 as a start state. If vertex v1 is not a start state, then there
will not be any change.
Step-4:
See that if the vertex v2 is a final state or not. If vertex v2 is a final state, then we
will also make vertex v1 as a final state. If vertex v2 is not a final state, then there
will not be any change. Repeat the steps(from step 1 to step 4) until all the epsilon
moves are removed from the NFA.
DR. V SREENIVASULU 24
In the above example, we have 5 states named as q0, q1, q2, q3 and q4. Initially, we
have q0 as start state and q2 as final state. We have q1, q3 and q4 as intermediate
states.
Transition table for the above NFA is:
States/Inputs INPUT 0 INPUT 1 INPUT EPSILON
q0 – q1 q2
q1 – q0 –
q2 q3 q4 –
q3 q2 – –
q4 q2 – –
DR. V SREENIVASULU 25
S
Figure – State q0 as vertex v1 and state q2 as vertex v2
Step-2:
Now find all the moves that starts from vertex v2 (i.e. state q2).
After finding the moves, duplicate all the moves that start from vertex v2 (i.e state
q2) with the same input to start from vertex v1 (i.e. state q0) and remove the epsilon
move from vertex v1 (i.e. state q0) to vertex v2 (i.e. state q2).
Since state q2 on getting input 0 goes to state q3.
Hence on duplicating the move, we will have state q0 on getting input 0 also to go to
state q3.
Similarly state q2 on getting input 1 goes to state q4.
Hence on duplicating the move, we will have state q0 on getting input 1 also to go to
state q4.
DR. V SREENIVASULU 26
Figure – NFA after making state q2 as a start state
Step-4:
Since vertex v2 (i.e. state q2) is a final state. Hence we will also make vertex v1 (i.e.
state q0) as a final state.
Note that state q0 will also remain as a start state as we had initially.
After making state q0 also as a final state, the resulting NFA is:
INPUT INPUT
STATES/INPUT 0 1
q0 q3 q1,q4
q1 – q0
q2 q3 q4
q3 q2 –
q4 q2 –
DR. V SREENIVASULU 27
DR. V SREENIVASULU 28
DR. V SREENIVASULU 29
Converting Regular Language to Regular Expressions
Applications of RE
→ pattern matching algorithm.
→ search Engines (google, yahoo)
→ Text Editors.
→ Web programming forms.
PRACTICE QUESTIONS
13. Set of all strings such that 10th symbol from the right is 1.
RE : ( 0+1 )* 1 ( 0+1 )9
14. Set of all strings such that 3rd symbol from LHS is 1
RE : ( 0+1 )2 1 ( 0+1 )*
DR. V SREENIVASULU 31
15. # 1’s followed by exactly 2 consecutive Os followed by # 1’s.
RE : 1* 00 1*
1. Ø* = ε
2. ε* = ε
3. RR* = R*R
4. 4. R*R* = R*
5. 5. (R*)* = R*
6. RR* = R*R
7. (PQ)*P =P(QP)*
DR. V SREENIVASULU 32
8. (a+b)* = (a*b*)* = (a*+b*)* = (a+b*)* = a*(ba*)*
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:
DR. V SREENIVASULU 33
q1 q1 q
a b
Case 3: For a regular expression (a+b), we can construct the following FA:
q1 q
a
Case 4: For a regular expression (a+b)*, we can construct the following FA:
a,b
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.
DR. V SREENIVASULU 34
0,1
q0 q1 q2 q3 q
1 є є 0
0,1
q0 q2 q
1 0
Now we will remove the є transitions. After we remove the є transitions from the NDFA, weget the
following:NDFA without NULL transition for RA: 1 (0 + 1)* 0
It is an NDFA corresponding to the RE: 1 (0 + 1)* 0. If you want to convert it into a DFA, simplyapply
the method of converting NDFA to DFA discussed in Chapter 1.
Examples:
1. a (a + b) * a
2. a (a + b) * b
3. a (a + b)+ a
4. a (a + b)+ b
5. a*
6. a* b*
DR. V SREENIVASULU 35
9. Consider the following DFA.
10. Consider the following strings. Which of these below strings are accepted by the DFA?
A. 011110 B. 101011 C. 010110 D. 1110110
==========================================================================
Pumping Lemma
Pumping lemma is used to prove that a language is Not Regular.
But can’t be used to prove that a language is regular.
If L is a Regular Language, L has a pumping length P such that any word W where
|w|>= p may be divided into 3 parts w = x y z then the following condition must be
true.
1) x y z ε L for every i > 0, the string xyiz is also in L
2) |y| > 0 or |y| ≠ ε
3) |xy| < = P
To prove that a language is not regular using pumping lemma follow the steps.
o Assume that L is regular.
o The pumping length is n.
o Words of length greater than p can be pumped |w| >= n
o Divide w into x y z.
o Show that x y i z L f for some i.
o So, none can satisfy all the above 3 conditions at a time.
o So, the assumption is wrong, proof by contradiction.
1. Prove that the language L = {an bn | n >= 0}is not regular using pumping lemma.
Proof: Assume that L = {an bn | n >= 0}is regular
Pumping length = K
w = a K bk → aaaaaaaabbbbbbb
DR. V SREENIVASULU 36
Case1: The y is in the ‘a’ part
a a a a a a a b b b b b b b b x y z → x y2 z
x y z aaaaaaaaaaabbbbbbb
11 a’s ≠ 7 b’s
Case2: The y is in the ‘b’; part
a a a a a a a b b b b b b b x y1 z → x y2 z
x y z aaaaaaabbaabbbbbb
Not even the pattern = a bk k
2. Prove that the language L = {y y|y ∈ {0, 1} * is not regular by using pumping lemma.
Assume that L is regular
x y z k =7
0000000100000001 → x yi z → x y2 z
x y z 00000000000100000001
110’s L.
|y| > 0
|xy| < k Not satisfied.
Contradiction by proof. So, L is not regular.
========================================================================
3.
RL or non-RL? → pumping lemma for RL.
For any RL, L ∃ an integer is dependent on L
Set V Z ∈ L and |z| >= n
i) z =uvw then u vi w Є L V i >= 0
ii) |uv| <= |z|
iii) v >= 1 or |v| # 0. u, v, w, z are words ∈
DR. V SREENIVASULU 37
1.Z → ak-1 a bk → length of v >= 1
2. | u v | = | a a | = | a | = k < = 2k
k-1 k
Check | v | ≠ 0, but 1
At first, it is assumed L is a RL Hence uvi w Є L V I >= 0
Proof by contradiction → i = 2: u v w
→ L is non-RL a k-1 a bk = ak+1 bk
Now it is m+1 not equal to bm L
L = {a b | m >= 1} if i changes
m m
========================================================================
Eg2: L = {am bp | m > p}
Let assume L is a RL → by proof by contradiction assumption is incorrect. L is a non-
RL
z∈L
z = ak+1 bk choose k s.t |z| >= n
2k+1 >= n
ak+1 b bk-1
1) |z| >= n satisfied
3) Then u vi w Є L V I >= 0
Choose i = 2
u v w
ak+1 b2 bk-1 ∈ L
Because m > p
Proof by contradiction
Hence L is non regular
========================================================================
Eg 3: L = {a3n | n >= 1}
1) z Є L
u v w
a 3n-1 a Є → can be
but v = 1
or
v≠0
2) |u v| < = | z |
3) Now check
U v w Є L V I >= 0
Choose I = 2:
u v w
a3n-1 a2 Є → a3n+1 L
DR. V SREENIVASULU 38
because L = a3n but ↑ say a’s and b’s are equal.
By proof by contradiction. it is not.
Special case of pumping lemma when ∑ = {a}
Singleton set.
Length of strings must follow AP for a L tube a RL.
u vi w → a (aa)i a → {a a, a a a a, a a a a a a}
2 4 6
========================================================================
Eg 4: L = {a3n | n >= 1} ∑ = {a}
a , b , a , a
3 6 9 12 .............. fn=1
RL n =2
3 6 9 12 . . .. n = 3
Diff: 3 3 3 n =4
Eg 6: L = {a2n+1 | n >= 0} → RL
1, 3, 5, 7 ..........AP
If n = 0
n =1
n =2
n =3
1, 3, 5, 7 . . ..
========================================================================
Eg 7: L = {an3 +n2 +1| n >= 0} → non RL if n =0
1, 3, 13 . . . Not AP n =1
N =2
==============================00oo00======================================
DR. V SREENIVASULU 39