You are on page 1of 7

CS 205 Quiz #9A Nov 29, 2012

NAME: ____________________________SOLUTIONS__________________________________

RUID:___________________________________________________________________________

Electronic Devices are not permitted during the quiz. These include but are not restricted to calculators, computers and cell phones. No communication between students or anyone outside of class is permitted during the quiz. Textbooks, notes and any other written materials are not permitted during the quiz.

DO NOT OPEN UNTIL INSTRUCTED TO DO SO

1. Let G be the phrase-structure grammar with vocabulary V = { A, B, a, b, S }, production set P = { S ABa, S Ba, A aB, B ab }, start symbol S, and terminal element set T = { a, b }. For each of the following, write True if it is derivable from S, otherwise write False. 1 point each ______False___________ a) a

S ABa & S Ba and all other productions are non-empty, so just a is impossible ______ False___________ b) b

Because S ABa & S Ba, all words will have a at the end ______ False___________ c) ab

Because S ABa & S Ba, all words will have a at the end ______ False___________ d) abb

Because S ABa & S Ba, all words will have a at the end _______True___________ e) S Ba aba ______ False___________ f) ba aba

B ab is the only production with b, so it must always be preceded by a ______ False___________ g) baab

Because S ABa & S Ba, all words will have a at the end _______True___________ h) aababa

S ABa Aaba aBaba aababa _______False__________ i) aaa

S ABa & S Ba and B always leads to b, so all as is impossible _______False__________ j) ababa

The derivation of (h) is close, but there is an extra a, which is impossible to remove

2. Answer the questions below.

3 points possible each

a) Find a set of exactly 2 productions that produces {12n | n > 0}. Use only nonterminal S, where S is the start symbol. Alternatively: S 11S S 11 S 11 S SS

b) Find a set of exactly 3 productions that produces {1n0 | n 0}. Use only nonterminals S and A, where S is the start symbol.

Alternatives: S A0 A 1A A S AS S 0 A 1 S 1S S A A 0 S A A 1A A 0

c) Find a set of exactly 4 productions that produces {0n1m0n | m 0 n 0}. Use nonterminals S and A, where S is the start symbol.

S 0S0 S A A 1A A

3. Let V = { A, B, a, b, S } and T = { a, b }. For each of the following production sets P, determine whether G = (V, T, S, P) is a A. Type 0 grammar, but not a type 1 grammar B. Type 1 (context-sensitive) grammar, but not a type 2 grammar C. Type 2 (context-free) grammar, but not a type 3 grammar D. Type 3 (regular) grammar 1 point each

______C_______ a) S aAB, A Bb, B

______D_______ b) S aA, A a, A b

______A_______ c) S ABa, AB a

______C_______ d) S ABA, A aB, B ab

______C_______ e) S bA, A B, B a

______A_______ f) S aA, aA B, B aA, A b

______D_______ g) S bA, A b, S

______A_______ h) S AB, B aAb, aBb b

______C_______ i) S aA, A bB, B b, B

______C_______ j) S A, A B, B

4. Consider grammar G = (V, T, S, P) where V = { A, B, a, b, S } and T = { a, b }. The set of productions P in Backus-Naur form are: S ::= AA | AB A ::= aB | ab B ::= b Determine whether each of the following is derivable starting from S. Write True if it is derivable and False if it is not derivable. Since B only produces b, there are only 2 possible strings in the language: abb and abab

_____False____________ a)

1 point each

_____False____________ b)

_____False____________ c)

aa

_____False____________ d)

ab

_____False____________ e)

aba

_____True_____________ f)

abb

_____False____________ g)

baa

_____False____________ h)

abaab

_____True_____________ i)

abab

_____False____________ j)

ababa

5. A palindrome is a string that reads the same backward as it does forward. In other words, a palindrome is string w, where w = wR, where wR is the reversal of the string w. Find a context-free grammar that generates the set of all palindromes over the alphabet {0, 1}. List the productions in the space provided below. Use only five productions and only one nonterminal, S. S 0S0 S 1S1 S 0 S 1 S 5 points possible

Scratch Paper