# prove (by grammar or closures) that the prefix language is context free

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.

-
Note, that sort of questions would be welcome in cs. Not that there's anything wrong with posting it here. – Karolis Juodelė Jan 4 '13 at 9:53

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 a $A_\varepsilon \to a$. 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.

-
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 4 '13 at 9: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 4 '13 at 10:20
Yuu also need $A_{\varepsilon}\to B_{\varepsilon}$, I think. – Henning Makholm Jan 4 '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 4 '13 at 13:28
@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 2 at 9:09

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.

-