2. Regular Expressions
Regular expressions were mathematical tool designed to represent
regular languages.
Built from a set of primitives and operations.
This representation involves a combination of strings of symbols
from some alphabet Σ, parentheses and the operators +, ⋅, and *.
A regular expression is obtained from the symbol {a, b, c}, empty
string ∈, and empty-set ∅ perform the operations +, ⋅ and *
(union, concatenation and Kleene star).
2
3. Regular Expressions
Examples
0 + 1 represents the set {0, 1}
1 represents the set {1}
(0 +1)1 represents the set {01, 11}
(a+b)⋅(b+c) represents the set {ab, bb, ac, bc}
(0 + 1)* = ∈+ (0 + 1) + (0 + 1) (0 + 1)..........= Σ*
(0 + 1 )+ =(0 +1) (0 +1)*= Σ+ =Σ*- {ε}
3
4. Building Regular Expressions
Assume that Σ = {a,b,c}
Zero or more: a* means zero or more a‟s,
To say zero or more ab‟s, i.e.,{λ, ab,abab........,} you need to say (ab)*.
One or more: Since a* means zero or more a‟s, you can use aa*
(or equivalently a*a) to mean one or more a‟s.
Similarly to describe „one or more ab‟s”, that is {ab, abab,
ababab, .........}, you can use ab (ab)*.
Zero or one: It can be described as an optional „a‟ with (a + λ).
4
6. Hierarchy of evaluation of regular expression
6
We follow the following order when evaluate a regular
expressions;
Parenthesis
Kleene Closure
Concatenation
Union
8. Regular Languages
Let Σ be an alphabet. The class of “regular languages” over Σ is
defined inductively as follows:
a) ∅ is a regular language
b) For each 𝜎 ∈ Σ, {ζ}is a regular language
c) For any natural number n ≥ 2, if L1, L2, L3, .... ,Ln are regular
languages, then so is L1∪L2 ∪......∪ Ln.
d) For any natural number n ≥ 2, if L1 L2,...... Ln are regular
languages, then so is L1 L2 ...... Ln.
e) If L is a regular language, then so is L*.
(f) Nothing else is a regular language unless its construction
follows from rules (a) to (e).
8
9. Examples
i. ∅ is a regular language (by rule (a))
ii. L = {a, ab} is a language over Σ = {a, b}because, both {a} and
{b} are regular languages by rule (b).
{a} o {b}={ab} is a regular language. By rule (d)
{a }∪{ab } = L is a regular language. Using rule (c),
iii. The language over the alphabet {0,1} where strings contain an
even number of 0‟s can be constructed by
(1*((01*)(01*))*) or simply 1*(01* 01*)*.
9
11. Regular Expressions to NFA
For any x in Σ, the regular expression denotes the language {x}.
The NFA (with a single start state and a single final state) as
shown below, represents exactly that language.
The regular expression λ denotes the language {λ}that is the
language containing only the empty string.
11
12. Regular Expressions to NFA
The regular expression ∅ denotes the language ∅; no
strings belong to this language, not even the empty
string.
For juxtaposition, strings in L(r1) followed by strings in L(r2)
, we chain the NFAs together
12
13. Regular Expressions to NFA
The “+” denotes “or” in a regular expression, we would use an
NFA with a choice of paths.
The star (*) denotes zero or more applications of the regular
expression, hence a loop has to be set up in the NFA.
13
14. Example :Construct An NFA for the regular expression (0+1)*.
14
Solution: NFA is constructed step by step by breaking the RE.
r3= (r1+r2)
r=r3*, where r1=0, r2=1
15. Example
Represent the following sets by regular expression
a. {∧, ab}
b. {1,11,111....}
c. {ab, a, b, bb}
Solution
a. The set {∧, ab} is represented by the regular expression ∧ + ab
b. The set{1, 11,111,....,}is got by concatenating 1 and any
element of {1}*.
Therefore 1(1)* represent the given set.
c. The set {ab, a, b, bb} represents the regular expression
ab+ a+ b +bb.
15
16. Exercise
1. Obtain the regular expressions for the following sets:
1.1. The set of all strings over {a, b} beginning and ending with „a‟.
The regular expression for ‘the set of all strings over {a, b}
beginning and ending with ‘a’ is given by: a (a + b)*a
1.2. {b2, b5, b8,. . . . .}
The regular expression for {b 2 , b 5 , b 8 , ............} is given by:
bb (bbb)*
1.3. {a2n+1 |n > 0}
The regular expression for {a 2n+1 |n > 0}is given by:
a (aa)+
16
17. FINITE AUTOMATA WITH OUTPUT
A finite-state machine M = (Q , Σ, δ , λ, O , q0 ) consists of
a finite set Q of states,
a finite input alphabet Σ,
a finite output alphabet O,
a transition function δ that assigns to each state and input pair
of a new state,
an output function λ that assigns to each state and input pair an
output, and
an initial state q0.
17
18. 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.
It can be described by a 6 tuple (Q, ∑, O, δ, X, q0)
M = (Q , O , ,Σ , δ , λ , q0 ) consists of
A finite set Q of states,
A finite input alphabet Σ,
A finite output alphabet O,
A transition function δ that assigns to each
state and input pair a new state,
An output function X that assigns to each
state and input pair an output, and
An initial state q0.
18
19. Examples
The above Mealy Machine outputs a value for each input.
You can tell input /output.
So for the following input:
000101010
It outputs
000010101
19
20. Moore Machine
Moore machine is an FSM whose outputs depend on only the
present state.
A Moore machine can be described by a 6 tuple(Q, ∑, O, δ, X, q0)
20
21. Current State Input Next State Output
A 0 D 0
A 1 B 0
B 0 E 0
B 1 C 0
C 0 F 0
C 1 C 0
D 0 G 0
D 1 E 0
E 0 H 0
E 1 F 0
F 0 I 1
F 1 F 0
G 0 G 0
G 1 H 0
H 0 H 0
H 1 I 1
I 0 I 1
I 1 I 1
Fig Example Moore machine
21
22. PROPERTIES OF REGULAR SETS
(LANGUAGES)
There are several operations defined on languages:
L1 ∪ L2: strings in either L1 or L2.
L1 ∩ L2 : strings in both L1 and L2.
L1L2 : strings composed of one string from L1, followed by one
string from L2.
L1*: Zero or more strings from L1 concatenated together
L1-L2 : strings in L1 that are not in L2.
L1
R : strings in L1, reversed.
22
23. Applying the Pumping Lemma
Theorem
Let L be a regular language. Then there exists a constant ‘c’ such that
for every string w in L −
|w| ≥ c
We can break w into three strings,
w = xyz, such that −
|y| > 0
|xy| ≤ c
For all k ≥ 0, the string xykz is also in L.
The “pumping lemma” for regular languages is another way of
showing that a given infinite language is not regular.
23
24. Applications of Pumping Lemma
Method to prove that a language L is not regular
At first, we have to assume that L is regular.
So, the pumping lemma should hold for L.
Use the pumping lemma to obtain a contradiction −
Select w such that |w| ≥ c
Select y such that |y| ≥ 1
Select x such that |xy| ≤ c
Assign the remaining string to z.
Select k such that the resulting string is not in L.
Hence L is not regular.
24
25. Problem
Prove that L = {aibi | i ≥ 0} is not regular.
Solution: At first, assume that L is regular and n is the number of states.
Let w = anbn. Thus |w| = 2n ≥ n.
By pumping lemma, let w = xyz, where |xy|≤ n.
Let x = ap, y = aq, and z = arbn, where p + q + r = n.
p ≠ 0, q ≠ 0, r ≠ 0. Thus |y|≠ 0
Let k = 2. Then xy2z = apa2qarbn.
Number of as = (p + 2q + r) = (p + q + r) + q = n + q
Hence, xy2z = an+q bn. Since q ≠ 0, xy2z is not of the form anbn.
Thus, xy2z is not in L. Hence L is not regular.
25
26. 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.
We will take an example and elaborate this below −
This DFA accepts the language
L = {a, aa, aaa , ............. }
over the alphabet
∑ = {a, b}
So, RE = a+.
26
27. Con..
Now we will swap its accepting states with its non-accepting states
and vice versa and will get the following −
This DFA accepts the language
Ľ = { ε , b, ab ,bb,ba, ............... } over the alphabet
∑ = {a, b}
27
28. Exercise
28
1. Let Σ = {a, b}.
Let L = { w ∈ Σ* | w contains at most one a }.
Which of the following is a regular expression for L?
A. Σ*aΣ*
B. b*ab* ∪ b*
C. b*(a ∪ ε)b*
D. b*a*b* ∪ b*
E. b*(a* ∪ ε)b*
F. None of the above, or two or more of the above.
2. Construct An NFA for the regular expression 1(11)*.