How to design Context Free Grammar for the language $L=\{0^{i}1^{j}0^{j}1^{i} | i,j\ge0\}$?

271 Views Asked by At

I have tried following.

S -> AB
A -> 0A1|01
B -> 0B1|01

Are there better way to do it? Thanks

1

There are 1 best solutions below

1
On BEST ANSWER

Your grammar allows $S\to AB\to 01B\to 010B1\to 010011\notin L$, and it does not allow $S\to \epsilon$, for example. How about $$ S\to 0S1\mid A,\qquad A\to 1A0\mid \epsilon$$ ?