Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 12

MTH 307: Discrete Structures II

(Theory of Computation)
Chapter 3: Regular Expressions

Dr. Maroun Abi Assaf

1
Chapter 3 Regular Expressions
• We have studied that a regular language can be described by dfa
and nfa. In this chapter we look at another way of representing
regular languages.
3.1 Regular Expressions
• Formal Definition of a Regular Expression
Definition 3.1
Let Σ be a given alphabet. Then
1.  , 
ε and a  Σ are all regular expressions. These are called primitive
regular expressons.
2. If r1 and r2 are regular expressions, so are r1  r2 , r1  r2 , r1* and (r1 ).
3. A string is a regular expression if and only if it can derived from
the primitive regular expressions by a finite number of applications
of the rules in (2).
Example 3.1 For   {a, b, c}, the string (a  b  c)
 (c  ) is a regular
expression, but (a  b  ) is not a regular expression.
• Language Associated with Regular Expressions
Definition 3.2
The language L(r) denoted by any regular expression r is defined
by the following rules.
1.  is a reguar expression denoting the empty set,
2. ελ is a regular expression denoting {ε}
{λ}, Termination
3. for every a  Σ , a is a regular expression denoting {a}. condition
If r1 and r2 are regular expressions, then
4. L(r1  r2 )  L(r1 )  L(r2 ),
5. L(r1  r2 )  L(r1 )L(r2 ), Reduce L(r ) to simpler components.
6. L((r1 ))  L(r1 ),
7. L(r1* )  ( L(r1 ))* .
Example 3.2 Exhibit the language L(a* ・ (a+b)) in set notation.
Solution L(a *  (a  b))  L(a * ) L(a  b)  ( L(a ))* ( L(a )  L(b))
 {ελ, a, aa, aaa,...}{a, b}  {a, aa, aaa,..., b, ab, aab,...}

Ambiguity: L(a  b  c)  {ab, c} or L(a  b  c)  {ab, bc} ?

Precedence rules: star-closure, concatenation, union


high low

Example 3.3
For Σ  {a,b}, the expression r  (a  b)*(a  bb)
is regular. It denotes the language
L(r )  L((a  b)* ) L(a  bb)  ( L(a  b))* L(a  bb)
 ( L(a )  L(b))* ( L(a )  ( L(b) L(b)))  {a, b}*{a, bb}
 {ε , a, b, ab, ba,...}  {a, bb, aa, abb, ba, bbb,...}
Example 3.4
What is the language described by the regular expression: r=(aa)*(bb)*b ?

L(r) = {a2nb2m+1; n≥0, m≥0}

Example 3.5
For   {0,1}, give a regular expression r such that
L(r)  {w  Σ * : w has at least one pair of consequtive zeros}

Solution: r  (0  1)* 00(0  1)*

Example 3.6
Find a regular expression for the language
L  {w  {0,1} : w has no pair of consevutive zeros}.

Solution: r  (1  01) (0  ε )
*
3.2 Connection Between Regular Expressions and
Regular Languages
• Given regular expression r, we have defined a language L(r ).
• A language is said to be regular, if it is accepted by a dfa (or by
an nfa since dfa and nfa are equivalent).
What is the relation between regular expressions and regular language?
Regular languages Regular expressions

Theorem 3.1 Let r be a regular expression. Then there exists some


nfa that accepts L(r ). Consequently, L(r ) is a regular language.

Theorem 3.2 Let L be a regular language. Then there exists a


regular expression r such that L=L(r ).
Theorem 3.1 Let r be a regular expression. Then there exists some
nfa that accepts L(r ). Consequently, L(r ) is a regular language.

Theorem 3.2 Let L be a regular language. Then there exists a


regular expression r such that L=L(r ).
Regular Expressions for Describing Simple Patterns

The set of all acceptable Pascal integers is defined


by the regular expression sdd * , where s stands
for the sign, with possible value from {-,λε}, and
d standsfor the digits 0 to 9.
The editor ed in the UNIX operating system
recognaizes the commad /aba* c/ as to find
the string aba* c. It means that the UNIX editor
can recognaize regular expressions.

Given a regular expression, we can find an nfa for recognizing it.


This nfa can be reduced to a dfa.
Example 3.7
Convert the following NFA to a Regular Expression:

Convert the following NFA to a Regular Expression:

Convert the following DFA to a Regular Expression:

r = a* + (a*ba*ba*ba*)*
Algorithm for the conversion of Regular Expression to ɛ-NFA

Input − A Regular Expression R


Output − ɛ-NFA accepting language denoted by R
Method
•For ε, NFA is

•For a, NFA is

•For a + b, NFA is

•For ab, NFA is Or

•For a*, NFA is


Example 3.8 Draw NFA for the Regular Expression a(a+b)*ab:

Example 3.9
Draw NFA for a + b + ab:

Example 3.10 Draw NFA corresponding to (0+1)*1(0+1):


• Exercise: Construct finite automata, deterministic or non-
deterministic for the following regular expressions.
• (a) a(a + b)*b
• (b) 1(1 + 0)*0 + 0
• (c) 0*10*
• (d) (0 + 1)*1(0 + 1)*
• (e) 0*(1 + ε)0*
• (f) (0 + 1)(0 + 1)(0 + 1) *

You might also like