Given a context-free grammar $G$, how can one systematically construct a grammar $G_k$ such that

$$ L(G_k) = \{w \in \Sigma^* : |\text{Pref}(w) \cap L(G)| = k\} $$

where $\text{Pref}(w)$ is the set of $|w|+1$ prefixes of $w$? Assume $G$ is unambiguous, if needed.


Unfortunately there is no such construction, as the context-free languages are not closed under this operation.

Consider the language $\{ a^nb^n \mid n\ge 1\} \cup \{ a^kb^nc^n \mid k,n\ge 1 \}$.

The language that has two prefixes in this language is $\{ a^nb^nc^n \mid n\ge 1\}\cdot \{a,b,c\}^*$, which is not context-free.


Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.