Lab 9 Solutions

Transcription

Lab 9 Solutions
CS 235 Spring 2015
Lab 9 Key
Rhys
April 3, 2015
Introduction
1
Exercise 1
Prove it’s decidable if two DFAs accept the same language.
def sameLang(M1, M2):
’’’ M1 and M2 are encodings of DFAs
return True if L(M1)==L(M2)
False otherwise’’’
# From our closure properties for regular languages
# we have effective procedures to build DFAs for intersections,
# unions and complements of regular languages
Build a DFA M recognizing
(L(M1 ) ∩ L(M2 )) ∪ (L(M2 ) ∩ L(M1 ))
return isEmpty(L(M))
2
Exercise 2
Express each of the following decision problems as a language that you can
demonstrate is decidable. Then do so.
1. Prove it is decidable if the language of a regular expression is infinite.
2. Prove it is decidable if a DFA accepts any strings with an odd number
of 1s.
3. Prove it is decidable if the language of a reg.exp. R is a subset of the
language of another reg.exp S
1. It is decidable if the language of a regular expression is infinite.
Define the language IN FREG = {r | r is a regular expression and L(r)
is infinite }.
1
Here is a TM that decides IN FREG :
First, make a minimized DFA M that recognizes L(r).
If the graph G of M contains a cycle, then L(r) is infinite, otherwise
it is finite. And we have an algorithm to decide if a graph has a cycle.
return(hasCycle(G));
2. It is decidable if a DFA accepts any strings with an odd number of 1s.
Let ODD1sREG = {< M >| M accepts a string with an odd number
of 1s }. The following TM decides ODD1sREG :
Build a DFA N to recognize the regular language L(M ) ∩ (0 + 1)((0 +
1)(0 + 1))∗
return(N ∈
/ EDF A ); // Sipser gives a decider for EDF A
3. It is decidable if the language of a reg.exp. R is a subset of the language
of another reg.exp. S
Let SU BSETREG = {(R, S) | L(R) ⊂ L(S)}
Here is a TM that decides SU BSETREG :
Build a DFA MR for L(R) and a DFA Mintersect for L(R) ∩ L(S);
return < MR ,Mintersect > ∈ EQT M
3
Exercise 3
Prove that it is decidable if a DFA accepts at least 42 strings.
def acceptsAtLeast42(M):
’’’M is the encoding of a DFA
return True if |L(M)| >= 42
False otherwise’’’
Let p denote the number of states in M
if isInfinite(M) return True
# otherwise we know that M does not accept any strings of length >= p
counter = 0
for w in the (finite) set of strings of length < p:
if M accepts w:
counter += 1
if counter >= 42:
return True
return False
4
Exercise 4
Flesh out the proof of Theorem 4.8 – that emptiness is decidable for CFGs.
2
def isEmpty(cfg):
’’’ Return True if L(cfg) is empty, False otherwise ’’’
Let S be the start symbol of cfg.
Apply our (guaranteed halting) algorithm to decide if S is usable
return not isUsable(S)
5
Exercise 5
1. Prove it is decidable if the language of a CFG is infinite.
2. Prove it is decidable if a DFA accepts any strings with an odd number
of 1s.
3. Prove it is decidable if the language of a reg.exp. R is a subset of the
language of another reg.exp S
1. It is decidable if the language of a regular expression is infinite.
(done in the lab description)
2. It is decidable if a PDA accepts any strings with an odd number of 1s.
Let ODD1sP DA = {< M >| M accepts a string with an odd number
of 1s }. The following TM decides ODD1sP DA :
Build a PDA N to recognize the regular language L(M ) ∩ (0 + 1)((0 +
1)(0 + 1))∗
Convert N to a context free grammar G return not isEmpty(G) where
isEmpty is from Exercise 4.
3. It is decidable if the language of a PDA M is finite.
Here is a TM that decides F IN IT EP DA :
Convert M into a CFG G.
return not isInfinite(G)
where isInfinite is from part 1.
6
Exercise 6
Prove that it is decidable if a PDA accepts at least 42 strings.
This is very similar to Exercise 3.
def acceptsAtLeast42(M):
’’’M is the encoding of a PDA
return True if |L(M)| >= 42
False otherwise’’’
Convert M into an equivalent CNF grammar G
Let p denote one more than the number of variables in G
3
if isInfinite(G) return True
# otherwise we know that G does not generate any strings of length >= p
counter = 0
for d in the (finite) set of derivations of length < p:
if d yields w:
counter += 1
if counter >= 42:
return True
return False
7
Exercise 7
Show that it is decidable if a CFG generates any strings that consist of all
1s. This exercise is answered in Sipser. Please submit a solution in your
own words after you have read Sipser’s solution.
Sipser’s solution is ok.
8
Exercise 8
Prove that it is decidable if there are any palindromes accepted by a given
DFA. Hint: You may want to look at Sipser’s solution for 4.25.
The language of palindromes is CF, so let M be a PDA for it.
(define PAL-DFA-decider
(lambda (d)
;; d is a DFA
build the product PDA P to simulate M and d in lockstep
note that L(P) = the intersection of L(M) and L(d)
We know the emptiness problems for CFGs is decidable
return not(isEmpty(P))
))
9
Exercise 9
deliverable Give me an example of an ambiguous grammar. To answer
this question, you need to define the grammar, and then draw two distinct
derivation trees for the same string of terminals. Hand-drawn derivation
trees are acceptable. You may hand them in on paper with your hardcopy
submissions.
S →S+S |S∗S |a
is ambiguous since there are two derivations for a+a*a:
S
/
S
|
S
\
+
/
S
S
/
\
/
\
*
\
4
S
|
a
S
|
a
*
S
|
a
S
|
a
+
5
S
|
a
a