regular expressions - mahesh jangid2011/07/01  · regular expressions a) find a regular expression...

36
Regular expressions

Upload: others

Post on 15-Mar-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Regular expressions - Mahesh Jangid2011/07/01  · Regular expressions a) Find a regular expression for representing the set L of strings in which every 0 is immediately followed by

Regular expressions

Page 2: Regular expressions - Mahesh Jangid2011/07/01  · Regular expressions a) Find a regular expression for representing the set L of strings in which every 0 is immediately followed by

Regular expressions

Page 3: Regular expressions - Mahesh Jangid2011/07/01  · Regular expressions a) Find a regular expression for representing the set L of strings in which every 0 is immediately followed by

Regular expressions

Page 4: Regular expressions - Mahesh Jangid2011/07/01  · Regular expressions a) Find a regular expression for representing the set L of strings in which every 0 is immediately followed by

Regular expressions

Page 5: Regular expressions - Mahesh Jangid2011/07/01  · Regular expressions a) Find a regular expression for representing the set L of strings in which every 0 is immediately followed by

Regular expressions

Page 6: Regular expressions - Mahesh Jangid2011/07/01  · Regular expressions a) Find a regular expression for representing the set L of strings in which every 0 is immediately followed by

Regular expressions

Page 7: Regular expressions - Mahesh Jangid2011/07/01  · Regular expressions a) Find a regular expression for representing the set L of strings in which every 0 is immediately followed by

Regular expressions

Page 8: Regular expressions - Mahesh Jangid2011/07/01  · Regular expressions a) Find a regular expression for representing the set L of strings in which every 0 is immediately followed by

Regular expressions

Page 9: Regular expressions - Mahesh Jangid2011/07/01  · Regular expressions a) Find a regular expression for representing the set L of strings in which every 0 is immediately followed by

Regular expressions

Page 10: Regular expressions - Mahesh Jangid2011/07/01  · Regular expressions a) Find a regular expression for representing the set L of strings in which every 0 is immediately followed by

Regular expressions

Page 11: Regular expressions - Mahesh Jangid2011/07/01  · Regular expressions a) Find a regular expression for representing the set L of strings in which every 0 is immediately followed by

Regular expressions

Page 12: Regular expressions - Mahesh Jangid2011/07/01  · Regular expressions a) Find a regular expression for representing the set L of strings in which every 0 is immediately followed by

Regular expressions

Page 13: Regular expressions - Mahesh Jangid2011/07/01  · Regular expressions a) Find a regular expression for representing the set L of strings in which every 0 is immediately followed by

Regular expressions

Describe the following languages by regular expressions

(a) L1=set of all strings of 0s and 1s ending in 00.

(b) L2=set of all strings of 0s and 1s beginning with 0 and ending with 1.

(c) L3={ε , 11, 1111,111111,…}

Solution:

(a) (0+1)* 00.

(b) 0(0+1)*1

(c) (11)*

Page 14: Regular expressions - Mahesh Jangid2011/07/01  · Regular expressions a) Find a regular expression for representing the set L of strings in which every 0 is immediately followed by

Regular expressions

Two regular expressions P and Q are equivalent if P and Qrepresent same set of strings. We write P = Q.

The following identities( I ) are useful for simplifying regular expressions:

1) Ø + R = R

2) ØR = RØ = Ø

3) εR = Rε = R

4) ε* = ε

5) R + R = R

6) R*R* = R*

7) RR* = R*R

8) (R*)* = R*

9) ε + RR* = R*

10) (PQ)*P = P(QP)*

11) (P+Q)* = (P*Q*)* = (P*+Q*)*

12) (P+Q)R = PR + QR

And R(P+Q) = RP + RQ

Page 15: Regular expressions - Mahesh Jangid2011/07/01  · Regular expressions a) Find a regular expression for representing the set L of strings in which every 0 is immediately followed by

Regular expressions

a) Find a regular expression for representing the set L of strings in which every 0 is immediately followed by at least two 1s.

b) Prove that the regular expression R = ε + 1*(011)*(1*(011)*)* also describes the same set of strings.

Solution:

a) (1 + 011)*

b) R = εεεε + PP* , where P = 1*(011)*

= P* using I9

= (Q*S*)* where Q=1, S=011

= (Q + S)* using I11

= (1 + 011)*

Page 16: Regular expressions - Mahesh Jangid2011/07/01  · Regular expressions a) Find a regular expression for representing the set L of strings in which every 0 is immediately followed by

Regular expressions

Prove (1+00*1) + (1 + 00*1)(0 +10*1)*(0 + 10*1) = 0*1(0 + 10*1)*

Solution:

L.H.S= (1 + 00*1)(εεεε + (0 + 10*1)*(0 + 10*1)) usingI 12

= (1 + 00*1)(0 + 10*1)* usingI 9

= (εεεε + 00*)1(0 + 10*1)* usingI 12

= 0*1(0+10*1)* usingI 9

= R.H.S

Page 17: Regular expressions - Mahesh Jangid2011/07/01  · Regular expressions a) Find a regular expression for representing the set L of strings in which every 0 is immediately followed by

Arden’s Theorem

Let P and Q be two regular expressions over Σ. If P does not contain εεεε, then the following equation in R,

R = Q + RP

has a unique solution (one and only one) given by R = QP*

Finite Automata and Regular Expressions

Every regular expression R can be recognized by a transition diagram. For every string there exists a path from the initial state to a final state.

Page 18: Regular expressions - Mahesh Jangid2011/07/01  · Regular expressions a) Find a regular expression for representing the set L of strings in which every 0 is immediately followed by

Finite Automata and Regular Expressions

A regular expression can be of the following forms:

1) R = P + Q

2) R = PQ

3) R = P*

Where P and Q are also regular expressions.

Assume that P and Q can be recognized by transition systems(FA) G and H respectively.

Page 19: Regular expressions - Mahesh Jangid2011/07/01  · Regular expressions a) Find a regular expression for representing the set L of strings in which every 0 is immediately followed by

Finite Automata and Regular Expressions

It is important to note that some authors use Λ (Lambda) symbol instead of εεεε.

Page 20: Regular expressions - Mahesh Jangid2011/07/01  · Regular expressions a) Find a regular expression for representing the set L of strings in which every 0 is immediately followed by

Regular Expression of a FA

Page 21: Regular expressions - Mahesh Jangid2011/07/01  · Regular expressions a) Find a regular expression for representing the set L of strings in which every 0 is immediately followed by

Regular Expression of a FA

Page 22: Regular expressions - Mahesh Jangid2011/07/01  · Regular expressions a) Find a regular expression for representing the set L of strings in which every 0 is immediately followed by

Regular Expression of a FA

Page 23: Regular expressions - Mahesh Jangid2011/07/01  · Regular expressions a) Find a regular expression for representing the set L of strings in which every 0 is immediately followed by

Regular Expression of a FA

Find the regular expression recognized by the following transition diagram.

Page 24: Regular expressions - Mahesh Jangid2011/07/01  · Regular expressions a) Find a regular expression for representing the set L of strings in which every 0 is immediately followed by

Regular Expression of a FA

We get the following equations:

As q1 is the only final state and the equation for q1 involves only q2 and q3, we use only q2 and q3 equations. Substituting for q2

and q3 in the equation for q1 , we get

Page 25: Regular expressions - Mahesh Jangid2011/07/01  · Regular expressions a) Find a regular expression for representing the set L of strings in which every 0 is immediately followed by

Regular Expression of a FA

Describe in English the set of strings accepted by the followingfinite automata

Page 26: Regular expressions - Mahesh Jangid2011/07/01  · Regular expressions a) Find a regular expression for representing the set L of strings in which every 0 is immediately followed by

As q1, q2 are final states

Regular Expression of a FA

Answer: Strings containing any number of 0 s followed by any number of 1 s.

Page 27: Regular expressions - Mahesh Jangid2011/07/01  · Regular expressions a) Find a regular expression for representing the set L of strings in which every 0 is immediately followed by

Regular Expression of a FA

Construct the regular expression corresponding to the following FA

Page 28: Regular expressions - Mahesh Jangid2011/07/01  · Regular expressions a) Find a regular expression for representing the set L of strings in which every 0 is immediately followed by

Regular Expression of a FA

Page 29: Regular expressions - Mahesh Jangid2011/07/01  · Regular expressions a) Find a regular expression for representing the set L of strings in which every 0 is immediately followed by

Regular Expression of a FA

Find the regular expression corresponding to the following FA

Page 30: Regular expressions - Mahesh Jangid2011/07/01  · Regular expressions a) Find a regular expression for representing the set L of strings in which every 0 is immediately followed by

Regular Expression of a FA

q1 = q10 + q30 + q40 + Λ , q2= q11 + q21 + q41

q3 = q20, q4 = q31

Page 31: Regular expressions - Mahesh Jangid2011/07/01  · Regular expressions a) Find a regular expression for representing the set L of strings in which every 0 is immediately followed by

FA of a Regular Expression

Construct an FA corresponding to the following RE.

(0+1)*(00+11)(0+1)*

First of all we need to construct a transition graph with Λmoves. Then all the Λ moves need to be removed after the construction of the graph.

Page 32: Regular expressions - Mahesh Jangid2011/07/01  · Regular expressions a) Find a regular expression for representing the set L of strings in which every 0 is immediately followed by

FA of a Regular Expression

Page 33: Regular expressions - Mahesh Jangid2011/07/01  · Regular expressions a) Find a regular expression for representing the set L of strings in which every 0 is immediately followed by

FA of a Regular Expression

After removing the Λmoves

What is the DFA corresponding to the above diagram?

Page 34: Regular expressions - Mahesh Jangid2011/07/01  · Regular expressions a) Find a regular expression for representing the set L of strings in which every 0 is immediately followed by

FA of a Regular Expression

Page 35: Regular expressions - Mahesh Jangid2011/07/01  · Regular expressions a) Find a regular expression for representing the set L of strings in which every 0 is immediately followed by

FA of a Regular Expression

As the last two rows of the transition table are identical, the two states can be combined into a single state as shown below.

Page 36: Regular expressions - Mahesh Jangid2011/07/01  · Regular expressions a) Find a regular expression for representing the set L of strings in which every 0 is immediately followed by

FA of a Regular Expression

Construct a DFA with reduced states equivalent to the regular expression 10 + (0 + 11)0*1