Theory of Automata and Formal Languages
Theory of Automata and Formal Languages
Formal Languages
Lecture 03
Regular Expression
• As we know, a* generates Λ, a, aa, aaa, … and a+
generates a, aa, aaa, aaaa, …,
• So the language L1 = {Λ, a, aa, aaa, …} and
L2 = {a, aa, aaa, aaaa, …} can simply be expressed
by a* and a+, respectively.
a* and a+ are called the regular expressions (RE)
for L1 and L2 respectively.
Note: a+, aa* and a*a generate L2.
Recursive definition of Regular Expression(RE)
2.r1 r2
3.r1 + r2 and
4. r1*
are also regular expressions.
Step 3: Nothing else is a regular expression.
Defining Languages (continued)…
• Method 3 (Regular Expressions)
• Consider the language L={Λ, x, xx, xxx,…} of strings, defined over Σ
= {x}.
We can write this language as the Kleene star closure of alphabet Σ
or L=Σ*={x}* this language can also be expressed by the regular
expression x*.
• Similarly the language L={x, xx, xxx,…}, defined over Σ = {x}, can be
expressed by the regular expression x+.
Example
• Now consider another language L, consisting of all possible strings,
defined over Σ = {a, b}. This language can also be expressed by the
regular expression
(a + b)*.
• Now consider another language L, of strings having exactly double
a, defined over Σ = {a, b}, then it’s regular expression may be
b*aab*
Example
• Now consider another language L, of even length, defined over Σ =
{a, b}, then it’s regular expression may be
((a+b)(a+b))*
• Now consider another language L, of odd length, defined over Σ =
{a, b}, then it’s regular expression may be
(a+b)((a+b)(a+b))* or
((a+b)(a+b))*(a+b)
Important
• It may be noted that a language may be expressed by more than one
regular expressions, while given a regular expression there exist a
unique language generated by that regular expression.
Example
• Consider the language, defined over
Σ={a , b} of words having at least one a, may be
expressed by a regular expression
(a+b)*a(a+b)*.
• Consider the language, defined over
Σ = {a, b} of words having at least one a and
one b, may be expressed by a regular
expression
(a+b)*a(a+b)*b(a+b)*+ (a+b)*b(a+b)*a(a+b)*.
Example
• Consider the language, defined over
Σ={a, b}, of words starting with double a and
ending in double b then its regular expression
may be
aa(a+b)*bb
• Consider the language, defined over
Σ={a, b} of words starting with a and ending in
bOR starting with b and ending in a, then its
regular expression may be,
a(a+b)*b+b(a+b)*a
An important example
The Language EVEN-EVEN :
Language of strings, defined over Σ={a, b} having even number of a’s
and even number of b’s. i.e.
EVEN-EVEN = {Λ, aa, bb, aaaa,aabb,abab, abba, baab, baba, bbaa,
bbbb,…} ,
its regular expression can be written as
(aa+bb+(ab+ba)(aa+bb)*(ab+ba))*
Note
• It is important to be clear about the difference of the following
regular expressions
r1=a*+b*
r2=(a+b)*
Here r1 does not generate any string of concatenation of a and
b, while r2 generates such strings.
Equivalent Regular Expressions
• Definition:
Two regular expressions are said to be equivalent if they
generate the same language.
Example:
Consider the following regular expressions
r1= (a + b)* (aa + bb)
r2= (a + b)*aa + ( a + b)*bb then
both regular expressions define the language of strings ending in aa
or bb.
Note
Example:
Consider the language L, defined over Σ={a,b}, of strings of length 2,
starting with a, then
L={aa, ab}, may be expressed by the regular expression aa+ab. Hence
L, by definition, is a regular language.
Note
It may be noted that if a language contains even thousand words, its
RE may be expressed, placing ‘ + ’ between all the words.
Here the special structure of RE is not important.
Consider the language L={aaa, aab, aba, abb, baa, bab, bba, bbb}, that
may be expressed by a RE aaa+aab+aba+abb+baa+bab+bba+bbb,
which is equivalent to (a+b)(a+b)(a+b).
Defining Languages (continued)…
• Method 4 (Finite Automaton)
Definition:
A Finite automaton (FA), is a collection of the followings
1) Finite number of states, having one initial and some
(maybe none) final states.
2) Finite set of input letters (Σ) from which input strings are
formed.
3) Finite set of transitions i.e. for each state and for each
input letter there is a transition showing how to move
from one state to another.
Example
• Σ = {a,b}
• States: x, y, z where x is an initial state and z is final state.
• Transitions:
1. At state x reading a go to state z,
2. At state x reading b go to state y,
3. At state y reading a, b go to state y
4. At state z reading a, b go to state z
Example Continued …
Reading a Reading b
x- z y
y y y
z+ z z
Note
• It may be noted that the information of an FA, given in the previous
table, can also be depicted by the following diagram, called the
transition diagram, of the given FA
a,b
y
b
x–
a,b
a
Z+
Remark