Unformatted text preview: CSE 105 Homework 2 Solutions Solution: In order to prove is a contextfree language, we will provide a contextfree grammar that generates . To do so, we can think of any string in as containing 5 parts , where is the reverse of . can be any string in ending with or just an empty string, . Similarly, can be any string in starting with or just . can be any string starting and ending with or or or , where the latter three cases deal with the case where . Based on this idea, the following grammar will generate all strings in . Problem 2. Let and . Show that is a contextfree language (CFL). Solution: In order to show that is a CFL, it’s sufﬁcient to provide a CFG that generates it. To do so, we can use the fact that the set of CFLs is closed under union and separate our solution into two cases: when and when . In order to deal with the ﬁrst case where , we can ﬁrst create a variable that generates an arbitrary string of equal length sides (where a variable generates a single arbitrary symbol) and, then, create another variable that generates an arbitrary string of size at least 1 and contatenate this string to either the beginning or the end of the string generated by . In order to deal with the second case where , at least one symbol on the left must mismatch the corresponding symbol on the right. However, if we try to only generate strings of equal length sides, we won’t be able to do it because the language and and itself is not a CFL (it’s a good exercise to prove that!). Therefore, we should make use of a little trick and also allow the generation of strings of unequal length sides. Because all such strings also belong to the language, the ﬁnal result will still be correct. Then, we ﬁrst do the case where a 0 on the left mismatches a 1 on the right. We can do so by using two variables and . Variable generates an equal number of symbols to either side of itself and then becomes a 0, followed by an arbitrary string, followed by a . Variable generates a 1 followed by an arbitrary string. Because the distance between the symbol 1 and the is the same of that between the 0 and the beginning of the string, we are the cannot be equal to . A production would then allow derivations of the form any any 1 3 1 © i Q F C © 8 Problem 1. Show that is contextfree. , each , and for some and , 5 @[email protected]¥ 9 CD ¥ ¡ ' V Q F H F 7C PIGE ¥
E
H X W % '
© X W s '
6 H ' eE '
6 E Y 45¨i y¥ ¡ 65 1£ ( ' 724320)¥ H X ¥ ¡' W C' [email protected]¥ 9 ¡ 8 5 i u¡ Bv¥ V e2U2c©"f© b © 3 1 V aa Y b e©da3 Y 3"U a1 Y 1 Vb "3 b 1 V cb © b© Q )Y E a © i u¡ B¥ ¡ ¨i A¥ W X hi ¤tw¥ E
% # ! ¥ © © ¥ ©§ ¥£ ¡ &$" ¨¦¤¢ u¡ hi ¥ H V © 65 ©R3R 1 7UT"[email protected]£ ` Y 5%Rs£ ( R ¥ i © ¥ 6 d¦t2rqiA¦Uh¦£ ¡ ¨i yw¥ 6 d¦t20rqipUh2Ig 5%Rs£ ( R ¥ i © ¥£ ¡ ` aa Y ` aY ` b ` Q ` E X hi w¥ W b 65 ©R3R 1 7UT"S2£ E F H eE ` Y H u¡ hi yxw¥ © V The case where a 1 on the left mismatches a 0 on the right can be generated in a similar fashion. Then, the entire grammar is: Problem 3. Construct a PDA for the language of all nonpalindromes over Solution: We can use the PDA for the language of all palindromes over , given in the sample solutions, to create a PDA for this language. To change the PDA accepting all palindromes into one that accepts all nonpalidromes, we simply insist in the new machine that there is at least one inconsistency between the ﬁrst and second half of input string . So the new PDA can essentially be the same, except when we are popping symbols off the stack and matching them with inputs, we must make sure that there is at least one a where there should have been a b or vice versa. Figure 1 shows the PDA. q4 q0 ε, ε q1 ε, ε 0, ε 1, ε 0, ε 1, ε 0 1 ε ε ε $ ε, $ ε q3 1, 0 0, 1 q2 ε ε 0, 0 1, 1 1, 0 0, 1 ε ε ε ε Figure 1: PDA for the language of all nonpalindromes over We ﬁrst mark the bottom of the stack with a $, push the ﬁrst half of the string (excepting the middle symbol if the string has odd length) onto the stack in , guess nondeterministically where the middle of the string is and switch to state . If , then at some point there will be a mismatch between what is on the stack and in the input; when this is true, the machine can take the transition from to . Otherwise, any match or mismatch of inputs symbols to symbols on the stack is allowed. Finally, the machine accepts when 1) the input is exhausted and 2) the stack is empty. 2 o o 53R 1 [email protected]£ 53R 1 2S2£ 53R 1 ¦S2£ §o n u ¡ qn o p n f gs ©d%
k
f l mk f V if %
©hg s eeE
f
E H d%s
eb
© b b xY Y § Y ` l ` k ` jxY ` H ` f ` E ` Y `
` b ` ` §Y ` Y case 1: unequal length sides case 2: 0 on left mismatches 1 on right case 3: 1 on left mismatches 0 on right . 0, 0 1, 1 1, 0 0, 1 ε ε ε ε Problem 4. Use the pumping lemma to show that the following language is not context free: Solution: Assume that is CFL. Let be the pumping length given by the pumping lemma and let . Obviously, . Because the length of is grater than we can split into satisfying the following conditions: 1. for each 2. 3. , For convenience, we will write as , where , respectively. cannot contain , since First of all, note that (from the deﬁnition of ). the fact that Then there are the following three cases for : of contains more symbols from contains more symbols from than from Because we have found a word which is in must be false. 3 D 2 h© D 2 3 ¥ 1 1 ¡ i 9 t x 9 % 1 3 D ¡ n D ¡ i a ¤ a a h a© h @¥ @ ¡ i i a ¤ a a h a© h @¥ @ ¡ i i i t #
$! © @¥ @ i © i © © h ¨@¥ i s # thi ( i s P ' ¥ ' @
# 8 [email protected]¥ @ i dhg"¡ 31 © 31 ( 5 D
¡ ' q¥R 9 u ¡ 8 qg}gzg"¦S2( ' $Sg¦quB$! ts©rr$© © § ¦I¢ ¥ u ~  {vRy53R 1£ ¥ xwv R t # ¥ ¥ ¥£ ¡
and would contain no . In this case, , where , where than from . In this case, and which cannot be “pumped” the assumption that 3 stand for the strings to the left and to the right which would contradict . . contains equal number of symbols from both and . In this case, because of conditions 2 and for some such that . Therefore and 3 of the pumping lemma, which is not in the language. is CFL ...
View
Full Document
 Summer '99
 Paturi
 Formal language, Formal languages, Contextfree grammar, CFL, arbitrary string

Click to edit the document details