# Grammar of words with exactly $k$ prefixes in another grammar

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.

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.