Regular expressions

RatnakarMikkili 4,383 views 22 slides Aug 04, 2019
Slide 1
Slide 1 of 22
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22

About This Presentation

Regular Expressions


Slide Content

REGULAR EXPRESSIONS M RATNAKAR BABU Asst.Prof . CSE

Introduction The Language accepted by finite automata are easily described by simple expressions called regular expressions . The regular expression is the most effective way to represent any language. The language accepted by some regular expression is known as a regular language .

Regular Expressions Any terminal symbol(i.e. an element of ∑), Ꜫ and φ are regular expressions. The union of two regular expressions R 1 and R 2 , written as R 1 +R 2 , is also a regular expression. The concatenation of two regular expressions R 1 and R 2 , written as R 1 R 2 , is also a regular expression. The iteration(or Closure) of a regular expression R, written as R* is also a regular expression. If R is a regular expression , then (R) is also a regular expression.

Identity Rules : Let P,Q and R are the regular expressions then the identity rules are as follows: φ +R = R φ R = R φ = φ Ꜫ R = R Ꜫ = R Ꜫ * = Ꜫ and φ *= Ꜫ R + R = R R*R* = R* RR* = R*R (R*)*=R* Ꜫ + RR*= R* = Ꜫ +R*R (PQ)*P = P(QP)* (P+Q)* = (P*Q*)* = (P* + Q*) (P+Q)R = PR + QR and R(P+Q) = RP + RQ Regular Expressions

Exercise 2: Construct the regular expression for the language which accepts all the strings which begin or end with either 00 or 11. Solution: The R.E. can be categorized into two subparts. R = L 1 + L 2 L1 = The String which begin with 00 or 11. L2 = The String which end with 00 or 11. Let us find out L 1 and L 2. L 1 = (00+11)( any number of 0’s and 1’s ) L 1 = (00+11)(0+1)* L 2 = ( any number of 0’s and 1’s ) (00+11) L 2 = (0+1)*(00+11) Hence R = [ (00+11)(0+1)*] + [(0+1)*(00+11)] Regular Expressions

Algorithm to build R.E. from given DFA Let q 1 be the initial state. There are q 2 ,q 3 ,q 4 ,…. q n number of states. The final state may be some q j where j<=n. Let  ji represents the transition from qj to qi. Calculate qi such that q i =  ji . q j If qi is a start state q i =  ji . q j +Ꜫ 5. Similarly compute the final state which ultimately gives the regular expression r.

Exercise 1 : Write a regular expression for the set of strings that contains an even number of 1’s over  ={0,1}. Treat zero 1’s as an even number. Solution: The regular expression for the above F.A. is 0*10*1 Regular Expressions Q Q 1 1 1

DFA to RE via State Elimination (1) Starting with intermediate states and then moving to accepting states, apply the state elimination process to produce an equivalent automaton with regular expression labels on the edges. The result will be a one or two state automaton with a start state and accepting state.

DFA to RE State Elimination (2) If the two states are different, we will have an automaton that looks like the following: We can describe this automaton as: (R+SU*T)*SU*

DFA to RE State Elimination (3) If the start state is also an accepting state, then we must also perform a state elimination from the original automaton that gets rid of every state but the start state. This leaves the following: We can describe this automaton as simply R*.

DFA to RE State Elimination (4) If there are n accepting states, we must repeat the above steps for each accepting states to get n different regular expressions, R 1 , R 2 , … R n . For each repeat we turn any other accepting state to non-accepting. The desired regular expression for the automaton is then the union of each of the n regular expressions: R 1  R 2 …  R N

DFA RE Example Convert the following to a RE First convert the edges to RE’s:

DFA  RE Example (2) Eliminate State 1: To: Start 3 1 2 1 1 0+1 3 Start 2 11 0+10 0+1 Note edge from 3 3 Answer: (0+10)*11(0+1)*

Second Example Automata that accepts even number of 1’s Eliminate state 2: 1 Start 2 3 1 1 1 0+10*1 1 Start 3 10*1

Second Example (2) Two accepting states, turn off state 3 first 1 Start 3 0+10*1 10*1 This is just 0*; can ignore going to state 3 since we would “die” 1 Start 3 0+10*1 10*1

Second Example (3) Turn off state 1 second: 1 Start 3 0+10*1 10*1 This is just 0*10*1(0+10*1)* Combine from previous slide to get 0* + 0*10*1(0+10*1)* 0+10*1 1 Start 3 10*1

Converting a RE to an Automata We have shown we can convert an automata to a RE. To show equivalence we must also go the other direction, convert a RE to an automaton. We can do this easiest by converting a RE to an ε -NFA

RE to ε -NFA Basis: R= a R= ε ε R= Ø a Next slide: More complex RE’s

R= S+T S T ε ε ε ε R=ST S T ε R=S * ε S ε ε ε

RE to ε -NFA Example Convert R= ( ab+a )* to an NFA We proceed in stages, starting from simple elements and working our way up a a b b ab a b ε

RE to ε -NFA Example (2) ab+a ( ab+a )* a b ε a ε ε ε ε a b ε a ε ε ε ε ε ε ε ε

End for Now Continue in Next Class