0% found this document useful (0 votes)
75 views

Lecture05 RegularExpression&FA

The document discusses regular expressions and finite automata. It begins by defining regular expressions using primitive regular expressions and rules to combine them. It then defines the language of a regular expression and provides examples. The document shows how to construct non-deterministic finite automata (NFAs) that accept the languages of various regular expressions using operations like union, concatenation, and star. It states that every regular language can be defined by both a regular expression and an NFA. In the end, it asks how to convert between regular expressions and finite automata.

Uploaded by

ana42145577
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

Lecture05 RegularExpression&FA

The document discusses regular expressions and finite automata. It begins by defining regular expressions using primitive regular expressions and rules to combine them. It then defines the language of a regular expression and provides examples. The document shows how to construct non-deterministic finite automata (NFAs) that accept the languages of various regular expressions using operations like union, concatenation, and star. It states that every regular language can be defined by both a regular expression and an NFA. In the end, it asks how to convert between regular expressions and finite automata.

Uploaded by

ana42145577
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Formal Languages and Automata

Text Book:
“Theory of Computation an Introduction" by James L. Hein, Jones & Bartlett
Publishers 1996; ISBN: 0-86720-497-4.

1
Regular Expressions
A regular language is a language that can be defined by a regular
expression. A regular expression is often described by means of an
algebraic expression called a regular expression notation .

Definition: Let  be a given alphabet. Then


1- ,  and a   are regular expression. These are called primitive regular
expression.

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 be derived from the


primitive regular expression by a finite number of applications of the rules in 2.

2
Examples

A regular expression: (a + b  c ) * (c + )

Not a regular expression: (a + b + )

3
Languages of Regular Expressions

◼ L (r ) : language of regular expression r

◼ Example

L((a + b  c) *) =  , a, bc, aa, abc, bca,...

4
Definition

For primitive regular expressions:

L( ) = 

L( ) =  

L(a ) = a
5
Recursive Definition
,  , a
Primitive regular expressions:
Given regular expressions r1 and r2

r1 + r2
r1  r2
Are regular expressions
r1 *
(r1 )
6
Regular Expressions

◼ Regular expressions
❑ describe regular languages

◼ Example:
describes the language (a + b  c) *

a, bc* =  , a, bc, aa, abc, bca,...


7
Definition (continued)

◼ For regular expressions r1 and r2


L(r1 + r2 ) = L(r1 )  L(r2 )
L(r1  r2 ) = L(r1 ) L(r2 )
L(r1 *) = ( L(r1 )) *

L((r1 )) = L(r1 )

8
Example

◼ Regular expression: (a + b )  a *

L((a + b )  a *) = L((a + b )) L(a *)


= L(a + b ) L(a *)
= ( L(a )  L(b )) ( L(a )) *
= (a  b) (a) *
= a, b , a, aa, aaa,...
= a, aa, aaa,..., b, ba, baa,...

9
Example

◼ Regular expression r = (aa ) * (bb ) * b

L(r ) = {a b
2n 2m
b : n, m  0}

10
Properties of Regular Languages

For regular languages L1 and L2


we have

Union: L1  L2

Concatenation: L1L2 Are regular


Languages
Star: L1 *
11
Theorem

Languages
Generated by
Regular Expressions
= Regular
Languages

12
Equivalent Regular Expressions

◼ Definition:

Regular expressions r1 and r2

are equivalent if

L(r1) = L(r2 )

13
Example

L = {all strings with no two consecutive 0 }

r1 = (1 + 01) * (0 +  )
r2 = (1* 011*) * (0 +  ) + 1* (0 +  )
r1 and r2
L(r1) = L(r2 ) = L are equivalent
regular expr.
14
Example

◼ Regular expression r = (0 + 1) * 00 (0 + 1) *

L(r ) = { all strings with at least


two consecutive 0 }

15
Regular Expression and FA

Every language that can be defined by a


regular expression can also be defined by a
finite automaton.

So we will see how to convert Regular


expression to a FA, then how to convert FA to
Regular expression.

16
Regular Expression and FA

◼ Primitive Regular Expressions: ,  , 


L( M1) =  = L()

regular
L( M 2 ) = {} = L( )
languages

a
L( M 3 ) = {a} = L(a)
17
Union

◼ NFA for L1  L2 M1

 

 M2 

18
Incorrect Union
a
A = {an | n is odd}
a
b
B = {bn | n is odd}
b

a
a
AB?
b
No: this NFA accepts aab
b

19
Correct Union
a
A = {an | n is odd}
a
b
B = {bn | n is odd}
b
a


a
AB
b

b

20
Example


NFA for L1  L2 = {a b}  {ba}
n

L1 = {a nb}
a

 b 

L2 = {ba} 
b a

21
Concatenation

◼ NFA for L1L2

M1 M2
 

22
Example

a
M1
L1 = {a b}
n
b

M2
L2 = ba b a

23
Example

◼ NFA for L1L2 = {a nb}{ba} = {a nbba}

L1 = {a b}
n

a L2 = {ba}
b  b a 

24
Incorrect Concatenation
a
A = {an | n is odd}
a
b
B = {bn | n is odd}
b

a b

a b
{xy | x  A and y  B} ?
No: this NFA accepts abbaab

25
Correct Concatenation
a
A = {an | n is odd}
a
b
B = {bm | m is odd}
b


a b

a b
{xy | x  A and y  B}

26
Star Operation

◼ NFA for L1 *
   L1 *
M1
 


27
Example

◼ NFA for L1* = {a nb} *


L1 = {a b} n

a
 b 


28
Example
Words that begin and end with the same letter. a(a+b)*a + b(a+b)*b

a,b

a a

b b
a,b a,b a,b
 aba 
Words that contain aba.
(a+b)*aba(a+b)*

29
Example

 


a b
a,b
a,b a,b

aba
a b a,b


(a+b)*aba(a+b)* + a(a+b)*a + b(a+b)*b

30
Example

a,b a,b a,b


 aba
a 2
a
 
1

b b
a,b
(a(a+b)*a + b(a+b)*b)((a+b)*aba(a+b)*)

31
Example

 a,b (a(a+b)*a + b(a+b)*b)*

a
a

– +

b b a,b
 a,b
a,b  aba
 
((a+b)*aba(a+b)*)* +

32
Regular Expressions to Finite Automata

NFA

Regular
expressions DFA

Regular Implementation of DFA


languages

33
Regular Expression and FA

Every language that can be defined by a


regular expression can also be defined by a
finite automaton.

So we were able to see how to convert Regular


expression to a FA, then we will see how to
convert FA to Regular expression.

34
◼ Describe the language of the following regular
expressions, and construct the NFA for E
◼ E = b* + ab + c*b
◼ E = (a*c). (ad)

35
Answer for the language only:

◼ For E = b* + ab + c*b the answer is


❑ L={L1∪L2 ∪L3} L1={bn: n>=0} L2={ab} L3={cnb :
n>=0}.

◼ For E = (a*c). (ad) the answer is


❑ L={L1.L2} L1={anc : n>=0} L2={ad}

36
Main Purpose
4. From Regular Expression To DFAs

◼ Study an algorithm:
❑ Translating a regular expression into a DFA via
NFA.

Regular Program
NFA DFA
Expression

37
The Idea of Thompson’s Construction
4.1 From a Regular Expression to an NFA

◼ Use ε-transitions
❑ to “glue together” the machine of each piece of a regular
expression
❑ to form a machine that corresponds to the whole expression
◼ Basic regular expression
❑ The NFAs for basic regular expression of the form a, ε,or φ

a

4.1 From a Regular Expression to an NFA
The Idea of Thompson’s Construction
◼ Concatenation: to construct an NFA equal to rs
❑ To connect the accepting state of the machine of r to the
start state of the machine of s by an ε-transition.
❑ The start state of the machine of r as its start state and the
accepting state of the machine of s as its accepting state.
❑ This machine accepts L(rs) = L(r)L(s) and so corresponds to
the regular expression rs.


r s
… …
4.1 From a Regular Expression to an NFA
The Idea of Thompson’s Construction

◼ Choice among alternatives: To construct an NFA


equal to r | s
❑ To add a new start state and a new accepting state and
connected them as shown usingε-transitions.
❑ Clearly, this machine accepts the language L(r|s) =L(r
)UL ( s), and so corresponds to the regular expression
r|s.
r

 

 

s

4.1 From a Regular Expression to an NFA
The Idea of Thompson’s Construction
◼ Repetition: Given a machine that corresponds to r,
Construct a machine that corresponds to r*
❑ To add two new states, a start state and an accepting state.
❑ The repetition is afforded by the newε-transition from the
accepting state of the machine of r to its start state.
❑ To draw an ε-transition from the new start state to the new
accepting state.
❑ This construction is not unique, simplifications are possible in the
many cases.

 
r


4.1 From a Regular Expression to an NFA
Examples of NFAs Construction
Example 1.12: Translate regular expression ab|a into NFA
a

a  b

a  b
 

 
a
4.1 From a Regular Expression to an NFA
Examples of NFAs Construction
Example 1.13: Translate regular expression letter(letter|digit)* into NFA
letter
letter
 

digit
 
letter

letter
 
 
 
letter 

letter
  
letter   
 
letter


44

You might also like