HW5 Solutions - Courses · PDF fileHW5 Solutions Micha l Derezinski February 24, 2015 1...
Click here to load reader
Transcript of HW5 Solutions - Courses · PDF fileHW5 Solutions Micha l Derezinski February 24, 2015 1...
HW5 Solutions
Micha l Derezinski
February 24, 2015
1 Exercise 4.3
(a) S → ASA | a A→ a | b
(b) S → ASA | aa | bb A→ a | b
(c) S → aCa | bDb C → ACA | a D → ADA | b A→ a | b
2 Exercise 4.10
(e) S → aSB | Λ B → b | bb | Λ
(f) S → aSB | a B → b | bb | Λ
3 Exercise 4.15
We use strong induction over the number n of rules in a derivation for a givenword.
Inductive Hypothesis H(n): Any word w, that has a derivation withn rules in our CFG, has more a’s than b’s.
Base case: n = 1. Only one rule allowed: S → a. So w = a and H(1)holds.
Inductive step: Suppose that H(k) holds for all 1 ≤ k < n. I will proveH(n). Take a word w with a derivation
S ⇒ w1 ⇒ ...⇒ wn = w.
1
Naturally, w1 has to be one of aS, bSS, SSb, or SbS, where any non-terminalS represents a subword of w with a derivation of length at most n − 1, sowe can apply the inductive hypothesis to it. For example, if w1 = aS, thenthere is a word v such that na(v) > nb(v), where w = av, which means that
na(w) = na(v) + 1 > nb(v) = nb(w).
Similarly, if w1 = bSS, then we have w = bv1v2 for some v1, v2, wherena(v1) > nb(v1) and na(v2) > nb(v2), so
na(w) = na(v1)+na(v2) ≥ (nb(v1)+1)+(nb(v2)+1) > nb(v1)+nb(v2)+1 = nb(w).
The other two cases follow similarly. Thus, we conclude that na(w) > nb(w),and so H(n) holds, concluding the induction.
4 Exercise 4.21
Definition 3.1 is a recursive definition of the class of regular languages overalphabet Σ, where every such language can be constructed from the basicbuilding blocks, that are:
1. the empty language ∅ = {},
2. and, for every a ∈ Σ, a language with a single one-letter word: {a}.
We allow three operations for the constructions: L1 ∪ L2, L1L2, and L∗1.We perform structural induction to prove that for regular L, the followingstatement P (L) is true: There is a CFG that generates L.
Base cases. Notice, that each of the basic languages can be generatedby a CFG: for empty language we use a grammar with no productions, andwe can get the language {a} from a grammar with one production: S → a.This concludes the base cases.
Inductive Step. We have to show that all three construction operationspreserve property P , when we apply them to regular languages. Theorem4.9 says that, in fact, P is closed under those operations (a stronger claim),so we obtain that if P (L1) and P (L2), then also P (L1 ∪ L2), P (L1L2) andP (L∗1). This concludes the structural induction.
2
5 Execrcise 4.22
We construct an NFA M, where the states are variables (non-terminals) fromthe grammar plus one additional state that is also the only accepting state:Z. We make variable S the start state. We create transitions as follows:
1. For every A→ aB, we add a transition Aa→ B.
2. For every A→ a, we add a transition Aa→ Z.
3. For every A→ Λ, we add a transition AΛ→ Z.
M accepts the same language as the grammar, because every path from startstate to accept state corresponds to a derivation in the grammar (and viceversa).
6 Exercise 4.29 (b)
The grammar corresponds to the regular expression (a + b)∗ab(ab + b)+. Toobtain a regular grammar, it is useful to construct an NFA for the language.
A DFA for this is
Sstart A B
C D
a
a, b
b
ab
b
ab
and therefore, the regular grammar is
S → aS | bS | aAA→ bB
B → aC | bDC → bD
D → aC | bD | Λ.
3
7 Exercise 4.34
To show that the grammar is ambiguous, I must simply exhibit a string withtwo different parse trees.
Consider the string x = ababa. The two parse trees are:
S
S
a
bS
S
a
bS
a
S
S
S
a
bS
a
bS
a
8 Exercise 4.37
Consider the string x = aababb. The two parse trees are:
S
B
B
B
B
S
Λ
b
B
B
S
Λ
b
a
B
S
Λ
b
a
a
S
B
B
S
Λ
b
B
S
B
S
Λ
b
a
b
a
a
9 Exercise 4.54(c)
For full credit, you should show your work, including at least the followingthree stages.
4
Grammar after removing Λ-productions:
S → AaA | CA | BaB | A | aA | Aa | a | CA→ aaBa | CDA | aa | DC | CA | C | DA | D | A | CD
B → bB | bAB | bb | aS | aC → Ca | bC | D | b | aD → bD | b.
Grammar after removing unit productions:
S → AaA | CA | BaB | aA | Aa | a | aaBa | CDA
| aa | DC | DA | CD | Ca | bC | b | bDA→ aaBa | CDA | aa | DC | CA | DA | CD | Ca | bC | b | a | bDB → bB | bAB | bb | aS | aC → Ca | bC | b | a | bDD → bD | b.
Final grammar after Chomskylization:
S → EA | CA | FB | XA | AX | a | HX | IA| XX | DC | DA | CD | CX | Y C | b | Y D
A→ HX | IA | XX | DC | CA | DA | CD | CX | Y C | b | a | Y D
B → Y B | JB | Y Y | XS | aC → CX | Y C | b | a | Y D
D → Y D | b
E → AX F → BX G→ XX H → GB
I → CD J → Y A X → a Y → b.
10 Exercise 4.16
Let us call the language generated by the grammar as LG. The proof consistsof two parts. First, we have to show that any word w ∈ L has a derivationin the grammar (so, w ∈ LG. Second, we will prove that any word w ∈ LG
(i.e. with a derivation) satisfies the property na(w) = nb(w) (i.e. w ∈ L).
5
1. For n ≥ 0, we prove inductive hypothesis H(n): Every word w ∈ L oflength n has a derivation in the grammar.
Base case. Let n = 0. So, w = Λ and the derivation is S ⇒ Λ.Inductive Step. Suppose that H(k) holds for any 0 ≤ k < n (strong
induction, n ≥ 1). Take w ∈ L, such that |w| = n. Let x denote the firstletter in w. We have two cases:
(a) If x = a, then clearly, if there is a derivation for w, it has to startwith the production S → aSbS. For this derivation to exist, the word has tobe of the form
w = avibv′i,
where vi, v′i ∈ LG and i represents the position of the separating letter b in
w (so vi = w[2..i− 1] and v′i = w[i + 1..n]). Notice, that nb(w) = na(w) ≥ 1,so w must contain a letter b. Now, we will show that there exists such letterb at some position i in w, that the subword vi (possibly empty) satisfiesna(vi) = nb(vi). For the sake of contradiction, suppose this is not true. Itimplies that the last letter in w also has to be a, because otherwise using vnwould lead to contradiction. Therefore, vn has to have more b’s than a’s tobalance out w. Denote
d(i) = nb(vi)− na(vi).
Like we said, d(n) > 0, and moreover d(2) = 0. Since, the value d(i) onlychanges by 1 between two consecutive input numbers, there has to be 2 ≤i ≤ n such that d(i) = 0 and d(i + 1) = 1. This means that there has to bea letter b at position i (since the function increases from i to i + 1), and sowe get a contradiction.
We showed that there exists a paritioning w = avibv′i, such that vi, v
′i ∈ L.
By inductive hypothesis, since |vi|, |v′i| < n, we find that vi, v′i ∈ LG, so both
derivations can be completed in our grammar, showing that w ∈ LG.(b) If x = b, then we repeat the above argument (with the roles of a and
b reversed), for the production
S → bSaB,
obtaining, again, that w ∈ LG, which ends the induction.2. We will prove the second part also by induction. For n ≥ 0, we
prove inductive hypothesis H(n): Every word w having a derivation with nproductions is in L.
6
Base case. Let n = 1. So, w = Λ, and obviously w ∈ L.Inductive Step. Suppose that H(k) holds for any 0 ≤ k < n (strong
induction, n ≥ 2). Take a word w with a derivation
S ⇒ w1 ⇒ ...⇒ wn = w.
Naturally, w1 has to be one of aSbS, bSaS. In either case, w consists of twosubwords vi, v
′i, which, having shorter derivations must be in L (by inductive
hypothesis). Since, both productions introduce the same number of a’s andb’s, the entire word w will remain balanced (concluding the induction):
na(w) = 1 + na(vi) + na(v′i) = 1 + nb(vi) + nb(v
′i) = nb(w).
7