Answer Key to CS304 Assessment 1 - Saylor.org

29 downloads 125 Views 75KB Size Report
Additional exercise and solution information is given here, as follows: Solution to Exercise #1 ... Solution to Exercise #2. We can derive the ... A regular grammar for L includes a grammar rule for a start symbol that generates an even number of  ...
Answer Key to CS304 Assessment 1

Additional exercise and solution information is given here, as follows:

Solution to Exercise #1 The language L is the set of all strings that consist of 0's and 1's, does not contain 00, and ends in 1. To describe this set using a general pattern, we reason as follows:

a. Write down some example strings, such as, (beginning with 0): 01, 011, 0111, 0101, 01111, 01011, 01101, 01111, etc.; (beginning with 1): 1, 11, 111, 101, 1111, 1101, 1011, etc. You should write down some more examples. Write down as many as you need to identify patterns. Thus, L is the set of all strings that has any number of 1's, with any number of 01's inserted anywhere. b. Looking at samples, we see that 1* is one pattern that appears. 01 can appear anywhere in 1* to generate some more strings in L. Thus, we can expand the pattern to 1* (011*)*. This pattern includes all the sample strings above. Alternatively, using the | symbol, one gets the simpler expression of the PDF quiz solution: (1 | 01)*.

Solution to Exercise #2 We can derive the regular expression by examining the strings that make up the set, L. L is the set of all strings that have an even number of 0's followed by an odd number of 1's. Note that 1 is in L, because it has zero 0's followed by a 1 (zero is even). An even number of zeros is expressed by (00) +. An odd number of ones is expressed by 1 (11)*. Thus, L is expressed by (00)+ 1 (11)* or equivalently, by (00)+ (11)* 1. Another way to derive the regular expression, is indicated by the steps:

regular language → regular grammar → regular expression, or --> fsa → regular expression A regular grammar for L includes a grammar rule for a start symbol that generates an even number of zeros followed by an even number of ones. Thus, we have S → Even Odd, where Even is a non-terminal representing an even number of zeros and Odd is a non-terminal representing an odd number of ones. Even → 00 Even | empty string generates strings of the form:

The Saylor Foundation 1

Even → 00 Even → 0000 Even → 000000 Even → etc.

00 ----------> 0000 ---------> 000000 --------> etc. , i.e. even number of 0's

Odd → 11 Odd | 1 generates strings of the form:

Odd → 1 → 11 Odd → 1111 Odd → 111111 Odd → etc.

1 → 111 -------> 11111 ------> 1111111 ------> etc. Thus, the grammar,

S → Even Odd Even → 00 Even | empty string Odd → 11 Odd | 1

generates the language L. The second grammar rule generates (00)* and the third grammar rule generates (11)* 1.

Solution to Exercise #6 Let L be the set of strings consisting of 2 or more 0's and 1's, such that the digit in the ith and i+2 position are the same. Assume that the strings in L are of length greater than 2, because otherwise the ith and i+2 positions would not be the same. We are to write a CFG (context free grammar) for L. Let a be either 0 or 1. Let b also be either 0 or 1. Then, for a string of a's and b's to have the ith and i+2 position be equal means that the string has the form ababab....ab. A grammar for generating such strings is: S → N ab N → b | N ab Now, a is either 0 or 1; b, also, is either 0 or 1. Rewriting this grammar replacing a and b by either 0 or 1 gives the grammar below.

The Saylor Foundation 2

If a and b are both 0, then we get a string of 3 or more 0's: S → N00, N → 0 | N0 If a and b are both 1's, then we get a string of 3 or more 1's: S → N11, N -->1 | N1 If a and b are different, i.e. 0 and 1, or 1 and 0, then we get: S → N01, N –> 1 | N01 and S –> N10, N → 0 | N10 Combining these 5 rules gives: S → N0 00 | N1 11 | N01 01 | N10 10 N0→ 0 | N0 0 N1-->1 | N1 1 N01–> 1 | N01 01 N10 → 0 | N10 10

The Saylor Foundation 3