Solutions to Assignment 4 - Computer Sciencehwxi/academic/courses/eces-670/HANDOUTS/...... (a+ T))...

4

Click here to load reader

Transcript of Solutions to Assignment 4 - Computer Sciencehwxi/academic/courses/eces-670/HANDOUTS/...... (a+ T))...

Page 1: Solutions to Assignment 4 - Computer Sciencehwxi/academic/courses/eces-670/HANDOUTS/...... (a+ T)) )((a+ F)) )((a+ (E))) )((a+ (T))) )((a+ (F))) )((a+ (a))) Exercise 2 Answer each

Solutions to Assignment 4

October 25, 2000

Exercise 1 (20 pts) Let G = (V,Σ, R, S) be a context-free grammar such that V = {E, T, F},Σ = {a,+, ∗, (, )}, S = E and R is

E → E + T | TT → T × F | FF → (E) | a

Give parse trees and leftmost derivations for the following strings.

1. a

2. a*(a+a)

3. a+a+a

4. ((a+(a)))

Solution We present the leftmost derivations below.

1. S ⇒ E ⇒ T ⇒ F ⇒ a.

2. S ⇒ E ⇒ T ⇒ T ∗F ⇒ F ∗F ⇒ a∗F ⇒ a∗ (E)⇒ a∗ (E+T )⇒ a∗ (T +T )⇒ a∗ (F +T )⇒a ∗ (a+ T )⇒ a ∗ (a+ F )⇒ a ∗ (a+ a)

3. S ⇒ E+T ⇒ E+T +T ⇒ T +T +T ⇒ T +T +T ⇒ F +T +T ⇒ a+T +T ⇒ a+F +T ⇒a+ a+ T ⇒ a+ a+ F ⇒ a+ a+ T ⇒ a+ a+ a

4. S ⇒ T ⇒ F ⇒ (E) ⇒ (T ) ⇒ (F ) ⇒ ((E)) ⇒ ((E + T )) ⇒ ((T + T )) ⇒ ((F + T )) ⇒((a+ T ))⇒ ((a+ F ))⇒ ((a+ (E)))⇒ ((a+ (T )))⇒ ((a+ (F )))⇒ ((a+ (a)))

Exercise 2 Answer each part for the following context-free grammar.

R → XRX | SS → aTb | bTaT → XTX | X | εX → a | b

1. What are the variables and terminals of G? Which is the start symbol? (The set of variablesis {R, S,X, T} and the set of terminals is {a, b}. R is the start symbol.)

2. Give three examples of strings in L(G). (ab, ba, aab)

3. Give three examples of strings not in L(G). (a, b, aa)

1

Page 2: Solutions to Assignment 4 - Computer Sciencehwxi/academic/courses/eces-670/HANDOUTS/...... (a+ T)) )((a+ F)) )((a+ (E))) )((a+ (T))) )((a+ (F))) )((a+ (a))) Exercise 2 Answer each

4. True or False: T ⇒ aba. (False)

5. True or False: T ⇒∗ aba. (True)

6. True or False: T ⇒ T . (False)

7. True or False: T ⇒∗ T . (True)

8. True or False: XXX ⇒∗ aba. (True)

9. True or False: X ⇒∗ aba. (False)

10. True or False: T ⇒∗ XX. (True)

11. True or False: T ⇒∗ XXX. (True)

12. True or False: S ⇒∗ ε. (False)

13. Give a description of L(G) in English. Every word in L(G) is of form w1awbw2 or w1bwaw2

for some w,w1, w2 ∈ {a, b}∗ such that w1 and w2 have the same length.

Exercise 3 Give context-free grammars that generate the following languages.

1. {w | w contains at least three 1’s}

2. {w | w starts and ends with the same symbol}

3. {w | the length of w is odd}

4. {w | the length of w is odd and its middle is 0}

5. {w | w contains more 1’s than 0’s}

6. {w | w is a palindrome, i.e., w = wR}

7. The empty set

Solution

1. Here is a context-free grammar for L = {w | w contains at least three 1’s}:

S → T1T1T1TT → 0T | 1T | ε

2. Here is a context-free grammar for L = {w | w starts and ends with the same symbol}:

S → 0T0 | 1T1T → 0T | 1T | ε

3. Here is a context-free grammar for L = {w | the length of w is odd}:

S → 0T | 1TT → 0S | 1S | ε

Note that S and T generate words with even and odd lengths, respectively.

2

Page 3: Solutions to Assignment 4 - Computer Sciencehwxi/academic/courses/eces-670/HANDOUTS/...... (a+ T)) )((a+ F)) )((a+ (E))) )((a+ (T))) )((a+ (F))) )((a+ (a))) Exercise 2 Answer each

4. Here is a context-free grammar for L = {w | the length of w is odd and its middle is 0}:

S → 0S0 | 0S1 | 1S0 | 1S1 | 0

5. Here is a context-free grammar for L = {w | w contains more 1’s than 0’s}:

S → TS | 1T | 1ST → TT | 0T1 | 1T0 | ε

Note that T generates all words in which there are equal number of 1’s and 0’s. If a word wcontains more 1’s that 0’s, then w must be of one of the following forms.

• w = 1w1 such that w1 contains more 1’s than 0’s.

• w = 1w1 such that w1 contains equal number of 1’s and 0’s.

• w = w1w2 such that w1 contains equal number of 1’s and 0’s and w2 contains more 1’sthan 0’s.

6. Here is a context-free grammar for L = {w | w is a palindrome, i.e., w = wR}:

S → 0S0 | 1S1 | 0 | 1 | ε

7. Here is a context-free grammar for the empty set:

S → S

Exercise 4 Please convert the following CFG into an equivalent CFG in Chomsky normal form,using the procedure given in Theorem 2.6.

A → BAB | ABA | B | εB → 00 | ε

Solution We introduce a new start symbol S and obtain the following CFG.

S → AA → BAB | ABA | B | εB → 00 | ε

After ε-rule elimination, we generate the following CFG.

S → A | εA → BAB | ABA | B | BA | AB | AA | BBB → 00

After unit rule elimination, we generate the following CFG.

S → BAB | ABA | BA | AB | AA | BB | 00 | εA → BAB | ABA | BA | AB | AA | BB | 00B → 00

3

Page 4: Solutions to Assignment 4 - Computer Sciencehwxi/academic/courses/eces-670/HANDOUTS/...... (a+ T)) )((a+ F)) )((a+ (E))) )((a+ (T))) )((a+ (F))) )((a+ (a))) Exercise 2 Answer each

We now convert the above rules into the proper form.

S → BU | AV | BA | AB | AA | BB |WW | εA → BU | AV | BA | AB | AA | BB |WWB → WWU → ABV → BAW → 0

4