CS138: Automata and Formal Languages Lecturer: Stefano Tessaro UC Santa Barbara Fall 2014 Solution 4 Task 1 – Right-Regular Grammars (6 points) Consider the following right-regular grammar G = ({A, B, C}, {a, b}, A, P ), where P consists of the following productions: A → abaB | babB B → aB | bC C → bC | aB | λ . a) Which ones of the following derivations are possible? Justify your answer! ∗ - A ⇒ abaaab ∗ ∗ - A ⇒ babaaa - B ⇒ babab ∗ - B ⇒ bababa ¨¨¨¨¨¨ - We have A ⇒ abaB ⇒ abaaB ⇒ abaaaB ⇒ abaaabC ⇒ abaaab. - It is not hard to see that the only production eliminating a variable is of the form C → λ, and C always appears after a b. Therefore, every string in L(G) must end with a b, and thus babaaa cannot be derived from A. - Here, B ⇒ bC ⇒ baB ⇒ babC ⇒ babaB ⇒ bababC ⇒ babab. - For similar reason as above, every string derived from B must end with b. ¨¨¨¨¨¨ b) Describe the language L(G) using a regular expression. ¨¨¨¨¨¨ It is not hard to see that the language L(G) is generated by the regular expression r = (aba + bab)(a∗ bb∗ )(aa∗ bb∗ )∗ . This is even more obvious when looking at the DFA given in c) below. ¨¨¨¨¨¨ c) Give a DFA M accepting L(G), i.e., such that L(M ) = L(G). 1 ¨¨¨¨¨¨ To obtain the following DFA, we first apply the procedure mapping rightregular languages to NFAs, noticing however that in the resulting NFA, every state has at most one outgoing edge per label, and no λ-transitions. Thus, to obtain a DFA, we simply complete this with the missing transitions to a trap state q5 , without the need to apply the more complex NFA to DFA transformation. ¨¨¨¨¨¨ Task 2 – Properties of Grammars (5 points) a) Prove that for every right-regular grammar G, there exists a right-regular grammar G0 such that L(G) = L(G0 ), and all productions have form A → xB or A → λ for A, B ∈ V , x ∈ T. ¨¨¨¨¨¨ First off, we have seen in class that for every right-regular grammar G there exists an NFA M such that L(M ) = L(G). Moreover, we also have seen explicitly that given an NFA M , there exists a corresponding right-regular grammar G0 such that L(G0 ) = L(M ) and moreover, all productions have the form A → xB or A → λ. The combination of these two facts yields the desired statement. Note: Some students have given direct proofs which are also valid (one can for example ignore the middle stage as an NFA, and give a solution which accounts to the combination of the two stages directly, essentially factoring out every production A → x1 x2 ...xr B as productions A → x1 A0 , A0 → x2 A00 , . . . and so on.) ¨¨¨¨¨¨ b) Prove that unless L(G) = ∅, every right-regular grammar G = (V, T, S, P ) contains at least a production of the form A → x where A ∈ V and x ∈ T ∗ . 2 ¨¨¨¨¨¨ Towards a contradiction, assume that G is such that all productions have form A → xB, i.e., there is a variable on the right hand side. Then, we can prove by induction that every string w ∈ (T ∪ V )∗ derived from S, i.e., such ∗ that S ⇒ w, contains one variable in V , and concretely, it has form w = yA for y ∈ T ∗ and A ∈ V . This is true for zero step derivations from S, since this would be S = λS itself, which is a variable. Now assume that we derived a string wi in i steps, and by the induction hypothesis, wi = yA. Then, we can only apply productions of the form A → xB, and thus we would obtain wi+1 = yxB, which has again the desired form. n o ∗ Therefore, for such a G, the set L(G) = w ∈ T ∗ : S ⇒ w would be empty. This implies that for L(G) to be non-empty, at least a production of the form A → x for x ∈ T ∗ and A ∈ V must be present in G. ¨¨¨¨¨¨ Task 3 – Right- and Left-Regular Grammars (6 points) For each of the following languages L with alphabet Σ = {a, b}, give right-regular and left-regular grammars G and G0 such that L(G) = L(G0 ) = L. a) L1 = L(aa∗ (ab + a)) ¨¨¨¨¨¨ The language L1 is generated by the following right-regular grammar: S → aS | aA A → ab | a . A corresponding left-regular grammar is obtained by going through the string backwards: S → Aab | Aa A → Aa | a . ¨¨¨¨¨¨ b) L2 = L(aaa∗ bbbb∗ ) ¨¨¨¨¨¨ The language L2 is generated by the following right-regular grammar: S → aaA A → aA | bbbB B → bB | λ . 3 A corresponding left-regular grammar is obtained by going through the string backwards: S → Sb | Abbb A → Aa | aa . ¨¨¨¨¨¨ c) L3 = L((aab∗ ab)∗ ) ¨¨¨¨¨¨ The language L3 is generated by the following right-regular grammar: S → aaB | λ B → bB | abS . A corresponding left-regular grammar is obtained by going through the string backwards: S → Bab | λ B → Bb | Saa . ¨¨¨¨¨¨ d) L4 = {w ∈ Σ∗ : w contains at most 3 a’s}. ¨¨¨¨¨¨ The language L4 is generated by the following right-regular grammar: S S0 S 00 S 000 → bS | aS 0 | λ → bS 0 | aS 00 | λ → bS 00 | aS 000 | λ → bS 000 | λ . Note that this language is symmetric, i.e., LR 4 = L4 , so the left-regular grammar is very similar: S S0 S 00 S 000 → Sb | S 0 a | λ → S 0 b | S 00 a | λ → S 00 b | S 000 a | λ → S 000 b | λ . ¨¨¨¨¨¨ Remark. As a general procedure to obtain a left-regular grammar for a language L, one can simply obtain a right-regular grammar for LR , and then just “turn around” all productions. Task 4 – Pumping Lemma (7 points) Use the Pumping Lemma to show that the following languages with alphabet Σ = {a, b} are not regular. 4 a) L1 = {ww : w ∈ Σ∗ }. ¨¨¨¨¨¨ For every m, consider the string wm = am bm am bm ∈ L1 . Then, let w = xyz be an arbitrary splitting such that |xy| ≤ m and |y| ≥ 1. Then, x and y must both consists of a’s only, and for example, w2 = xy 2 z has form w2 = an bm am bm where m + 1 ≤ n ≤ 2m, and thus w2 ∈ / L1 . This implies that L1 is not regular by the Pumping Lemma. ¨¨¨¨¨¨ b) L2 = an b` : n ≤ ` . ¨¨¨¨¨¨ For a given m, we consider the string w = am bm ∈ L2 . Then, given an arbitrary split w = xyz where |xy| ≤ m and |y| ≥ 1, we know that xy must consist of all a’s, and y consists of at least one a, and thus w2 = xy 2 z must have form an bm where n > m. Therefore, w2 ∈ / L2 , and we have proved that L2 is not regular by the Pumping Lemma. ¨¨¨¨¨¨ c) L3 = {an b2n : n ≥ 0}. ¨¨¨¨¨¨ m 2m For any given m, consider w = a b ∈ L3 . Then, let w = xyz be a splitting such that |xy| ≤ m, and |y| ≥ 1. Then, in particular, it must be that w2 = xy 2 z is such that w2 = an b2m where n > m, and thus w2 ∈ / L3 . By the Pumping Lemma, this implies that L3 is not regular. ¨¨¨¨¨¨ Hint: You need to show that for every possible m, there exists w ∈ L with length |w| ≥ m such that for every splitting w = xyz where |xy| ≤ m and |y| ≥ 1, there exists i ≥ 2 with wi = xy i z ∈ / L. In particular, you need to clearly describe how you construct w given m, and why wi ∈ / L for some i no matter how x, y, and z are chosen. Task 5 – Closure Properties (6 points) The symmetric difference L1 ⊕ L2 of two languages L1 and L2 is defined as L1 ⊕ L2 = (L1 ∪ L2 ) − (L1 ∩ L2 ) . a) Let L1 = L(r1 ) and L2 = L(r2 ) be two languages with alphabet Σ = {0, 1} defined by the regular expressions r1 = (00 + 11)(01)∗ , r2 = 00(0 + 1)∗ . Describe the language L1 ⊕ L2 using a regular expression. 5 ¨¨¨¨¨¨ Note that L1 ⊕ L2 is the set of strings which are in either of L1 or L2 , but not both. It can also be written as L1 ⊕ L2 = (L1 − L2 ) ∪ (L2 − L1 ). Note that L(r1 ) = L(00(01)∗ ) ∪ L(11(01)∗ ), and that L(00(01)∗ ) ⊆ L2 , but L(11(01)∗ ) ∩ L2 = ∅, because all strings in L2 start with 00. Thus, L1 − L2 = L(r1 ) where r1 = 11(01)∗ . Moreover, all strings in L1 have even lengths, and all odd-length strings in L2 are thus in L2 − L1 , i.e., those generated by the regular expression r2 = 00(0 + 1)(00 + 01 + 10 + 11)∗ . Finally, all even-length strings defined by the the following regular expression are the remaining elements of L2 − L1 , r3 = 00(00 + 01 + 10 + 11)∗ (00 + 11 + 10)(00 + 01 + 10 + 11)∗ . Thus, L1 ⊕ L2 = L(r1 + r2 + r3 ), i.e., it is described by the regular expression r = r1 + r2 + r3 . ¨¨¨¨¨¨ b) Prove or disprove the following: If L1 and L2 are regular, then the symmetric difference L1 ⊕ L2 is also regular. ¨¨¨¨¨¨ This is true. We know that if L1 and L2 are regular, then both L1 ∪ L2 and L1 ∩ L2 are also regular. Moreover, we know that if L and L0 are both regular, then L − L0 is also regular. Thus, with L = L1 ∪ L2 and L0 = L1 ∩ L2 , this implies that L1 ⊕ L2 is regular, too. ¨¨¨¨¨¨ c) Prove or disprove the following: If L is regular, then any L0 such that L0 ⊆ L is also regular. ¨¨¨¨¨¨ This is false. For example, for Σ = {a, b}, the language L = Σ∗ consisting of all strings made out of a’s and b’s is regular, as a single-state DFA with initial and final state q0 and a loop on q0 with labels a, b accepts all these strings. However, we have seen above examples of language L0 ⊆ Σ∗ which are not regular. ¨¨¨¨¨¨ d) Prove or disprove the following: If L1 is not regular, and L2 is not regular, then L1 ∪ L2 is not regular. ¨¨¨¨¨¨ This is false: Take any non-regular language L1 ⊆ Σ∗ with some alphabet Σ. (It does not matter which one, pick your preferred one.) Then, let L2 = Σ∗ − L1 , i.e., L2 is the complement L1 of L1 . If L1 is not regular, then L2 is 6 also not regular, as if L2 were regular, then L1 would be regular, too. (Recall that the complement of a regular language is regular.) But by definition, L1 ∪ L2 = Σ∗ , which, as we argued above in c), is a regular language. ¨¨¨¨¨¨ For b), c), and d), either give a proof (if the statement is true), or a counterexample (if the statement is false). 7
© Copyright 2025 Paperzz