Sie sind auf Seite 1von 10

Context-Free Languages Sample Solutions

Designing CFLs
Problem 1: Give a context-free grammar that generates the following language over
   

contains more 1s than 0s

Solution: Idea: this is similar to the language where the number of 0s is equal to the number of 1s, except we must ensure that we generate at least one 1, and we must allow an arbitrary number of 1s to be generated anywhere in the derivation. The following grammar accomplishes this task:
  !        "#   $  %  #    & 

Proof of correctness: it should be clear that this grammar cannot generate any strings not in . The  production for guarrantees that any string contains at least one 1, and any time a 0 is generated, at least one additional 1 is generated with it. We must argue that the grammar generates all strings with more 1s   generate all strings containing a number of 1s greater than or equal to the than 0s. The productions for   )( 10 number of 0s (proven below). The production for asserts that any string ' in can be written ' &10 3 (5476 3 (@4 3 0 476 30 4  where 2 and 2 . This is true: if ' begins with a 1, we can say that ' . If 298 2A8 ' begins with a 0, we can use a counter which is incremented by 1 for each 0 encountered and decremented by 1 for each 1 encountered, and at some point in the string this counter must become -1 upon encountering ( part of ' prior to this point be and the part of ' after this a 1 since0 ' contains more 1s than 0s. Let the 1 0 B( point be ; clearly, this breakdown of ' satises the requirements stated above.  3 4C6   3 4 Now, to show that generates all strings ' such that 2 10 ' 2 8 ' , the same counter argument will 3 (@47 3 (@4 3 0 4D6 30 4  ( 298 2 2A8 work. If ' begins with a 0, it must be of the form ' where 2 and . 3 (@4F 3 (54  ( E0 If, on the other hand, ' begins with a 1, it must either be the case that ' where 2 and 2A8 3 0 4G6 30 4 3 (54G6 3 (54  ( 2 2A8 298 , or it is the case that ' where 2 . Both of these cases are handled by the   transitions.

Problem 2:

Give a context-free grammar generating the language




the complement of the language

HPIRQ%I5 S 6

RUT V HPWXQ`Y" aDb

c @dAH W Q Y  aFe

Idea: we can break this language into the union of several simpler languages: c 5d 3 HFdfQ 4 Q 3 HFdgQ 4 1H 3 HFdfQ 4 . That is, all strings of as followed by bs in which the number HPWhQ`Y . of as and bs differ, unioned with all strings not of the form First, we can achieve the union of the CFGs for the three languages:
ip   5q  5r

Solution:

Now, the set of strings

H W Q Y  asb

is generated by a simple CFG:


 s H   Qt H u  H H 5q Qt 5q QE Q

Similarly for Finally,

HPWvQ`Y" ase

5qw

3 HfdxQ 4 Q 3 HfdxQ 4 H 3 HDdxQ 4

is easily generated as follows:


 r   y y H5 Q Q y H y

Problem 3:

Give a CFG to generate


 H W Q Y  a c 6

and either

a  c

or

c 

UT

Is the grammar ambiguous? Why or why not? Solution: Idea: this language is simply the union of and HPWvQ`Y  a c 6 c  . We can create simple grammars for the separate languages and union them:
ip    q   H W Q Y  a c 6 a  c q 

For

f

, we simply ensure that the number of as equals the number of bs:


      & H Qt &

Similarly for ensuring that the number of bs equals the number of cs:
 q   H  q   & Q  &  

This grammar is ambiguous. For

(x

H I Q I I

, we may use either

or

5q

to generate .

Problem 4:

Show that

( @ 0  ( 0  (  

 0@ (

is a context free language.

Solution: This was done in class, but it may be useful to see the solution written formally. Idea: any string ' E0R must be even length, and its two halves must differ in at least one bit. This means f1  d    f@  0@    ed gd 10RfPE ' can be written ' ' or where and . But this is the same as saying  d    f5  0i   j hd Ef"051 hd 1f"0E or ' where and . Formulated this way, we can easily write a ' grammar for the language:
  8  !   y    8 y  $#       y  # 8 8 y  y

Problem 5: Give a simple description of the language generated by the following grammar in English, then use that description to give a CFG for the complement of that language.
 k  k 3 HldQ 4   H  QE Qkx kH Qk HRk & H I 3 HldQ 4 HQ I H I Q 3 HldQ 4 Q I

Solution: Clearly, generates . , then, generates strings like and . H WQ Y aFb c H WQ Y ame c Thus we can geta strings like where , and we can also get strings like where , but cannot HPWXQnY Q H  c get where . Furthermore, we can generate any string beginning with a or ending with an , H Q H WQ Y and every string beginning with and ending with that is not of the form . This, then, is exactly the H I Q I  S 6 R complement of the language . H I Q I  S 6 R A grammar for the complement of this language (which is, of course, just ) is simply
i H  Qt &T o lp ( qlr (

Problem 6. Write a context-free grammar for the language g ( s .

is a substring of

for
p

Solution: Strings in this language share the property that they start with a string w followed l by a , r p 3 jd followed by anything, followed by lp , f followed by anything. So we want strings of D the form 4 tr 3 Ddi 4 3 di 4vu tr 3 dw 4 . Let generate the part, and let generate the nal part. Thus we want derivations that proceed as follows:
ix x u   r x u Dp 3 yd 4  r 3 ydz 4 T

A grammar accomplishing this is:


    {U lp t|"}~&  pj

Since the recursion with nonterminal ends only when the transition , must generate a 3 vd 4 string whose beginning and end are mirror images. Since generates , the nonterminal generates Dp 3 d 4 1tr   & all strings of the form . Note that this also covers the case where . Since is  followed by in the transition for the top-level nonterminal , the grammar generates all strings of the lp 3 ld 4 tr 3 yd 4 form . 3

Chomsky Normal Form (CNF)


Problem 7: Solution:
     x & t$ &   8    x & t$ & 

Convert the following CFG into CNF, using the procedure given in Theorem 2.6.

1. First add a new start symbol 8 and the rule 8




2. Next eliminate the -rule

&

&

, resulting in new rules corresponding to


     8   8        & t

3. Now eliminate the redundant rule

and the -rule


&

&

&

    t

4. Now remove the unit rule

:
  8   & x    t t

5. Then remove the unit rule 8

:
8        t$ &     t t

6. Finally convert the 00 and

rules:
8      f    x     x 29812A8 2 8 2 8 2A81298 &

f

298

This grammar satises all the requirements for Chomsky Normal Form. 4

Closure Properties
Problem 8: Let be a context-free language and be a regular language. Prove that the language i is context-free. Then use the above to show that the language given below is not a CFL.
 VsHQ

and contains equal numbers of s, s and s

Solution: We have a CFL and a regular language and we want to show that is context-free.  Since is given to be a CFL we know that there exists a PDA, say , to recognize . Since is given q to be regular we have a DFA, say , to recognize . To prove that ez is a CFL we demonstrate a pushdown automaton, call it , that recognizes .  q The proof is by construction. We construct from and . The construction is similar to the proof of showing that the class of regular languages are closed under the union (or intersection) operation on pg. " of the text.    3X %t     4 Let recognize , where . q q  3XDq %l q q q 4 recognize , where . Let  3X %lFP 4 Construct to recognize , where . 1. 2. 3. 4.
  3t q 4  U A

and

Dq 

is wlog assumed the same in




and

.
Hz Qy

is dened as follows: for each

3t q 4

; each

and each

let

33t q 4 HQ 4m   3  q 4 3t q 4  U 

3 3U H$Q 4 q"3q H 44

5. 6.

and

q q q

Note that the above construction works only because one of the machines being simulated (the DFA above) does not need a stack. Observe that we may need to maintain stacks if we attempted to simulate PDAs instead, and that a PDA cannot do that. Now to show that the given language is not a CFL, we will make the assumption that it is and then derive a contradiction. Under this assumption we are guaranteed (from the part above) that if we intersected if we show that for some some regular language with , then the resulting language would be a CFL. So  regular language and some language which is not a CFL that, , then we have derived the contradiction. To see what this and might be consider all these languages, , H andQ as capturing some property. From the denition of we see that this property is equality of s, s and s. For  H I Q I I  S 6 R lets try the canonical example of the language that is notH a CFL, viz. . has the Q property of equality as well as order of (zero or more) s followed by (zero or more) s followed by (zero or more) s. Now it is easy to see what we want of ; that should have the property of order.  HR1Q1 (and we know that this is regular). This is Since we have a contradiction, it must be that is not a CFL.

Designing PDAs
Problem 9: Give an informal description and state diagram for the language
 j   7D  UT

that is,

is a palindrome


Solution: This is fairly simple: we can push the rst half of , nondeterministically guess where its middle is, and start popping the stack for the second half of  j , making sure the second half matches what we pop is odd or even, though. off the stack. We have to worry about the case where  The state diagram for is shown in gure 1.
q0 , q1 , 0, 1, 0, 1, 0 1 0, 0 1, 1 $ q3 , $ q2

Figure 1: State diagram for

 

From the start state 8 , we push a $ onto the stack to mark its bottom. In state , we push the rst half of    is odd. Then we nondeterministically guess where onto the stack, not including the middle symbol if q the middle occurs, at which point we can either move to state without consuming any input if the length    q is odd. In state , we pop each stack symbol from of is even, or simply ignore the middle symbol if the stack, ensuring that it matches the current input symbol. Finally, if all goes well, we will reach the end  of with an empty stack (top symbol = $) and accept. Otherwise the PDA will always crash.

Problem 10: Give an informal English description of a PDA for the language H I Q I  S 6 R . language



the complement of the

Solution: A PDA for this language can be motivated by the CFG for it, which was described in the sample solutions part I, and displayed in Problem 2.25. As a reminder, here is the CFG:
 k   H  QE Qkx kH Qk HRk & H I Q 3 HdQ 4 }Q I H I 3 HjdQ 4 HQ I

Recall that this CFG generates strings of the form pr . All we have to do to S  accept strings of this form is to push the rst as onto the stack in state , and nondeterministically switch q to a new state when that is done. At this point we have two branches: 1. If the next symbol is a b,Hg we ush that input, go to state , then continue ushing the part of the 3 dQ 4 . We nondeterministically guess when this is done and move to state string corresponding to S , which pops bs corresponding to the number of as that were pushed at the beginning of the string, nally switching to an accept state if the correct number of bs were matched. 2. If the next symbol was not a b, on the other hand, we allow the machine to switch from to , 3 HtdQ 4 nondeterministically ush the part of the string (in this case our input string must be of the H I 3 Hmd9Q 4 1HQ I } S form ) then consume the a on the way to state which as before pops bs and accepts if everything matches correctly. Problem 11: Give an informal description of a pushdown automaton that recognizes
 H W Q W1 a c 6 q r

and either

a  c

or

c 

UT

Solution: Our PDA is fairly simple. First it pushes a bottom-of-stack marker then nondeterministically W Y % Y proceeds to either state or .

FromH state WY , we accept strings in which the number of s is equal to the number of Q s by pushing Q each encountered. Then when the automaton sees the rst , it begins matching the s against the H s already on the stack, one-for-one, until the bottom-of-stack marker is reached. If the number of H Q s and Q s matches (note this could also be 0), the machine then reads and ignores all s following H crashes if the number of s is not the last , accepting on Q the end of input. This part of the machine H}Q1 . equal to the number of s or the input string is not of the form

From state YH , we accept strings in which the number of s is equal to the Q number of s by rst ignoring any s occuring at the beginning of the string, pushing the following s onto the stack, then Q popping a for each encountered at the end of the string. Again, this part of the machine must allow Q strings where the number of s and s is zero, and crash when the input string is not of the form H Q Q or when the number of popped s is not the same as the number of s at the end of the string.

Problem 12: Convert the CFG in Theorem 2.12. Solution: The CFG

given in Exercise 2.1 to an equivalent PDA using the procedure given

is:
   B  V  3 A4  H

Assuming, as in Theorem 2.12, that a shorthand notation allows us to write an entire string to the stack in one PDA step, this task simply reduces to forming transition rules that implement the productions in the grammar. Figure 2 shows the PDA.
qstart , E$ , E , E , T , T , F , F E+T T TxF F (E) a a, a +, + ), ) (, ( x, x

loop

, $ q
accept

Figure 2: PDA recognizing the language generated by

The transitions for the rules of the grammar allow us to nondeterministically replace grammar nonterminals on the stack with their corresponding right-hand-sides; the transitions for the terminals of the 4 3 H grammar ( ) allow matching of input  symbols to grammar terminals. There will be an accepting  path through the PDA on string if and only if can be generated by the grammar .

Non-Context-Free Languages
Problem 13: Show that Solution: f W 0 W
(   H I Q%A  S q

is not context-free.
g f ( 0  HUQn

Using thea pumping lemma, assume the contrary, and let . The lemma says ' f 0 6 ' for any . Clearly, if either or straddles the boundary between q q as and bs, pumping f 0  f ( 0  HYQn je c{s will generate strings not in . If and are composed entirely of as, then , '  which isfPnot in since the number of bs is no longer the square of the number of as. The same argument 0  Q f 0 holds if . The is the q as and is q one or more bs. q If this q q only q remaining case is when is one or more 3 4  f ( 0  HYQn 5 c xb e 3 4 case, then we have . But since ' q , where q ze 3 c 4 c and (becaue ). Since cannot be any perfect square, it certainly cannot be  c b for any . Since every case results in a contradiction, is not context-free. Problem 14: Show that
FA   H$ } 6

is not context-free.
F 

is context-free. Hence, the Solution: For the sake of contradiction, let us rst assume that   6  H ` , where is the pumping length. Since pumping lemma can be applied to it. Let mA   f ( 0 ' , satisfying the following and , we know that can be broken into 5 parts, conditions:

1. for each 2. 3.
 fP0@b  f ( 0i s

a 6

f"W ( 0PW '

mA

; and .
a  , then q q  f ( 0 '      f f q ( 0 q q ( 0  fP0@ i q '   q s q q T  3 4 q

If we choose

and

 

 f"0iRb  

' is between As we can see, the length of q q the squares of two consecutive integers and, thus, can FA FA  f ( 0  not be a square of an integer. Consequently, . So the assumption that ' is a CFL is false.

Problem 15: Decide whether answer.






HQ 

29

3 (54 e

2A

3 (54 e

E2A

3 (@4

is a CFL and prove your


 H  q  Q

is not context free. We can prove thisHwith lemma. Let . Clearly Solution:  the q pumping  f ( 0  HEYE c b Q  c xb  (   this string is in . If , then ' , which the q q is not in  q because  f ( 0  Q Y c b f ( 0  H Q 5 b ' number of bs is more than twice the number of as. If , then f ( 0  . This string cannot be in because there are at least twice as many bs as as. If, on the other hand, 3 fP0 4 b 3 f"0 4 contains both as and bs, then the situation is a little l more complicated. If 2 then we can 29 c 3 $( 4A 3 ( 4A $( l c eh ' , for which 2A ' 2A ' pump down to get and with . But C 3 c 4s (  c e c eVy c e , since whenever , so ' is not in . If 3 f (@4 3 f (@4 2A 2A , on the other hand, we can pump up to get a number of bs more than the number of as: 3 f ( 0 4w 3 4 c 3 f ( 0 4y f ( 0E 3 4 if we use the string and ' , 29 ' 2A ' 6 c 3 f ( 0 4F c 3 4 e 3 4 ' where . Then E29 9

Problem 16: Let

s

Dp (  s

is a substring of

. Show that

s

is not a context-free language. s

Solution: In order to show that is not a CFL, we will proceed by contradiction. Assume is a  H Q pH Q is a CFL. Let be the pumping length given by the pumping lemma and let . Because  Pb s f ( 0 member of and , the pumping lemma says that can be split into ' satisfying the following conditions: 1. for each 2. 3.
 fP0@b  f ( 0i s a 6

f W( 0 W '

GX

, and .
 p 

For convenience, we also write as , where and stand for the strings to the left and to the right of p , respectively. q q fP0 p p f ( 0 First of all, we can note that cannot contain , since and ' would contain more than one fP0 s . Then, we can think of three possible cases for the string : would not be in

contains more from than  from . q symbols q f ( 0 m p F b  F cannot be a substring of and the In this case, ' where . Therefore, s . entire string is not in

fP0

contains more symbols from f than from .p   tb  8 ( 0 8   ' where . Therefore, cannot be a In this case (pumping down), GX substring of and the entire string is not in .

fP0

contains an equal number of symbols from both and . f  QnY   HEY c In this case, because of conditions 2 and for some q andq 3 of the pumping lemma, cs5 f ( 0  HUQYpHYQn such that is not in the language. . Therefore '
f ( 0 ' yields a contradiction, the initial assump-

fP0

Because every possible way of splitting the input into s tion that is a CFL is false and the proof is complete.

10

Das könnte Ihnen auch gefallen