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

Automata Lecture 03 RE

This regular expression captures strings over the alphabet {0, 1} that have an even number of 0s by matching pairs of 0s and allowing them to repeat zero or more times.

Uploaded by

zone.raihan
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Automata Lecture 03 RE

This regular expression captures strings over the alphabet {0, 1} that have an even number of 0s by matching pairs of 0s and allowing them to repeat zero or more times.

Uploaded by

zone.raihan
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

School of Engineering & Computer Science

Independent University, Bangladesh (IUB)

Regular Expressions

M Ashraful Amin, PhD


Alphabets and Languages

Alphabets: a finite set of symbols. Denoted my ∑


Example: Roman alphabet ∑ = {a,b,…,z}, binary alphabet ∑={0,1}

A String over an alphabet is a finite sequence of symbols


from the alphabet.
Alphabets and Languages…
String concatenation
Two strings over the same alphabet can be combined
to form a third string by the operation concatenation

s = abb t = bab st = abbbab


ts = bababb
ss = abbabb
sst = abbabbbab

s = x1…xn t = y1…ym st = x1…xny1…ym

Discuss: Substring, prefix, suffix


Operations on languages

L1L2 = {st: s  L1, t  L2}

Ln = {s1s2...sn: s1, s2, ..., sn  L}

L1  L2 = {s: s  L1 or s  L2}
Example

L1 = {0, 01} L2 = {, 1, 11, 111, …}


any number of 1s

L1L2 = {0, 01, 011, 0111, …} {01, 011, 0111, …}


= {0, 01, 011, 0111, …}
0 followed by any number of 1s

L12 = {00, 001, 010, 0101} L22 = L2


L2n = L2 (n ≥ 1)

L1  L2 = {0, 01, ,1, 11, 111, ...}


Operations on languages
• The star of L are all strings made up of zero or more
chunks from L:

L* = L0  L1  L2  …

• Example: L1 = {01, 0}, L2 = {, 1, 11, 111, …}.


What is L1* and L2*?
Example

L1 = {0, 01} L1*: 00100001 is in L1*


00110001 is not in L1*
10010001 is not in L1*
L10 = {}
L1* are all strings that start
L11 = {0, 01}
with 0 and do not contain
L12 = {00, 001, 010, 0101} consecutive 1s
(plus the empty string)
L13= {000, 0001, 0010, 00101,
0100, 01001, 01010, 010101}
Example

L2 = {, 1, 11, 111, …}


any number of 1s

L20 = {} L2* = L20  L21  L22  …


L21 = L2 = {}  L21  L22  …
L22 = L2 = L2
L2n = L2 (n ≥ 1)
L2* = L2
Combining languages
• We can construct languages by starting with simple
ones, like {0}, {1} and combining them

{0}({0}{1})* 0(0 + 1)*


all strings that start with 0

({0}{1}*)({1} 01* + 10*


{0}*) 0 followed by any number of 1s, or
1 followed by any number of 0s
Regular expressions
• A regular expression over  is an expression formed
using the following rules:
– The symbols  and  are regular expressions
– Every a in  is a regular expression
– If R and S are regular expressions, so are R+S, RS and R*.
(0 +
1 *(  1)*0
 0(0 + + 0) 1( 0
1)* 0 * + 1)
 * + 1 *
01

A language is regular if it is represented by a


regular expression
Analyzing regular expressions
 = {0, 1}

01* = 0(1*) = {0, 01, 011, 0111, …}

0 followed by any number of 1s

(01*) = {001, 0101, 01101, 011101, …}


(01)
0 followed by any number of 1s and then 01
Analyzing regular expressions

0+1 = {0, 1} strings of length 1

(0+1)* = {, 0, 1, 00, 01, 10, 11, …} any string

(0+1)*010 any string that ends in 010

(0+1)*01(0+1)* any string that contatins the pattern 01


Analyzing regular expressions

((0+1)(0+1))*+((0+1)(0+1)
(0+1))*
all strings whose length is even or a mutliple of 3
= strings of length 0, 2, 3, 4, 6, 8, 9, 10, 12, ...

((0+1)(0+1))* strings of even length


(0+1)(0+1) strings of length 2

((0+1)(0+1)(0+1))* strings of length a multiple of 3

(0+1)(0+1)(0+1) strings of length 3


Analyzing regular expressions

((0+1)(0+1)+(0+1)(0+1)(0+1))*
strings that can be broken in blocks,
where each block has length 2 or 3

(0+1)(0+1)+(0+1)(0+1)(0+1) strings of length 2 or 3

(0+1)(0+1) strings of length 2

(0+1)(0+1)(0+1) strings of length 3


Analyzing regular expressions

((0+1)(0+1)+(0+1)(0+1)(0+1))*
strings that can be broken in blocks,
where each block has length 2 or 3

✓ 1✗ 10 ✓ 011✓ 00110 ✓ 011010110✓

this includes all strings except those of length 1

((0+1)(0+1)+(0+1)(0+1)(0+1))* = all strings except 0 and 1


Analyzing regular expressions

(1+01+001)*(+0+00)
ends in at most two 0s
there can be at most two 0s between
consecutive 1s

there are never three consecutive 0s

Guess: (1+01+001)*(+0+00) = {x: x does not contain 000}

 00 0110010110 0010010
Writing regular expressions
• Write a regular expression for  = {0, 1}
all strings with two consecutive 0s.

(anything) 00 (anything else)

(0+1)*00(0+1)*
Writing regular expressions
• Write a regular expression for  = {0, 1}
all strings that do not contain two consecutive 0s.

1110110101101010
some 1s at every 0 maybe a 0 at the end
the beginning followed by
one or more 1s
... every middle 0 followed by one or more 1s (011*)

... and at most one 0 in the last block ( + 0)

1*(011*)*( + 0)
Writing regular expressions
• Write a regular expression for  = {0, 1}
all strings with an even number of 0s.

even number of zeros = (two zeros)*

two zeros = 1*01*01*

(1*01*01*)*

You might also like