ACD Module - 2 Notes
ACD Module - 2 Notes
vtucode.in Page 1
Automata Theory & Compiler Design 21CS51 Module 2
If R is a regular expression denoting the language LR and S is a regular expression denoting the
language LS then R + S is a regular expression corresponding to the language LR U LS.
R.S is a regular expression corresponding to the language LR. LS.
R* is a regular expression corresponding to the language LR. Thus the expressions obtained by
applying any of the rules are regular expressions.
Examples of Regular expressions
Regular expression Meaning
a* String consisting of any number of a’s. (zero or more a’s)
a+ String consisting of at least one a. (one or more a’s)
(a + b) String consisting of either a or b
*
(a+b) String consisting of any nuber of a’s and b’s including ε
(a+b)* ab Strings of a’s and b’s ending with ab.
ab(a+b)* Strings of a’s and b’s starting with ab.
(a + b)* ab (a+b)* Strings of a’s and b’s with substring ab.
vtucode.in Page 2
Automata Theory & Compiler Design 21CS51 Module 2
vtucode.in Page 3
Automata Theory & Compiler Design 21CS51 Module 2
Obtain regular expression to accept the language containing strings of a’s and b’s such that L = {
a2n+1 b2m+1 | n, m 0 }.
a2n+1 means odd number of a’s, regular expression = a(aa)*
b2m+1 means odd number of b’s, regular expression = b(bb)*
The regular expression for the given language = a(aa)*b(bb)*
Obtain regular expression to accept the language containing strings of 0’s and 1’s with exactly
one 1 and an even number of 0’s.
Regular expression for exactly one 1 = 1
Even number of 0’s = (00)*
So here 1 can be preceded or followed by even number of 0’s or 1 can be preceded and followed
by odd number of 0’s.
The regular expression for the given language = (00)* 1 (00)* + 0(00)* 1 0(00)*
Obtain regular expression to accept the language containing strings of 0’s and 1’s having no two
consecutive 0’s. OR
Obtain regular expression to accept the language containing strings of 0’s and 1’s with no pair of
consecutive 0’s.
Whenever a 0 occurs it should be followed by 1. But there is no restriction on number of 1’s. So
it is a string consisting of any combinations of 1’s and 01’s, ie regular expression = (1+01)*
Suppose string ends with 0, the above regular expression can be modified by inserting (0 + ε ) at
the end.
Regular expression for the given language = (1+01)* (0 + ε )
Obtain regular expression to accept the language containing strings of 0’s and 1’s having no two
consecutive 1’s. OR
Obtain regular expression to accept the language containing strings of 0’s and 1’s with no pair of
consecutive 1’s.
Whenever a 1 occurs it should be followed by 0. But there is no restriction on number of 0’s. So
it is a string consisting of any combinations of 0’s and 10’s, ie regular expression = (0+10)*
Suppose string ends with 1, the above regular expression can be modified by inserting (1 + ε ) at
the end.
Regular expression for the given language = (0+10)* (1 + ε )
Obtain regular expression to accept the following languages over Σ = { a, b}.
i. Strings of a’s and b’s with substring aab.
vtucode.in Page 4
Automata Theory & Compiler Design 21CS51 Module 2
So the regular expression for the given language = [(a+b) (a+b) (a+b)]*+ [(a+b)
(a+b)]*
ix. Obtain the regular expression to accept the language L = { anbm | m+n is even }
Here n represents number of a’s and m represents number of b’s.
m+n is even results in two possible cases;
case i. when even number of a’s followed by even number of b’s.
regular expression : (aa)*(bb)*
case ii. Odd number of a’s followed by odd number of b’s.
regular expression = a(aa)* b(bb)*.
So the regular expression for the given language = (aa)*(bb)* + a(aa)* b(bb)*
x. Obtain the regular expression to accept the language L = { anbm | n 4 and m 3 }.
Here n 4 means at least 4 a’s, the regular expression for this = aaaa(a)*
m 3 means at most 3 b’s, regular expression for this = (ε+b) (ε+b) (ε+b).
So the regular expression for the given language = aaaa(a)* (ε+b) (ε+b) (ε+b).
xi. Obtain the regular expression to accept the language L = { anbm cp | n 4 and m 3 p
2}.
Here n 4 means at least 4 a’s, the regular expression for this = aaaa(a)*
m 3 means at most 3 b’s, regular expression for this = (ε+b) (ε+b) (ε+b).
p 2 means at most 2 c’s, regular expression for this = (ε+c) (ε+c)
So the regular expression for the given language = aaaa(a)*(ε+b) (ε+b) (ε+b) (ε+c)
(ε+c).
xii. All strings of a’s and b’s that do not end with ab.
Strings of length 2 and that do not end with ab are ba, aa and bb.
So the regular expression = (a+b)*(aa + ba +bb)
xiii. All strings of a’s, b’s and c’s with exactly one a.
The regular expression = (b+c)* a (b+c)*
xiv. All strings of a’s and b’s with at least one occurrence of each symbol in Σ = {a, b}.
At least one occurrence of a’s and b’s means ab + ba, in between we have n number
of a’s and b’s.
So the regular expression =(a+b)* a (a+b)* b(a+b)* +(a+b)* b(a+b)* a(a+b)*
vtucode.in Page 6
Automata Theory & Compiler Design 21CS51 Module 2
Case ii. Since nm 3, if m = 1 then n should be 3. The equivalent regular expression is given
by: RE = aaa(a)* b
Case iii. Since nm 3, if m 2 and n 2 then the equivalent regular expression is given by:
RE = aa(a)* bb(b)*
So the final regular expression is obtained by adding all the above regular expression.
Regular expression = abbb(b)* + aaa(a)*b + aa(a)*bb(b)*
Application of Regular expression:
1. Regular expressions are used in UNIX.
2. Regular expressions are extensively used in the design of Lexical analyzer phase.
3. Regular expressions are used to search patterns in text.
FINITE AUTOMATA AND REGULAR EXPRESSIONS
1. ****Converting Regular Expressions to Automata:
Prove that every language defined by a regular expression is also defined by a finite automata.
Proof:
Suppose L = L(R) for a regular expression R, we show that L = L(E) for some ε-NFA E with:
a. Exactly one accepting state.
b. No arcs into the initial state.
c. No arcs out of the accepting state.
The proof must be discussed with the following transition diagrams for the basis of the
construction of an automaton.
vtucode.in Page 7
Automata Theory & Compiler Design 21CS51 Module 2
Starting at new start state, we can go to the start state of either the automaton for R or S. We then
reach the accepting state of one of these automata R or S. We can follow one of the ε- arcs to the
accepting state of the new automaton.
Automaton for R.S is given by:
The start state of the first( R) automata becomes the start state of the whole and the final state of
the second(S) automata becomes the final state of the whole.
Automaton for R* is given by:
From start state to final state one arc labeled ε ( for ε in R*) or the to the start state of automaton
R through that automaton one or more time and then to the final state.
vtucode.in Page 8
Automata Theory & Compiler Design 21CS51 Module 2
vtucode.in Page 9
Automata Theory & Compiler Design 21CS51 Module 2
Finally the ε-NFA for the regular expression: (0+1)*1(0+1) is given by:
vtucode.in Page 10
Automata Theory & Compiler Design 21CS51 Module 2
vtucode.in Page 11
Automata Theory & Compiler Design 21CS51 Module 2
vtucode.in Page 12
Automata Theory & Compiler Design 21CS51 Module 2
vtucode.in Page 13
Automata Theory & Compiler Design 21CS51 Module 2
3. If the start state of FSM M is part of a loop (i.e: it has any transitions coming into it), then
create a new start state s and connects to M ‘s start state via an ε-transition. This new
start state s will have no transitions into it.
4. If a FSM M has more than one accepting state or if there is just one but there are any
transitions out of it, create a new accepting state and connect each of M’s accepting states
to it via an ε-transition. Remove the old accepting states from the set of accepting states.
Note that the new accepting state will have no transitions out from it.
5. At this point, if M has only one state, then that state is both the start state and the
accepting state and M has no transitions. So L (M} = {ε}. Halt and return the simple
regular expression as ε.
6. Until only the start state and the accepting state remain do:
6.1. Select some state s of M which is of any state except the start state or the accepting
state.
6.2 Remove that state s from M.
6.3 Modify the transitions among the remaining states so that M accepts the same
strings The labels on the rewritten transitions may be any regular expression.
7. Return the regular expression that labels the one remaining transition from the start state
to the accepting state
Consider the following FSM M: Show a regular expression for L(M).
OR
Obtain the regular expression for the following finite automata using state elimination method.
We can build an equivalent machine M' by eliminating state q2 and replacing it by a transition
from q1 to q3 labeled with the regular expression ab*a.
So M' is:
Obtain the regular expression for the following finite automata using state elimination method.
There is no incoming edge into the initial state as well as no outgoing edge from final state. So
there is only two states, initial and final.
There is no incoming edge into the initial state as well as no outgoing edge from final state.
After eliminating the state B:
Regular expression = ab
Obtain the regular expression for the following finite automata using state elimination method.
There is no incoming edge into the initial state as well as no outgoing edge from final state.
After eliminating the state B:
vtucode.in Page 15
Automata Theory & Compiler Design 21CS51 Module 2
Obtain the regular expression for the following finite automata using state elimination method.
Since initial state has incoming edge, and final sate has outgoing edge, we have to create a new
iniatial and final state by connecting new initial state to old initial state through ε and old final
state to new final state through ε. Make old final state has non-final state.
vtucode.in Page 16
Automata Theory & Compiler Design 21CS51 Module 2
Since there are multiple final states, we have to create a new final state.
vtucode.in Page 17
Automata Theory & Compiler Design 21CS51 Module 2
Obtain the regular expression for the following finite automata using state elimination method.
vtucode.in Page 18
Automata Theory & Compiler Design 21CS51 Module 2
vtucode.in Page 19
Automata Theory & Compiler Design 21CS51 Module 2
Obtain the regular expression for the following finite automata using state elimination method.
Since start state 1 has incoming transitions, we create a new start state and link that state to state
1 through ε.
vtucode.in Page 21
Automata Theory & Compiler Design 21CS51 Module 2
Since accepting state 1 and 2 has outgoing transitions, we create a new accepting state and link
that state to state 1 and state 2 through ε. Remove the old accepting states from the set of
accepting states. (ie: consider 1 and 2 has non final states)
Finally we have only start and final states with one transition from start state 1 to final state 2,
The labels on transition path indicates the regular edpression.
Regular Expression = (ab U aaa* b)* (a U ε )
vtucode.in Page 22
Automata Theory & Compiler Design 21CS51 Module 2
vtucode.in Page 23
Automata Theory & Compiler Design 21CS51 Module 2
vtucode.in Page 24
Automata Theory & Compiler Design 21CS51 Module 2
vtucode.in Page 25
Automata Theory & Compiler Design 21CS51 Module 2
vtucode.in Page 26
Automata Theory & Compiler Design 21CS51 Module 2
vtucode.in Page 27
Automata Theory & Compiler Design 21CS51 Module 2