3

I have a language $L$, which is context-free, and I have $Pref(L)$, which is the language of all the prefixes of the words in $L$.

I need to prove that $pref(L)$ is context free only with closures or grammar.

I can use some help please...thanks in advance

P.S. I'm aware of the fact that there is a question about this language already, but I didn't manage to understand anything of it, and I don't have the permission to comment on the other question. Sorry in advance for duplicating.

Amzoti
  • 56,093
DanielY
  • 979

2 Answers2

8

I suggest that you take your grammar in Chomsky Normal Form and for every production $A \to BC$ add productions $A_\varepsilon \to BC_\varepsilon$ and $A_\varepsilon \to B_\varepsilon$. Also, for every $A \to a$ add the two rules $A_\varepsilon \to a$ and $A_\varepsilon \to \varepsilon$. Finally change the starting symbol to $S_\varepsilon$ and add $S_\varepsilon \to \varepsilon$. Now any derivation tree will have some $X_\varepsilon$ as the rightmost symbol and it will thus be possible to terminate it at any step.

fmatt
  • 115
Karolis Juodelė
  • 9,702
  • 1
  • 25
  • 39
  • Thank you so much! One thing only - is S'(you called it S epsilon) points to the original S? if not, where to then? – DanielY Jan 04 '13 at 09:58
  • If $S \to XY$ then $S_\varepsilon \to XY_\varepsilon$ as for any other $\varepsilon$ production I suggested to introduce. I'm not sure if it is okay to change the starting symbol though (as $S$ is traditional). If not, some renaming can solve that... – Karolis Juodelė Jan 04 '13 at 10:20
  • Yuu also need $A_{\varepsilon}\to B_{\varepsilon}$, I think. – hmakholm left over Monica Jan 04 '13 at 13:07
  • @HenningMakholm, yes, if $A \to B$ is in your definition of the normal form. It seems productions of that kind are often omitted. They are easy to replace, after all. – Karolis Juodelė Jan 04 '13 at 13:28
  • @KarolisJuodelė: No, I mean if $A\to BC$ is in the original grammar, then you need $A_{\varepsilon}\to B_{\varepsilon}$ in order for $A_{\varepsilon}$ to generate all prefixes of what $A$ generates. – hmakholm left over Monica Jan 04 '13 at 15:08
  • @HenningMakholm, I see you are right. I'll fix the answer... – Karolis Juodelė Jan 04 '13 at 15:39
  • @KarolisJuodelė Hi , you added new variables to keep it in Chomsky Normal Form? if we don't care if the new CFG will be in HNF or not, we could just add for all rules of the form $A\to BC$ the rule $A\to B$. right? (of course under the assumption that we start with a HNF CFG) – sha Apr 01 '16 at 15:05
  • 1
    @sha no,you need the extra variables. For example, if the grammar is $S \to XX, X \to AB, A \to a, B \to b$ (so that $L = {abab}$), and then you add $X \to A$, then new grammar will also produce the word $aa$ which is not a prefix of $abab$. – Karolis Juodelė Apr 02 '16 at 09:09
  • Do you mean that with every $X_{\varepsilon}$ comes a rule of the form $X_{\varepsilon} \to \varepsilon$? Otherwise your statement about termination at any time does not make sense to me... – StefanH Feb 03 '17 at 12:13
  • @StefanH no, you cut off the end of a string by choosing the rule $A_\varepsilon \to B_\varepsilon$ instead of $A_\varepsilon \to BC_\varepsilon$. While some of the cutting could be done using $X \to \varepsilon$ rules, it's neither necessary nor sufficient. – Karolis Juodelė Feb 04 '17 at 13:15
5

Karolis has given a nice construction involving grammars, I will add the alternative options given in your question, closure properties.

Let $\Sigma$ be the alphabet for your language. We take a copy $\bar\Sigma = \{ \bar a \mid a\in \Sigma\}$. Let $h : (\Sigma\cup\bar\Sigma)^* \to \Sigma^*$ be the morphism that removes bars: $h(a) = a$, $h(\bar a)=a$ for all $a\in\Sigma$. Let $g : (\Sigma\cup\bar\Sigma)^* \to \Sigma^*$ be the morphism that deletes barred letters: $g(a) = a$, $g(\bar a)=\varepsilon$ for all $a\in\Sigma$.

Then for $u\in\Sigma^*$, $h^{-1}(u)$ is the set of strings that result from $u$ by "barring" an arbitrary subset of letters in the string. In that we we select them for deletion. In the prefix operation we cannot delete arbitrary letters, by only suffixes. Thus we restrict the selection by intersection with a proper regular language: $pref(L) = g( h^{-1}(L) \cap \Sigma^*\bar\Sigma^* )$.

Same approach for instance: language of subwords $g( h^{-1}(L) \cap \bar\Sigma^*\Sigma^*\bar\Sigma^* )$, delete every even letter in the string $g( h^{-1}(L) \cap ( (\Sigma\bar\Sigma)^* \cup (\Sigma\bar\Sigma)^*\Sigma ) )$.

Formally, any family of languages closed under morphisms, inverse morphisms, and intersection with regular languages is closed under prefix. Such a family is called cone or full trio. So also valid for regular languages and recursively enumerable languages.

Hendrik Jan
  • 1,910