You are on page 1of 25

CT-4212

Theory of Computation and Automata

Instructor: Awot D.
Awot D. Defence University, college of
5/27/2020 1
Engineering
Week 5

Chapter Two
Finite Automata, Regular Expressions and Languages

Introduction to Regular Expressions

Awot D. Defence University, college of


5/27/2020 2
Engineering
Regular Languages
• The regular languages are those languages that can be constructed from the
“big three” set operations of (a) Union (b) Concatenation (c) Kleene star
(*).
• A regular language is defined as follows
• Definition: Let S be an alphabet. The class of “regular languages” over S
is defined inductively as follows:
a. ∅ is a regular language
b. For each a ∈ , {a} is a regular language
c. For any natural number n >= 2 if L1 , L2 , . . . 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).

5/27/2020 Awot D. Defence University, college of 3


Engineering
Regular Languages
• Examples:
∅ is a regular language (by rule (a))
(ii) L = {a, ab} is a language over S = {a, b} because, both {a} and {b}
are regular languages by rule (b). By rule (d) it follows that
{a} . {b} = {ab} is a regular language. Using rule (c), we see that
{a} ∪ {ab} = L is a regular language.
(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*)*.

Awot D. Defence University, college of


5/27/2020 4
Engineering
Regular Expressions
• Regular expressions were designed to represent regular languages
with a mathematical tool, a tool 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 *.
• In short, Certain sets of strings or languages can be represented in
algebraic fashion, then these algebraic expressions of languages are
called regular expressions.

Awot D. Defence University, college of


5/27/2020 5
Engineering
The set of regular expressions is defined by the following rules:

• Every letter including ∈ and ∅ can be made into a regular expression


itself is a regular expression.
• If l and m are regular expressions, then so are
• (l)
• lm
• l+m
• l*
• 𝑰+ = ll*
• Nothing else is regular expression.
• The regular expression over a given alphabet , are precisely those
obtained recursively by the application of the above rules once or several
times.

Awot D. Defence University, college of


5/27/2020 6
Engineering
Cont…
• For example, now we would build expression from the symbols 0,1
using the operations of union, concatenation, and Kleene closure.
 01 means a zero followed by a one (concatenation)
 0+1 means either a zero or a one (union)
 0* means ∈+0+00+000+….. (Kleen closure) .
• With parentheses, we can build larger expressions. And, we can
associate meanings with our expressions. Here’s how
Expression Set represented
(0+1)* all strings over {0,1}
0*10*10* strings containing exactly two ones
(0+1)*11 strings which end with two ones.
• The language denoted/represented by the regular expression R is
L(R).

Awot D. Defence University, college of


5/27/2020 7
Engineering
Cont…
Example: given
Alphabet . = {a, b, c, d}
1) (a + b)*
= all strings containing only a and b
2) c(a + b + c)*c2
= all strings containing only a, b, and c that begin
with c and end with cc
3) (a+c+d)*b(a+c+d)*
= all strings containing only one b

Awot D. Defence University, college of


5/27/2020 8
Engineering
Cont…
Alphabet . = {0, 1}
1) (0 + 1)*
= the set of all binary strings
2) 031*04
= all strings consisting of three 0’s, followed
by any number of 1’s, followed by four 0’s
3) 0*1001*
= all strings starting with any number of 0’s,
followed by 100, followed by and number of
1’s
Awot D. Defence University, college of
5/27/2020 9
Engineering
Cont…
Example 1 Example 2
• The language L defined by the regular • The language associated with the
expression ab*a is the set of all strings regular expression a*b* contains all
of a’s and b’s that begin and end with the strings of a’s and b’s in which all
a’s, and that have nothing but b’s inside. the a’s (if any) come before all the b’s
• L = {aa, aba, abba, abbba, abbbba, } (if any).
• L = { , a, b, aa, ab, bb, aaa, aab, abb,
bbb, aaa,…)

Awot D. Defence University, college of


5/27/2020 10
Engineering
Example 3
• Let us consider the language L defined by the regular
expression (a+b)* a(a+b)*. The strings of the language L are
obtained by concatenating a string from the language
corresponding to (a+b)* followed by a followed by a string
from the language associated with (a+b)*. We can also say
that the language is a set of all words over the alphabet
, = {a,b} that have an a in them somewhere.

Awot D. Defence University, college of


5/27/2020 11
Engineering
Cont.…
• Definition: If S and T are sets of 2. If S = {a ,bb ,bab}, T = { ,bbbb}
strings of letters (they may be finite or Then, ST = {a ,bb ,bab ,abbbb ,bbbbbb
infinite sets),we define the product set ,babbbbb}
of strings of letters to be.
• ST = {all combinations of a string 3.
from S concatenated with a string
from T in that order}.
Example:
1. If S = {a, aa, aaa}, T = {bb, bbb}
Then, ST = {abb, abbb, aabb, aabbb,
aaabb, aaabbb}.
Awot D. Defence University, college of
5/27/2020 12
Engineering
Exercise
 Find a regular expression to describe each of the following languages:
a) {a,b,c}

a) {a,b,ab,ba,abb,baa,….}

a) { ,a,abb,abbbb,….}

a) {∈, 0, 00, 000, 0000, . . .}

a) {baa, b, a, aab}

a) {1, 11, 111, 1111, . . . }

Awot D. Defence University, college of


5/27/2020 13
Engineering
Designing Regular Expressions
1. Design RE from the ff languages over an alphabet {a,b}
L1 = {set of all strings of length exactly two}
Solution:
L1 = {aa, ab, ba, bb}
RE = aa + ab + ba + bb
RE = a(a +b) + b(a + b)
RE = (a + b)(a + b) this is the regular expression

Awot D. Defence University, college of


5/27/2020 14
Engineering
Equivalence of RE with FA
• Regular expressions and finite automata are equivalent in
terms of the languages they describe.
Theorem:
• A language is regular iff some regular expression describes it.

Awot D. Defence University, college of


5/27/2020 15
Engineering
Converting RE into FA
• If a language is described by a regular expression, then it is
regular.
Proof idea:
• Build a NFA by transforming some regular expression r into a
non-deterministic finite automaton N that accepts the language
L(r).

Awot D. Defence University, college of


5/27/2020 16
Engineering
RE to FA Algorithm
• Given r we start the algorithm with N having a start state, a
single accepting state and an edge labeled r:
• Note: We assume at a moment that transactions can be labeled
with RE, not just letters.

Awot D. Defence University, college of


5/27/2020 17
Engineering
RE to FA Algorithm
• Now transform this machine into an NFA N by applying the
following rules until all the edges are labeled with either a
letter δ from , or ∈ :
1. If an edge is labeled ∅, then delete this edge.

Awot D. Defence University, college of


5/27/2020 18
Engineering
RE to FA Algorithm
• Transform any diagram of the type

into the diagram

Awot D. Defence University, college of


5/27/2020 19
Engineering
RE to FA Algorithm
• Transform any diagram of the type

into the diagram

Awot D. Defence University, college of


5/27/2020 20
Engineering
RE to FA Algorithm
• Transform any diagram of the type

into the diagram

Awot D. Defence University, college of


5/27/2020 21
Engineering
Cont…
• Construct an NFA for the regular • Next we apply rule 3 for ab:
expression b*+ ab.
• Solution: We start by drawing the
diagram:

• Next we apply rule 4 for b*:


• Next we apply rule 2 for b*+ab:

Awot D. Defence University, college of


5/27/2020 22
Engineering
• The final NFA

• Exercise : Draw an NFA for (ab+a)*

Awot D. Defence University, college of


5/27/2020 23
Engineering
Example
• Convert the following regular expression to it’s equivalent FA.
a) (a|b)*(abb|a𝑎+ b)

b) 10 + (1+00)1*0 try this by yourself!

Awot D. Defence University, college of


5/27/2020 24
Engineering
Example
• Convert the following regular expression to it’s equivalent FA.
1. ab*b

2. (a+c)b

3. c(ab)* try this one!

Awot D. Defence University, college of


5/27/2020 25
Engineering

You might also like