Properties of Contextfree Languages By Mr R Karunamoorthi
Properties of Context-free Languages By, Mr. R. Karunamoorthi, AP / CSE 1
Closure Property Results n CFLs are closed under: n n n n Union Concatenation Kleene closure operator Substitution Homomorphism, inverse homomorphism reversal CFLs are not closed under: n n n Intersection Difference Complementation Note: Reg languages are closed under these operators 2
Strategy for Closure Property Proofs n First prove “closure under substitution” Using the above result, prove other closure properties n CFLs are closed under: n n Prove this first n n n Union Concatenation Kleene closure operator Substitution Homomorphism, inverse homomorphism Reversal 3
Note: s(L) can use a different alphabet The Substitution operation For each a ∑, then let s(a) be a language If w=a 1 a 2…an L, then: n s(w) = { x 1 x 2 … } s(L), s. t. , xi s(ai) Example: n n n Let ∑={0, 1} Let: s(0) = {anbn | n ≥ 1}, s(1) = {aa, bb} If w=01, s(w)=s(0). s(1) n E. g. , s(w) contains a 1 b 1 aa, a 1 b 1 bb, a 2 b 2 aa, a 2 b 2 bb, … and so on. 4
CFLs are closed under Substitution IF L is a CFL and a substititution defined on L, s(L), is s. t. , s(a) is a CFL for every symbol a, THEN: n s(L) is also a CFL What is s(L)? L w 1 w 2 w 3 w 4 … s(L) s(w 1) s(w 2) s(w 3) s(w 4) … Note: each s(w) is itself a set of strings 5
CFLs are closed under Substitution n n G=(V, T, P, S) : CFG for L Because every s(a) is a CFL, there is a CFG for each s(a) n n Construct G’=(V’, T’, P’, S) for s(L); n n Let Ga = (Va, Ta, Pa, Sa) V’ has V and all V for a ∑; T’ has T and all a ∑ P’ consists of: n n The productions of P, but with every occurrence of terminal “a” in their bodies replaced by Sa. All productions in any Pa, for any a ∑ Parse tree for G’: Sa 1 As w = x 1 x 2. . xn; CFL S Sa n Sa 2 … x 1 x 2 xn 6
Substitution of a CFL: example n n Let L = language of binary palindromes s. t. , substitutions for 0 and 1 are defined as follows: n s(0) = {anbn | n ≥ 1}, s(1) = {xx, yy} Prove that s(L) is also a CFL. CFG for L: CFG for s(0): CFG for s(1): S=> 0 S 0|1 S 1| S 0=> a. S 0 b | ab S 1=> xx | yy Therefore, CFG for s(L): S=> S 0 SS 0 | S 1 S S 1 | S 0=> a. S 0 b | ab S 1=> xx | yy 7
CFLs are closed under union Let L 1 and L 2 be CFLs To show: L 2 U L 2 is also a CFL Let us show by using the result of Substitution n Make a new language: n Lnew = {a, b} s. t. , s(a) = L 1 and s(b) = L 2 ==> s(Lnew) == same as == L 1 U L 2 n A more direct, alternative proof n Let S 1 and S 2 be the starting variables of the grammars for L 1 and L 2 n Then, Snew => S 1 | S 2 8
CFLs are closed under concatenation n Let L 1 and L 2 be CFLs Let us show by using the result of Substitution n Make Lnew= {ab} s. t. , s(a) = L 1 and s(b)= L 2 ==> L 1 L 2 = s(Lnew) n A more direct, alternative proof n Let S 1 and S 2 be the starting variables of the grammars for L 1 and L 2 n Snew => S 1 S 2 9
CFLs are closed under Kleene Closure n Let L be a CFL n Let Lnew = {a}* and s(a) = L 1 n Then, L* = s(Lnew) 10
We won’t use substitution to prove this result CFLs are closed under Reversal n n Let L be a CFL, with grammar G=(V, T, P, S) For LR, construct GR=(V, T, PR, S) s. t. , n If A==> is in P, then: n n n A==> R is in PR (that is, reverse every production) Sentential forms of G are reversal of sentential forms of GR 11
homorphism n L is in CFL over Σ and h is a homomorphism on Σ. Let ‘s’ be substitution that replaces very symbol ‘a’ in Σ by a lang of one string h(a). s(a)=h(a). Then h(L)=s(L) 12
Some negative closure results CFLs are not closed under Intersection n n Existential proof: n n i n L 1 = {0 1 2 | n≥ 1, i≥ 1} i n n n L 2 = {0 1 2 | n≥ 1, i≥ 1} Both L 1 and L 1 are CFLs n S AB; A 0 A 1 | 01; B 2 B | 2 ( same no of 0’s and 1’s) n S AB ; A 0 A | 0; B 1 B 2 | 12 (same no of 1’s and 2’s) But L 1 L 2 cannot be a CFL n Why? same no of 0, 1, 2 not CFL We have an example, where intersection is not closed. 13
Some negative closure results CFLs are not closed under complementation n n Follows from the fact that CFLs are not closed under intersection L 1 L 2 = L 1 U L 2 Logic: if CFLs were to be closed under complementation the whole right hand side becomes a CFL (because CFL is closed for union) the left hand side (intersection) is also a CFL but we just showed CFLs are NOT closed under intersection! CFLs cannot be closed under complementation. 14
Some negative closure results CFLs are not closed under difference n n Follows from the fact that CFLs are not closed under complementation Because, if CFLs are closed under difference, then: n n n L = ∑* - L So L has to be a CFL too Contradiction 15
- Slides: 15