a X a | b

advertisement
Answers to some of the review problems for the final exam
Fall 14
1. Construct a context-free grammar for the language L = {uawb | u, w {a, b}*, |u| = |w|}.
Then put your grammar into CNF. Finally, construct a PDA that recognizes L.
Grammar: S  Ab
CNF: Bb  b
S  ABb
A  XAX | a
A  XD1 | a
Xa|b
D1  AX X  a | b
2. Find a regular expression and a regular grammar for the following language over  = {0, 1}.
L = {w | w starts with 0 and has odd length or starts with 1 and has even length}
Grammar: S  0A | 1B
A  0C | 1C | 
B  0D | 1D | 1 | 0
C  0A | 1A
D  0B | 1B
Regular Expression: 0[(0 + 1)(0 + 1)]* + 1[(0 + 1)(0 + 1)]*(0 + 1)
3. Try the following problems from the text:
a. p. 170 # 15 See answer in text
p. 220 # 17 Show that L = {anbn | n is not a multiple of 5 is context –free.
Construct a grammar.
A grammar to generate this language is:
S  aaaaaSbbbbb | ab | aabb | aaabbb | aaaabbbb
4. Prove that if G is a CFG in Chomsky normal form then for any string w  L(G) of
length n  1, exactly 2n – 1 steps are required for any derivation of w.
Let G be a grammar in CNF and let w  L(G) and suppose |w| = m. Consider the
derivation tree for w. It’s a binary tree in which every nonleaf node has two
children. Since G has no  productions, all leaves are terminals in the string w.
Since each terminal is derived by using a production of the form A  a, n steps are
required to get the terminal symbols. Now, notice that there are exactly n nodes
that are parents of leaves. Then, the binary tree properties tell us that there are n-1
nodes that are not parents of leaves. Since each node represents using a grammar
rule, there are n – 1 steps needed to derive the parents of the leaves giving us a
total of 2n – 1 steps.
Another way to look at this is as follows: Suppose that no A  a rule was used until
the sentential form contained n variables i.e. only rules of the type A  BC are used
until a string of the right length is obtained. Then also notice that each time an A 
BC rule is used the total number of variables increases by 1 (one variable has been
replaced by two variables). Then, it requires n – 1 steps to produce a string of n
variables. That added to the n applications of rules of the form A  a gives us 2n -1
steps.
5. Use closure under homomorphism and/or closure under inverse homomorphisms to prove
that the following language is not context-free. L = {aib2ic3i | i  0}
Define h(0) = a, h(1) = bb h(2) = ccc. Then, h-1(L) = {0n1n2n} which we know is not a
CFL
6. L1, L2 and L3 are languages satisfying L1  L2  L3. Find infinite languages L1, L2, and L3
such that L1 and L3 are not regular and L2 is regular.
Here’s one answer: L1 = {ap | p is prime}, L2 = a*, L3 = {akbncn | n, k  0}
Obviously, lots of other answers are possible.
7. Construct a context-free grammar to generate the set of strings over {b, c} that begin and
end with b, and have an even number of c's between any two b's. Is this language
regular? If so, construct a finite automaton to recognize it.
S  bAb | 
A  bA | ccA | 
allowed
Guarantees string will begin and end in b
Remember that 0 is an even number so consecutive b’s
8. Construct a Turing machine that recognizes all string over {a, b, c} in which the first c is
immediately preceded by the substring aaa.
state
a
b
c
explanation
q0
q0, a, R
q0, b, R
q1, c, L
look for first c
q1
q2, a, L
look for first a
q2
q3, a, L
look for second a
q3
q4, a, L
look for third a
q4
accept state
If the word immediately was not there the most straightforward strategy (in my
opinion) is to first look for three consecutive a’s. Then, search to the right until a c
is found in which case move to the accept state.
9. Construct a finite automaton for L = {w  {a, b}* | if w contains substring aa then |w| is odd}
10. Find language generated by this grammar. To what class of languages does it belong?
S  AB
A  aAa | bAb | a | b
B  aB | bB | 

Any string of a’s and b’s. It’s regular. To see this notice that S  AB where A can
be replaced by either a or b. After that using aB or bB to replace B lets you
generate any string of a’s and b’s.
11. Prove that L is a CFL and its complement is not where L = {aibjck | i  j or i  k}
Correct this one anbncn is not in the complement
Here’s a grammar that generates L:
S  aAc | aBbD | 
A  aAc | aA | C
B  aBb | aB | 
C  bC | 
D  cD | 
Suppose the complement of L is a CFL (Use L’ for convenience). Since CFLs are
closed under intersection with regular sets, this implies L’  a*b*c* = {anbncn} is a
CFL and we already know it’s not.
12. Use the pumping lemma to prove that the language below is not regular.
L = {w = xcy | x, y  {a, b}* and |x| = |y| and na(x)  na(y)
Assume L is regular and let m be the integer from the pumping lemma. Choose w =
amcam. Clearly, w  L so it can be broken down as w = xyz such that |xy|  m, |y|  1
and for every i, xyiz L. Consider w0 = xz = am-kcam where |y| = k. Since m – k > m, w0
 L. Therefore, L cannot be context-free.
13. Construct a CFG for the complement of {anbn | n  0}.
The grammar for the complement can be broken into several parts:
1. Strings which start with b and those with only a’s
2. Strings in which there is an a following a b
3. Strings of the form anbm where n  m i.e. more a’s than b’s or vice versa.
Lets start with S  A | B | C | aD when A and D generate part 1 strings, B generates
part 2 strings, and C generates part 3 strings.
A  bD
D  aD | bD | 
B  DbaD
C  aCb | aE | Fb
E  aE | 
F  Fb | 
Download