http://wikistudent.ws/Unisa Proofs Example 1 Give an informal justification to show that the context-free languages are closed under the Kleene star. Solution 1 To show that context-free languages are closed under the Kleene star, you first need to find a CFG for the language. Then, replace all instances of the start state S with the letter X (which doesn’t change the language generated). A new start state S must be introduced, with the productions S XS | /\. We already know that all words in the language can be generated starting from the X-production. With the new S-production S XS, you can now have words of the form XXX...S, i.e. you can concatenate words from the language as many times as you like, which is analogous to a Kleene star operation. (The production S /\ is there just to eliminate S so that words can be produced). Example 2 Give an informal justification by means of grammars to show that the context-free languages are closed under product. Solution 2 To show that two languages L and M are closed under product, you first need to find CFGs for these languages. If you give all non-terminals in L the subscript ‘1’ and all non-terminals in M the subscript ‘2’, it ensures that both CFGs don’t have any non-terminals in common. Say L and M have the start states S 1 and S 2 respectively. Now, if you combine all productions from both languages into one CFG, and insert a new start-production S S 1 S 2 , this ensures that all words generated by the new CFG consist of a word from L followed by a word from M, which is precisely the product language. Example 3 Give an informal justification by means of grammars that the context- free languages are closed under union. Solution 3 To show that the CFLs L and M are closed under union, you first need to find CFGs for these languages. Then, for the language L, add the subscript ‘1’ to each of the non-terminals, and for the language M, add the subscript ‘2’ to each of the non-terminals. This doesn’t change the languages generated, since it was only non-terminals that were changed. Suppose the start state of each of the original languages was S. Then you should combine the productions of both L and M and add a new start state S (without a subscript). From this new S, add the productions: S S 1 | S 2 . The first production will (eventually) generate all words from L, while the second production will (eventually) generate all words from M. (Because the two languages were given different subscripts, no mixing of L and M productions is possible). In this way, all words from L or M can be generated, which is the same as the union language: L + M. http://wikistudent.ws/Unisa Example 4 Context-free languages are closed under Kleene closure. Consider the language L generated by the CFG below, write down a grammar that will generate L*, and prove that your grammar is correct. (Hint: The proof should be done in two directions). S aS | bX X aX | bS | aY Y b Solution 4 S ZS | /\ Z aZ | bX X ax | bZ | aY Y b The original CFG was changed as follows: All instances of S were replaced with Z, and new start-productions S ZS | /\ were added. Bidirectional proof that the CFG generates L*: Show that the CFG can generate all words in L*: The new productions S ZS | /\ makes the following change to the original CFG: All the original words can still be produced, but with this new start state, words of the form ZZZ...S can also be produced (where the Z can be replaced with any word from the original language since it was the old CFG’s start state). Now, if you follow the same sequence of productions for every Z (i.e. if you ensure that you generate the same word from each Z), you end up with a concatenation of multiple instances of the same word. Depending on how many times the production S ZS is used, you can generate as many repetitions as you like, eventually putting an end (or generating the null word) by employing the production S /\. Words of this form are precisely all those in L*. Show that all words in L* can be generated by the CFG: Words in L* can either be empty or have the form: X, XX, XXX, i.e. a word can be concatenated to itself an infinite number of times. All these words can be generated by the CFG because the production S ZS, used any number of times, produces words of the form ZZZ...S, where each Z may be replaced with the word under Kleene closure (i.e. X), since Z is the start state of the old CFG. The smallest word in L* (the empty word) can be generated by S /\. These constitute all the words in L* From the two parts above, we can conclude that the CFG generates all words in L*. Example 5 Context-free languages are closed under product. Consider the languages L1 and L2 generated by the two CFGs below. Write down a grammar that will generate L1L2, and prove that your grammar is correct. (Hint: The proof should be done in two directions.) CFG1 S aS | bX http://wikistudent.ws/Unisa X aY | bX Y bX | b CFG2 S aY | b | /\ X aX | b Y bX | a | /\ Solution 5 First give the nonterminals of the different CFGs different subscripts: CFG1 S1 aS 1 | bX 1 X1 aY 1 | bX 1 Y1 bX 1 | b CFG2 S2 aY 2 | b | /\ X2 aX 2 | b Y2 bX 2 | a | /\ Then combine all productions into a product CFG (L1L2), with a new start production: S S1 S 2 S1 aS 1 | bX 1 X1 aY 1 | bX 1 Y1 bX 1 | b S2 aY 2 | b | /\ X2 aX 2 | b Y2 bX 2 | a | /\ a) Prove that the new CFG generates all words in L1L2: The start production introduces the working string S 1 S 2 , after which productions from lines 2->4 must be used to generate a word from S 1 and productions from lines 5->7 to arrive at a word from S 2 . Words from L1 and L2 are thus concatenated (in that order). This is the only way in which words can be produced, and these words are precisely those in L1L2. Because different subscripts are given in lines 2->4 and 5->7, no overlapping of productions is possible, so no extra words are generated. b) Prove that all words in L1L2 can be generated by the CFG: Words in the language L1L2 begin with a word from L1, followed by a word from L2. The start production in the above CFG can be used to achieve a working string of this form. From there on, all words in L1 can be placed in the first half, since all of L1’s productions are in the new CFG (with the addition of subscripts, which don’t change the language generated). Similarly for L2, all words from that language can be generated and placed after an L1 word. http://wikistudent.ws/Unisa From a) and b) above, we can conclude that the new CFG generates the product language. Example 6 Context-free languages are closed under union. Consider the language L1 and L2 generated by two CFGs below. Write down a grammar that will generate L1 + L2, and prove that your grammar is correct. (Hint: The proof should be done in two directions). CFG1: S aS | bX X aX | bY Y bY | b CFG2: S aX | bY | /\ X aY | a Y bX | aY | /\ Solution 6 To obtain the union language of L1 and L2 (i.e. L1 + L2), first give the non-terminals in each CFG different subscripts, as follows: CFG1: S 1 aS 1 | bX 1 X1 aX 1 | bY 1 Y1 bY 1 | b CFG2: S 2 aX 2 | bY 2 | /\ X2 aY 2 | a Y2 bX 2 | aY 2 | /\ Then combine all productions into one new CFG, with additional start- productions that enable you to derive words from either start state of the above two CFGs: S S1 | S 2 S1 aS 1 | bX 1 X1 aX 1 | bY 1 Y1 bY 1 | b S2 aX 2 | bY 2 | /\ X2 aY 2 | a Y2 bX 2 | aY 2 | /\ Bi-directional proof that the CFG is correct: Show that all words generated by the new CFG can be generated by either CFG1 or CFG2: The start production S leads to two possibilities: Either S 1 or S 2 are introduced into the working string. From S 1 , only the productions in lines 2-4 apply, and for S 2 , only the productions in lines 5-7 can be http://wikistudent.ws/Unisa used. These are precisely the productions from the old CFG1 and CFG2, so all the words that are generated are in the union of CFG1 and CFG2. Note, one might think that the union CFG could generate extra words, not in CFG1 or CFG2, for instance if single words employed both CFG1 and CFG2 productions. However assigning the different subscripts to the CFGs has made this impossible. Show that all words generated by CFG1 or CFG2 can be generated by the new CFG: CFG1 has the 6 specific productions, given above. CFG2 has the 8 specific productions, given above. In the new CFG, all these productions were included, the only change to the non-terminals being the subscripts. Subscripts, however, do not change the words generated since they are added to what are merely placeholders for terminals. In addition to this, we also have the start production S S 1 | S 2 to reach the start-productions of the above-mentioned CFGs. It is therefore clear that all words generated by CFG1 and CFG2 can be generated by the new CFG. From the two parts above, we can conclude that the new CFG does indeed generate the union language.