I'm currently attempting to construct a grammar for the language $L = \{0^a1^b2^c3^d | a,b,c,d \in \mathbb{N} \land a+b = c+d\}$
However I'm getting stuck on constructing the rules in a way that the $2$s and $3$s always appear in the correct order.
My current approach is the following ruleset with $E$ being the initial rule. \begin{align*} E &\rightarrow \epsilon &E &\rightarrow AC\\ F &\rightarrow BC &A &\rightarrow 0\\ A &\rightarrow 0E &A &\rightarrow B\\ B &\rightarrow 1 &B &\rightarrow 1F\\ C &\rightarrow 2 &C &\rightarrow 3 \end{align*} However this also incorrectly accepts the word $w = 0032$. How can I make sure no $2$ ever follows a $3$?
I’d take a different approach altogether:
$$\begin{align*} E&\to X_{03}\mid X_{02}\mid X_{13}\mid X_{12}\mid\epsilon\\ X_{03}&\to 0X_{03}3\mid 0X_{02}3\mid 0X_{13}3\mid 0X_{12}3\mid\epsilon\\ X_{02}&\to 0X_{02}2\mid 0X_{12}2\mid\epsilon\\ X_{13}&\to 1X_{13}3\mid 1X_{12}3\mid\epsilon\\ X_{12}&\to 1X_{12}2\mid\epsilon \end{align*}$$