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.


1 Answer 1


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 agree to our terms of service, privacy policy and cookie policy

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