Closure Prop erties of CFL's | Substitut i on If a substitution assigns a CFL to ev ery sym b ol s in the alphab et of a CFL L , then s ( L ) is a CFL. Pro of � T ak e a grammar for L and a gramma r for eac h language L = s ( a ). a � Mak e sure all the v ariables of all these grammars are di�eren t. ✦ W e can alw a ys rename v ariables whatev er w e lik e, so this step is easy . � Replace eac h terminal in the pro ductions for a L b y S , the start sym b ol of the grammar for a L . a � A pro of that this construction w orks is in the reader. ✦ In tuition: this replacemen t allo ws an y string in to tak e the place of an y L a o ccurrence of in an y string of L . a Example n n � = f 0 1 j � 1 g , generated b y the L n grammar ! 0 S 1 j 01. S � f a n m j � n g , s (0) = b m generated b y the ! j ! j grammar S aS b A ; A aA ab . � f ab; abc g , s (1) = generated b y the grammar ! ! j S abA ; A c � . 1. Rename second and third S 's to S and 0 S , resp ectiv ely . Rename second A to B . 1 Resulting grammars are: ! 0 S 1 j 01 S S ! aS b j A ; A ! aA j ab 0 0 S ! abB ; B ! c j � 1 2. In the �rst grammar, replace 0 b y and 1 b y S 0 . The com bined grammar: S 1 ! j S S S S S S 0 1 0 1 ! j ! j S aS b A ; A aA ab 0 0 ! ; ! j S abB B c � 1 Consequences of Closure Under Substitut i on 1. Closed under union, concatenation, star. ✦ Pro ofs are the same as for regular languages, e.g. for concatenation of CFL's L , L , use L = f ab g , s ( a ) = L , and 1 2 1 s ( b ) = L . 2 1
2. Closure of CFL's under homomorphi sm . Nonclosure Under In tersection � The reader sho ws the follo wing language L = f 0 i j k l j g 1 2 3 i = k and j = l not to b e a CFL. ✦ In tuitiv ely , y ou need a v ariable and pro ductions lik e A ! 0 A 2 j 02 to generate the matc hing 0's and 2's, while y ou need another v ariable to generate matc hing 1's and 3's. But these v ariables w ould ha v e to generate strings that did not in terlea v e. i j k l � Ho w ev er, the simpler language f 0 1 2 3 j i = k g is a CFL. ✦ A grammar: ! j S S 3 A ! 0 A 2 j A B ! 1 B j B � � f 0 i j k l j g . Lik ewise the CFL 1 2 3 j = l � Their in tersection is L . Nonclosure of CFL's Under Complemen t � Pro of 1: Since CFL's are closed under union, if they w ere also closed under complemen t, they w ould b e closed under in tersection b y DeMorgan's la w. � Pro of 2: The complemen t of L ab o v e is a CFL. Here is a PD A recognizing it: P ✦ Guess whether to c hec k i 6 = k or j 6 = l . Sa y w e w an t to c hec k i 6 = k . ✦ As long as 0's come in, coun t them on the stac k. ✦ Ignore 1's. ✦ P op the stac k for eac h 2. ✦ As long as w e ha v e not just exp osed the b ottom-of-stac k mark er when the �rst 3 comes in, accept, and k eep accepting as long as 3's come in. ✦ But w e also ha v e to accept, and k eep accepting, as so on as w e see that the input is not in L (0 1 2 3 ). � � � � Closure of CFL's Under Rev ersal Just rev erse the b o dy of ev ery pro duction. 2
Closure of CFL's Under In v erse Homomorphism PD A-based construction. � Keep a \bu�er" in whic h w e place h ( a ) for some input sym b ol a . � Read inputs from the fron t of the bu�er ( � OK). � When the bu�er is empt y , it ma y b e reloaded with h ( b ) for the next input sym b ol b , or w e ma y con tin ue making � -mo v es. T esting Emptiness of a CFL As for regular languages, w e really tak e a represen tation of some language and ask whether ; . it represen ts � In this case, the represen tation can b e a CF G or PD A. ✦ Our c hoice, since there are algorithms to con v ert one to the other. � The test: Use a CF G; c hec k if the start sym b ol is useless? T esting Finiteness of a CFL � Let L b e a CFL. Then there is some pumping- lemma constan t n for L . � T est all strings of length b et w een and 2 n � 1 n for mem b ership (as in next section). � If there is an y suc h string, it can b e pump ed, and the language is in�nite. � If there is no suc h string, then � 1 is an n upp er limit on the length of strings, so the language is �nite. ✦ T ric k: If there w ere a string = z uv w xy of length 2 n or longer, y ou can �nd a shorter string in L , but it's at most uw y shorter. Th us, if there are an y strings n of length 2 n or more, y ou can rep eatedly cut out to get, ev en tually , a string v x whose length is in the range to 2 n � 1. n T esting Mem b ership of a String in a CFL Sim ulating a PD A for on string do esn't L w quite w ork, b ecause the PD A can gro w its stac k inde�nitely on � input, and w e nev er �nish, ev en if the PD A is deterministic. 3
3 � There is an ( n ) algorithm ( n = length O of w ) that uses a \dynamic programmi ng" tec hnique. ✦ Called Co c k e-Y ounger-Kasami (CYK) algorithm. � Start with a CNF grammar for L . � Build a t w o-dimensional table: ✦ Ro w = length of a substring of w . ✦ Column = b eginning p osition of the substring. ✦ En try in ro w and column = set of i j v ariables that generate the substring of b eginning at p osition and extending w j for p ositions. i ✦ In reader, these en tries are denoted , i.e., the subscripts are X j;i + j � 1 the �rst and last p ositions of the string represen ted, so the �rst ro w is X ; X ; : : : ; X , the second ro w is 11 22 nn X ; X ; : : : ; X , and so on. 12 23 n � 1 ;n Basis : (ro w 1) X = the set of v ariables A suc h ii that A ! a is a pro duction, and a is the sym b ol at p osition i of w . Induction : Assume the ro ws for substrings of length up to m � 1 ha v e b een computed, and compute the ro w for substrings of length m . � W e can deriv e a a � � � a from A if there is a i i +1 j pro duction A ! B C , B deriv es an y pre�x of � � � a a a , and C deriv es the rest. i i +1 j � Th us, w e m ust ask if there is an y v alue of k suc h that ✦ � i k < j . ✦ is in . B X ik ✦ is in . C X k +1 ;j Example In class, w e'll w ork the table for the grammar: ! j j S AS S B AB ! A a ! B b and the string aabb . 4
Recommend
More recommend