Вы находитесь на странице: 1из 23

THEORY OF COMPUTATION

CST-352

Regular Expressions

1
Regular Expressions vs. Finite Automata
• Offers a declarative way to express the pattern of any
string we want to accept
– E.g., 01*+ 10*

• Automata => more machine-like


< input: string , output: [accept/reject] >
• Regular expressions => more program syntax-like

• Unix environments heavily use regular expressions


– E.g., bash shell, grep, vi & other editors, sed
• Perl scripting – good for string processing
• Lexical analyzers such as Lex or Flex

University Institute of Engineering 2


Regular Expressions

Regular
expressions
= Finite Automata
(DFA, NFA, -NFA)
Syntactical
expressions Automata/machines

Regular
Languages

Formal language
classes

University Institute of Engineering 3


Regular Expressions

University Institute of Engineering


RE Examples
• L(001) = {001}
• L(0+10*) = { 0, 1, 10, 100, 1000, 10000, … }
• L(0*10*) = {1, 01, 10, 010, 0010, …} i.e. {w | w has exactly a single 1}
• L((0(0+1))*) = { ε, 00, 01, 0000, 0001, 0100, 0101, …}
• L((0+ε)(1+ ε)) = {ε, 0, 1, 01}
• L(1Ø) = Ø ; concatenating the empty set to any set yields the empty set.
• Rε = R
• R+Ø = R

• Note that R+ε may or may not equal R (we are adding ε to the language)
• Note that RØ will only equal R if R itself is the empty set.

University Institute of Engineering


RE Exercise
Describe the following sets by regular expressions:
• (a) L 1 =the set of all strings of O's and l's ending
in 00.
• (b) L2 = the set of all strings of O's and l's
beginning with 0 and ending with 1.
• (c) L3 ={^, 11, 1111, 111111, ... }.
• Solution:
• a) (0 + 1)* 00
• b) 0(0 + 1)* 1
• c) (11)*

University Institute of Engineering


IDENTITIES FOR REGULAR EXPRESSIONS

University Institute of Engineering


Arden’s Method

Example based on Arden’s theorem

University Institute of Engineering


Kleen’s theorem

University Institute of Engineering


Kleene Closure (the * operator)
• Kleene Closure of a given language L:
– L0= {}
– L1= {w | for some w  L}
– L2= { w1w2 | w1  L, w2  L (duplicates allowed)}
– Li= { w1w2…wi | all w’s chosen are  L (duplicates allowed)}
– (Note: the choice of each wi is independent)
– L* = Ui≥0 Li (arbitrary number of concatenations)
Example:
• Let L = { 1, 00}
– L0= {}
– L1= {1,00}
– L2= {11,100,001,0000}
– L3= {111,1100,1001,10000,000000,00001,00100,0011}
– L* = L0 U L1 U L2 U …

University Institute of Engineering 10


TRANSITION SYSTEM CONTAINING
MOVES

University Institute of Engineering


CONSTRUCTION OF FINITE AUTOMATA EQUIVALENT
TO A REGULAR EXPRESSION
• Construct the finite automaton equivalent to the
regular expression (0 + 1)*(00 + 11)(0 +1)*

• First of all we construct the transition graph with


moves using the constructions and then we eliminate
–moves.

University Institute of Engineering


Solution

University Institute of Engineering


• Now convert this NDFA to DFA.

University Institute of Engineering


• Construct a DFA with reduced states
equivalent to the r.e. 10 + (0 + 11)0*1.
• Solution:

University Institute of Engineering


University Institute of Engineering
DFA to RE construction
DFA Reg Ex
Theorem 1

Informally, trace all distinct paths (traversing cycles only once)


from the start state to each of the final states
and enumerate all the expressions along the way

Example: 1 0 0,1

q0 0 q1 1 q2

(1*) 0 (0*) 1 (0 + 1)*

1* 00* 1 (0+1)*

1*00*1(0+1)*
University Institute of Engineering 17
RE to -NFA construction
Reg Ex  -NFA
Theorem 2

Example: (0+1)*01(0+1)*

(0+1)* 01 (0+1)*

 


0  
0 
 0 1

 1
  1

 

University Institute of Engineering 18


What will be the RE if an Automaton
or FA is given.

• Firstly write down the equations for each state


of FA such that each transition should be
marked in the pair of (state, Input symbol).
Note: Null is included in the
equation of start state.

University Institute of Engineering


• By using the Arden’s Theorem:

University Institute of Engineering


Example 2: Construct a regular expression
corresponding to the state diagram described by

Arden’s Theorem

University Institute of Engineering


Arden’s Theorem

Note: If there are more than one final states in the given FA then the Regular
expression of the FA will be union of all the expressions evaluated for different final
states.
University Institute of Engineering
References :

• K.L.P MISHRA,“Introduction to computer


Theory” .
• https://youtu.be/yKOmggxrYmY
• https://youtu.be/cFd3pz5H-TA

University Institute of Engineering