# Easy proof for context-free languages being closed under cyclic shift

The cyclic shift (also called rotation or conjugation) of a language $L$ is defined as $\{ yx \mid xy \in L \}$. According to wikipedia (and here) the context-free languages are closed under this operation, with references to papers from Oshiba and from Maslov. Is there an easy proof of this fact?

For regular languages the closure is discussed in this form as "Prove that regular languages are closed under the cycle operator".

You can try to use pushdown automata. Given a pushdown automaton for the original language, we construct one for the cyclic shift. The new automaton operates in two stages, corresponding to the $y$ and the $x$ part of the word $yx$ (where $xy$ is in the original language). In the first stage, whenever the automaton would like to pop a non-terminal $A$, it can instead push a non-terminal $A'$; the idea is that at the end of the first stage, the stack would contain, in reverse order, the symbols that are found in the stack after reading $x$ by the original automaton. In the second stage (the switch is non-deterministic), instead of pushing a non-terminal $A$, we are allowed to pop a non-terminal $A'$. If the original automaton can indeed generate the stack upon reading $x$, then the new one would be able to exactly pop the entire stack.

Edit: Here are some more details. Suppose we are given a PDA with alphabet $\Sigma$, set of states $Q$, set of accepting states $F$, non-terminals $\Gamma$, initial state $q_0$, and a set of allowable transitions. Each allowable transition is of the form $(q,a,A,q',\alpha)$, meaning that when in state $q$, upon reading $a \in A$ (or $a = \epsilon$, in which case it's a free transition), if the top-of-stack is $A \in \Gamma$ (or $A = \epsilon$, which means stack is empty), then the PDA can (it's a non-deterministic model) move to state $q'$, replacing $A$ with $\alpha \in \Gamma^*$.

The new PDA has a new non-terminal $A'$ for each $A \in \Gamma$. For every two states $q,q' \in Q$ and $A \in \Gamma \cup \{\epsilon\}$, there are two states $(q,q',1),(q,q',2,A)$. The starting states (the actual starting state is chosen non-deterministically among them via $\epsilon$-transitions) are $(q,q,1)$. For each transition $(q,a,A,q',\alpha)$ there are corresponding transitions $((q,q'',1),a,A,(q',q'',1),\alpha)$ and $((q,q'',2,B),a,A,(q',q'',2,B),\alpha)$. There are other transitions as well.

For each transition $(q,a,A,q',\alpha)$, there are transitions $((q,q'',1),a,B',(q',q'',1),B'A'\alpha)$, where $B \in \Gamma \cup \{\epsilon\}$ and $\epsilon' = \epsilon$. For every final state $q \in F$, there are transitions $((q,q'',1),\epsilon,A,(q_0,q'',2,\epsilon),A)$, where $A \in \Gamma \cup \{\epsilon\}$.

For every transition $(q,a,\epsilon,q',\alpha)$, there are transitions $((q,q'',2,A),a,B',(q',q'',2,A),B'\alpha)$, where $A \in \Gamma \cup \{\epsilon\}$. For every transition $(q,a,\epsilon,q',A)$, there are transitions $((q,q'',2,B),a,A',(q',q'',2,A),\epsilon)$, where $B \in \Gamma \cup \{\epsilon\}$. For every transition $(q,a,A,q',B)$, there are "generalized transitions" $((q,q'',2,C),a,B'A,(q,q'',2,C),\epsilon)$; these are implemented as a sequence of two transitions through an intermediate new state. Transitions $(q,a,\epsilon,q',\alpha)$ with $|\alpha| \geq 2$ are handled similarly. For every transition $(q,a,A,q',A)$, there are transitions $((q,q'',2,A),a,B,(q',q'',2,A),B)$, where $B \in \Gamma' \cup \{\epsilon\}$. Transitions $(q,a,A,q',A\alpha)$ are handled similarly. Finally, there is a sole final state $f$, and transitions $((q,q,2,A),\epsilon,\epsilon,f,\epsilon)$.

(There might be a few transitions that I missed, and some of the details that I'm omitting are somewhat messy.)

Recall we're trying to accept a word $yx$, where $xy$ is accepted by the original PDA. A state $(q,q',1)$ means that we're at stage 1, at state $q$, and the original PDA is at state $q'$ after reading $x$. A state $(q,q',2,A)$ is similar, where $A$ corresponds to the last $A'$ that was popped. At stage 1, we are allowed to push $A'$ instead of popping $A$. We do that for each non-terminal that is produced while processing $x$, but only popped while processing $y$. At stage 2, we are allowed to pop $A'$ instead of pushing $A$. If we do this, then we have to remember that the top-of-stock is really $A$; this only applies when there are no "temporary" things on the stack, which in the simulated PDA is the same as the top-of-stack being $\epsilon$ or of the form $B'$.

Here is a simple example. Consider an automaton for $x^n y^n$ that pushes $A$ for each $x$, and pops $A$ for each $y$. The new automaton accepts words of two forms: $y^k x^n y^{n-k}$ and $x^k y^n x^{n-k}$. For words of the first form, stage 1 consists of pushing $k$ times $A'$, stage 2 consists of popping $k$ times $A'$, pushing $n-k$ times $A$, and popping $n-k$ times $A$. For words of the second form, we first push $k$ times $A$, then pop $k$ times $A$, push $n-k$ times $A'$, transition to stage 2, and pop $n-k$ times $A'$.

Here is a more complicated example, for the language of balanced parentheses of various types ("()","[]","<>") such that the immediate descendants of each type of parentheses must belong to a different type. For example, "([]<>)" is OK but "()" is wrong. For each "(", we push $A$ if the top-of-stack isn't $A$, for each ")", we pop $A$. Similarly $B$,$C$ are associated with "[]" and "<>". Here is how we accept the word ">)([()]<". We consume ">)", pushing $C'A'$, and transition to stage 2. We consume "(", popping $A'$ and remembering the top-of-stack $A$. We consume "[()]" , pushing and popping $BA$; when pushing $B$, we are aware that the "real" top-of-stack is $A$, and so square brackets are allowed (we wouldn't be fooled by ">)(()<"); when pushing $A$, since the top-of-stack is $B$ (which is not $\epsilon$ or of the form $X'$), then we know that $B$ is also the "real" top-of-stack, and so round parentheses are allowed (even though the shadow top-of-stack is $A$). Finally, we consume "<" and pop $C'$.

• Sorry, I'm having trouble understanding -- can you explain further? Where does the automaton start and what are its transitions? And does the $A \to A^{\prime}$ switch happen for every stack symbol? Thanks!
– usul
Jan 10 '13 at 4:00
• Very interesting suggestion. Thanks. I will chew on this a little, to let it sink in. Jan 10 '13 at 8:19
• @usul, You'll have to fill-in the details yourself. The switch $A \rightarrow A'$ (in the first stage) should happen when the automaton "wants" to pop $A$ but cannot, and instead it pushes $A'$. You can think of it as a non-deterministic move. Jan 10 '13 at 8:33
• @Yuval: sorry but I cannot make this happen. As I understand your idea, the new automaton starts by simulating the $y$ part, changing pops and pushes. Then generate $\alpha$ on the stack, where the original automaton starts with $\alpha$ when reading $y$. Waht is the original starts by pushing? Then the nwe automaton needs to pop from the empty stack. I still think your intuition is worthwhile, but some extra care seems needed. Jan 13 '13 at 19:18
• @Hendrik, I'm sorry, but I can't follow your counterexample. At what point do you think that the new automaton needs to pop from the empty stack? Jan 14 '13 at 4:37

Consider Greibach normal form. To construct a shifted language you only need to change productions $S \to \alpha A_1 \dots A_n$ to $S \to A_1\dots A_n \alpha$ and add a new non-terminal $S'$ that behaves like $S$ used to, in case some some production referenced $S$.

• Thanks, but this is shift by a single letter. I am interested in general rotation, by an arbitrary number of letters. Jan 8 '13 at 21:25
• @HendrikJan, Well, if $\text{shift}_1(L)$ is context free, so $\text{shift}_n(L) = \text{shift}_1(\text{shift}_1(\dots (L) \dots)$ will surely be context free as well. You can construct a grammar for it by applying the method I suggested $n$ times. You can also construct the $\text{shift}_n(L)$ grammar directly, by "flattening" the given grammar. For instance if $n = 2$ and the grammar has productions $S \to \alpha AB$ and $A \to \beta C$, you'd add a production $S \to \alpha \beta CB$ and rotate that. Of course, the size of your grammar might grow very quickly. Jan 8 '13 at 21:33
• For fixed $n$ you are right. But here $n$ is fixed nor bounded. For instance if $L = \{ a^nb^n \mid n\ge 0 \}$ then we obtain $\{ a^kb^na^{\ell} \mid k+\ell = n \} \cup \{ b^ka^nb^{\ell} \mid k+\ell = n \}$. Jan 9 '13 at 0:25
• @HendrikJan, I see. I wrongly assumed the question was about a finite shift. I'll reconsider my answer... Jan 9 '13 at 9:29

It turned out to be a good idea to check the old Hopcroft and Ullman classic Introduction to Automata Theory (1979). Closure under cycle is Exercise 6.4c and is marked S**. The double stars mean it is one of the most difficult problems (in the book). Fortunately the S indicates it is one of the selected problems with a solution.

The solution is as follows. Take a CFG in Chomsky normal form. Consider any derivation tree and basically turn it upside down. Consider a path $S=X_1, X_2, \dots , X_n$ in the original tree. To the left the tree has contributions $x_1, x_2, \dots, x_n$ to the right $y_1, y_2, \dots, y_n$, meaning the string derived equals $x_1 x_2 \dots x_n y_n \dots y_2 y_1$. (Actually as the grammar is CNF when the path continues left, the contribution will be to the right and the corresponding $x_i$ is empty, etc.)

The tree upside down has a path $S', \hat X_n, \dots \hat X_2, \hat X_1$ with contributions $y_n, \dots, y_2 y_1$ to the left and $x_n, \dots, x_2 x_1$ to the right, so the result is a derivation for $y_n \dots y_2 y_1 x_1 x_2 \dots x_n$. As required.

Full details of the construction are given in the book.

Note how this reminds of the (accepted) solution by Yuval. The nonterminals that are pushed instead of popped are in the opposite order on the stack. Quite similar to upside down in the tree.