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

8.9k Views Asked by At

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.

2

There are 2 best solutions below

10
On BEST ANSWER

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.

0
On

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.