Lab3 - nil PDF

Title Lab3 - nil
Course Theoretical Aspects of Computer Science
Institution Coventry University
Pages 8
File Size 351.9 KB
File Type PDF
Total Downloads 101
Total Views 140

Summary

nil...


Description

Lab 3: Converting NFAs to DFAs

380CT

(1) Use the subset construction method to convert the following NFA to an equivalent DFA. b

B

a

a

start

D

b

F

b

a

A

b b

C a

Give an informal description of the language recognized by this NFA/DFA. (2) Design an NFA that accepts strings over {a, b} which end with aaa, then convert it to an equivalent DFA. (3) Design an NFA that accepts strings over {0, 1} which have 1 in the second position from the end (e.g. 0010, 1011,10, etc.), then convert it to an equivalent DFA. (4) Convert the following ε-NFA to an equivalent DFA. start

1

b

ε

a 2

a,b

a

1

3

Lab 3: Regular Expressions

380CT

(1) (Regular Expressions in practice) Suppose we have a programming language where comments are delimited by @= and =@. Let L be the language of all valid delimited comment strings, i.e. a member of L must begin with @= and end with =@. Use the page at https://regex101.com/r/Ez1kqp/3 and try the following RegEx searches: Programming @ @= . .* @.* @.*|.*@ @.*@ @=.*=@

380CT notation Interpreation @ Just the symbol @ @= Just the string @= Σ Any symbol from the alphabet ∗ Σ Any string over the alphabet ∗ @Σ ....................................... ∗ @Σ + ∗Σ@ ....................................... ∗ @Σ @ ....................................... ∗ @=Σ =@ .......................................

Interpret the results for the last 4 searches. Try alternative searches to develop your understanding of how RegEx is used in practice. What is the correct RegEx for L? N.B. Please note that the regular expressions used in programming languages are more general than RegEx’s defined for Regular Languages. See for example https://en.wikipedia.org/wiki/RE2_(software) (2) Complete the descriptions of the following regular expressions (write in the gray boxes). Assume the alphabet Σ= {0, 1} in all the parts. Recall that, unless brackets are used to explicitly denote precedence, the operators precendence for the regular operations is: star, concatenation, then union. a) 01 + 10 = {

,

}

b) (ε + 0)(ε + 1) = {ε, 0, 1,

}

c) (0 + ε)1∗ = 01∗ + 1∗ = {w | w has at most d) ∗Σ0 = {w | w ends with a

} = {w | w respresents an

e) 0∗ 10∗ = {w | w contains a single ∗ f) ∗Σ0Σ = {w | w has at least one

number in binary}

}

}

∗ g) ∗Σ001Σ = {w | w contains the string

h) ∗Σ000∗∗Σ= {w | w cotains at least i) (011∗ )∗ = {w | every

and is at the start of w}

as a substring} consective

’s}

in w is followed by at least one

j) Σ + Σ= Σ (ε + Σ ) = {w | the length of w is exactly k) (Σ )∗ = {w | w is a string of Σ

or

}

}

length}

l) (Σ )∗ = {w | the length of w is a multiple of Σ

}

∗ ∗ m) 0Σ 0 + 1Σ 1 + 0 + 1 = {w | w starts and ends with the

symbol}

n) 1∗ ∅ = ∅ . . . why!? (Concatenating the empty RegEx ∅ to any RegEx yields the empty RegEx again) o) ∅∗ = {ε}

. . . why!?

For the last two, you may find it helpful to construct the corresponding ε-NFAs.

2

Lab 3: Regular Expressions

380CT

(3) Produce a regular expression for the following languages over the alphabet {a, b} a) The language La of all strings that start with a. b) The language Lb of all strings that end with b. c) The union La ∪ Lb . d) The concatenation La Lb . e) L = (La ∪ Lb )La Lb . f) The closure of L: L∗ . Produce ε-NFAs for each of the above using the constructions shown in the lecture for the union, concatenation, and star. You can refer to the last page for the graphical illustrations for these constructions. (4) For each of the following RegEx’s, give two strings that are members of the corresponding language, and two strings that are not. (A total of 4 strings for each part.) Assume the alphabet Σ = {a, b} in all the parts. a) a∗ b∗ b) a(ba)∗ b c) a∗ + b∗ d) (aaa)∗ e) Σ∗ aΣ∗ bΣ∗ aΣ∗ f) aba + bab g) (ε + a)b h) (a + ba + bb)Σ∗ (5) Give regular expressions generating the languages below over Σ= {0, 1} a) {w | w begins with 1 and ends with a 0} b) {w | w contains at least three 1’s} c) {w | w contains the substring 0101} d) {w | w has length at least 3 and its third symbol is 0} e) {w | w starts with 0 and has odd length, or starts with 1 and has even length} f) {w | w does not contain the substring 110} g) {w | the length of w is at most 5} h) {w | w is any string except 11 and 111} i) {w | every odd position of w is 1} j) {w | w contains at least two 0’s and at most one 1} k) {ε, 0} l) {w | w contains an even number of 0’s, or contains exactly two 1’s} m) The empty set. n) All strings except the empty string.

3

Lab 3: NFA to GNFA to RegEx

380CT

Reminder: We can convert any NFA into a GNFA as follows: • Add a new start state with an ε-transition to the NFA’s start state. • Add a new accept state with ε-transitions from the NFA’s accept states. • If a transition has multiple labels then replace them with their union. (e.g. a, b → a + b.) Once the GNFA is produced, start removing states, one at a time, and “patch” any affected transitions using regular expressions (RegEx’s). Repeat until only two states (initial and accept) remain. The RegEx on the only remaining transition is the equivalent RegEx to the NFA. (1) Use the GNFA algorithm to find regular expressions for the languages recognized by the following NFAs. Can you interpret the results? S a,b a

a

1

b

1

1

1 D

b a

b

a,b

B

A

a,b

1

B

a

a

a,b

start

a,b

A

b

D

a,b

b

C b

start

b

1

a

a

a

0

b

3

2

a

b

4 a

4

b

5

a

Lab 3: NFA to GNFA to RegEx

380CT

(2) Give RegEx’s for the languages recognized by the following similar NFAs, using the GNFA algorithm. What do you notice? 0

0

ε

1

0

0

ε

1

1

0

ε

1

1

0

1

(3) Let Ln be the language of all strings over Σ= {1} that have length a multiple of n, where n is a natural number (i.e. n ∈ N = {1, 2, 3, . . .}). a) Design an NFA to recognize L3 , and another to recognize L5 . b) Write down RegEx’s for L3 and L5 , then for their union L3 ∪ L5 . c) Construct the ε-NFA that recognizes L3 ∪ L5 . d) Use the GNFA algorithm to obtain a RegEx for L3 ∪ L5 . (4) Let Bn = {am | m is a multiple of n} = {akn | k ∈ Z≥0 } over the alphabet Σ= {a}. Show that the language Bn is regular for any n ∈ N by writing a regular expression for it. Outline the description of an NFA that can recognize it. (5) (Closure of regular languages under reversal of strings) For any string s = s1 s2 . . . sn , where si are symbols from the alphabet, the reverse of s is the string s written in reverse order: sR = sn sn−1 . . . s1 . Given an NFA or RegEx that recognizes a language A, describe how you can transform this NFA/RegEx to recognize the language AR = {wR | w ∈ A}, i.e. the language that contains all the strings from A but in the reverse order. Hint: Test your ideas on the languages given by the RegEx’s: (Σ = {a, b}) a, b, aa, ab, aa + bb, ab + bb, a∗ b∗ , Σ∗ a, aΣ∗ , ab∗ a∗ b, (ab)∗ , (aa + bb)∗ , (ab + bb)∗ .

5

Lab 3: Extra exercises

380CT

(1) Which of the strings listed below does the following NFA accept? 0

start

A

0

1 1 0

B

C 1

• 10011010 • 01010011 • 00010111 • 0010010 Now, convert the above NFA into a DFA. Which of the following sets of NFA states is not a state of the DFA that is accessible from the start state of the DFA? • {A, C } • {B, C } • {A} • {B } (2) Let Σ= {0, 1} and let D = {w | w contains an equal number of the occurances of the substrings 01 and 10}. As an example, 101 ∈ D but 1010 6∈ D. Show that D is a regular language (by producing an NFA for it, or otherwise). Does this hold for {w | w contains an equal number of 0’s and 1’s} ? Can you see why? What is the difference!? How about the language {w | w contains a non-equal number of 0’s and 1’s} ?

6

Lab 3: Extra exercises

380CT

(3) Intersection, union and difference of DFAs Given two languages described by two DFAs, the idea is to construct a new DFA that simulates simultaneously-running the given DFAs. To do this, the states of the new DFA will be pairs of states from the original DFAs. Suppose M1 = (Q1 , Σ , δ1 , q0,1 , F1 ) and M2 = (Q2 , Σ , δ2 , q0,2 , F2 ) are DFAs recognizing L1 and L2 , respectively. Let M be the DFA given by (Q, Σ , δ, q0 , F ) where Q = Q1 × Q2 q0 = (q0,1 , q0,2 ) and the transition function δ is defined by     δ (p, q ), σ = δ1 (p, σ ), δ2 (q, σ) for all (p, q) ∈ Q1 × Q2 and σ ∈ .Σ Then • M recognizes L1 ∩ L2 if F = {(p, q) | p ∈ F1 ∧ q ∈ F2 } = F1 × F2 • M recognizes L1 ∪L2 if F = {(p, q) | p ∈ F1 ∨q ∈ F2 } = (F1 × Q2 )∪(Q1 × F2 ) • M recognizes L1 − L2 if F = {(p, q) | p ∈ F1 ∧ q 6∈ F2 } = F1 × (Q2 − F2 ) a) Let the languages L1 and L2 be given by the two RegEx’s: b∗ aΣ∗ and a∗ bΣ∗ , respectively, where Σ = {a, b}. First, produce state diagram for DFAs recognizing L1 and L2 , then use the above method to construct a DFA for L1 ∩ L2 = {w | w has at least one a and at least one b}, then outline how it can be changed for L1 ∪ L2 and L1 − L2 . b) Use the same method for the language {w | w has an even number of a’s and each ais followed by at least one b}. c) (Complement of a language) ∗ In the special case where L1 = L(Σ ), i.e. the language of all possible strings over Σ , we get   M1 = (Q1 , Σ , δ1 , q01 , F1 ) = {q01 }, ,Σ(q, σ) 7→ q01 , q01 , {q01 }

This choice for M1 provides us with a method to produce the complement of ∗ L2 which is defined as L(Σ ) − L2 = L1 − L2 . Now, note that Q = Q1 × Q2 = {q01 } × Q2 F

= F1 × (Q2 − F2 ) = {q01 } × (Q2 − F2 )

means that the new DFA is essentially the DFA for L2 but with the accepting and non-accepting states “flipped.” (swapping roles.) Use this observation to produce a DFA for the language {w | w does not contain the substring baba.} This does not always work for NFAs – give an example to show that it does not work. (Counterexample) 7

Appendix: ε-NFA constructions for the regular operations

Union:

Concatenation:

Star:

8

380CT...


Similar Free PDFs