27
CSCI 3130: Formal languages and automata theory Tutorial 2 Chin

CSCI 3130: Formal languages and automata theory Tutorial 2 Chin

Embed Size (px)

Citation preview

Page 1: CSCI 3130: Formal languages and automata theory Tutorial 2 Chin

CSCI 3130: Formal languagesand automata theory

Tutorial 2

Chin

Page 2: CSCI 3130: Formal languages and automata theory Tutorial 2 Chin

Reminder

• Homework 1 is due at 23:59, today!

• Subscribe Moodle for tutorial information• My office hour: 11:00 – 12:00, Tuesday

Page 3: CSCI 3130: Formal languages and automata theory Tutorial 2 Chin

Outline

• Regular expression• Closure properties

• In this (only this) tutorial. Assume = {0, 1}.

Page 4: CSCI 3130: Formal languages and automata theory Tutorial 2 Chin

Regular Expressions

• 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*

• Remember for R*, * could be 0

Page 5: CSCI 3130: Formal languages and automata theory Tutorial 2 Chin

Regular Expressions

• What are their regular expressions?1. L1 = {w : w is a string of even length}

((0 + 1)(0 + 1))*2. L2 = {w : w begins with 0 or ends with 0}

0(0 + 1)* + (0 + 1)*03. L3 = {w : w contains the pattern 01 at least once}

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

Page 6: CSCI 3130: Formal languages and automata theory Tutorial 2 Chin

Regular Expressions

• What are their regular expressions?1. L1 = {w : w is a string of odd length}

2. L2 = {w : w begins with 0 or ends with 0 but not both}

3. L3 = {w : w contains the pattern 01 at least twice}

4. L4 = {w : w contains less 1s than 0s and at most two 0s}

Page 7: CSCI 3130: Formal languages and automata theory Tutorial 2 Chin

Regular Expressions

1. L1 = {w : w is a string of odd length}

odd means of length 2n + 1string of length 2n = ((0 + 1)(0 + 1))*

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

Page 8: CSCI 3130: Formal languages and automata theory Tutorial 2 Chin

Regular Expressions

2. L2 = {w : w begins with 0 or ends with 0 but not both}

w must be of length at least 2Either starts with 0 and ends with 1, or

starts with 1 and ends with 0

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

Page 9: CSCI 3130: Formal languages and automata theory Tutorial 2 Chin

Regular Expressions

3. L3 = {w : w contains the pattern 01 at least twice}

w must contain 01 twice in the expressioncan be anything before, after, or between the two

01s

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

Page 10: CSCI 3130: Formal languages and automata theory Tutorial 2 Chin

Regular Expressions

4. L4 = {w : w contains less 1s than 0s and at most two 0s}

There can be at most two 0sEnumerate all possible cases:0, 00, 001, 010, 100OR them using +

0 + 00 + 001 + 010 + 100

Page 11: CSCI 3130: Formal languages and automata theory Tutorial 2 Chin

Regular Expressions

• What do these regular expressions represent?1. 0(0 + 1)*0 + 1(0 + 1)*1 + 0 + 12. 1*(011*)*3. (0*10*10*)*4. ((0 + 1)1)*

Page 12: CSCI 3130: Formal languages and automata theory Tutorial 2 Chin

Regular Expression

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

First alphabet must be equal to the last alphabet

Page 13: CSCI 3130: Formal languages and automata theory Tutorial 2 Chin

Regular Expression

2. 1*(011*)*011* = start with 0 then followed by at least one 1(011*)* = start with 0 and every 0 must be

followed by at least one 11* at the beginning = possibly start with 1s

Every 0 is followed by at least one 1

Page 14: CSCI 3130: Formal languages and automata theory Tutorial 2 Chin

Regular Expression

3. (0*10*10*)*There are two 1s inside the brackets0*10*10* = there can be 0 before, after, or

between the two 1s = there are two 1s(0*10*10*)* = there are 2n 1s

All strings that contain even number of 1s

Page 15: CSCI 3130: Formal languages and automata theory Tutorial 2 Chin

Regular Expression

4. ((0 + 1)1)*(0 + 1)1 = X1, X = 0/1((0 + 1)1)* = strings of the form X1X1…X1, X = 0/1

Strings of even length and every even position is 1

Page 16: CSCI 3130: Formal languages and automata theory Tutorial 2 Chin

Closure Properties

• Suppose L and L’ are both regular1. L is also regular2. LR is also regular3. L ∪ L’ is also regular4. L ∩ L’ is also regular

Page 17: CSCI 3130: Formal languages and automata theory Tutorial 2 Chin

Closure Properties

• Suppose L and L’ are both regular. ThenL and L’ are also regularL ∪ L’ is also regular(L ∪ L’) is also regular L ∩ L’ = (L ∩ L’) by De Morgan’s law

4. Hence L ∩ L’ is also regularThe construction idea in lecture is also important.

Page 18: CSCI 3130: Formal languages and automata theory Tutorial 2 Chin

Closure Properties

Let L be a language comprising all strings w such that 1.w contains an even number of 1s,2.an odd number of 0s, and3.no occurrences of the substring 10.

Is L regular?

Page 19: CSCI 3130: Formal languages and automata theory Tutorial 2 Chin

Closure Properties

• To show a language L is regular,

1.represent L using a DFA / NFA / RE, or2.use closure properties

Page 20: CSCI 3130: Formal languages and automata theory Tutorial 2 Chin

Closure Properties

1. L1 = {w : w contains an even number of 1s}

regular2. L2 = {w : w contains an odd number of 0s}

regular3. L3 = {w : w contains the substring 10}

regular

Page 21: CSCI 3130: Formal languages and automata theory Tutorial 2 Chin

Closure Properties

Let L be a language comprising all strings w such that 1.w contains an even number of 1s,2.an odd number of 0s3.no occurrences of the substring 10.

Is L regular? L = L1∩L2∩L3. Hence it is regular.

Page 22: CSCI 3130: Formal languages and automata theory Tutorial 2 Chin

Regular Expression (Extra)

• L = {1ky : y {0, 1}* and y contains at least ∈ k 1s, k ≥ 1}

• What is the regular expression of L?

Page 23: CSCI 3130: Formal languages and automata theory Tutorial 2 Chin

Regular Expression (Extra)

• L = {1ky : y {0, 1}* and y contains at least ∈ k 1s, k ≥ 1}

e.g. 111 0001111 (k = 3) is in L.1k y

Page 24: CSCI 3130: Formal languages and automata theory Tutorial 2 Chin

Regular Expression (Extra)

• L = {1ky : y {0, 1}* and y contains at least ∈ k 1s, k ≥ 1}

• What about 111 00011?• Same as 1 1100011. Hence is in L!• L = Strings that start with 1 and contain at least

two 1s

1k y

Page 25: CSCI 3130: Formal languages and automata theory Tutorial 2 Chin

Non-regular languages

• Homework 1 Problem 4• What happens if we allow nested parans?

Page 26: CSCI 3130: Formal languages and automata theory Tutorial 2 Chin

Non-regular languages

• Consider the case ((((( )))))• same as• L = {(n )n : n ≥ 0}• L’ = {0n1n : n ≥ 0} is NOT regular (lecture)

• More on non-regular languages next week.

Page 27: CSCI 3130: Formal languages and automata theory Tutorial 2 Chin

The End

• Questions?