# Is equivalence of CFGs decidable for finite sets of grammars?

Is there a way to show that for all finite sets $S$ of context free grammars, there exists a Turing Machine $M$ such that for all grammars $G_1, G_2 \in S$, we have that $M(G1,G2)$ terminates and answer yes if and only if $L(G_1)=L(G_2)$? Is that even true?

I know that this problem is in general undecidable, but I also know that a finite set is in general decidable. I just can't figure out how to determine the answer for this problem.

-
When you say for all sets S of context free grammars, do you mean for all finite sets S of context free grammars? If so, then there does exist a Turing machine for $M_S$ which decides where $L(G_1) = L(G_2)$ for $G_1, G_2 \in S$. The reason? There are finitely many comparisons (in fact, $|S|^2$ of them) and the Turing machine may have the correct result hard-coded for each case. Since any two grammars can be compared via equality, the function is computable, even if we (in general) have no algorithmic way to determine the values to hard-code into the TM. –  Patrick87 Feb 11 at 21:18
@Patrick87 Even assuming that's what he meant, that still only proves that $\forall$ finite sets of grammars, $\exists$ a TM that decides language equality. It doesn't show that $\exists$ TM which $\forall$ finite sets of grammars decides equality. –  G. Bach Feb 11 at 21:52
Yes sorry, i meant for all FINITE sets S ... –  user222 Feb 11 at 21:59
What i don't get is the reason behind the "may have the correct result hard-coded for each case". I mean, I give you a set S with a finite number of CFG Grammars, how can you even know that two of them generates the same language? Where does that piece of knowledge come from? The problem says that for all S exists M, what does make it true in general? Sorry if it sounds stupid but I really don't get it. –  user222 Feb 11 at 22:14
@G.Bach Yes, I assume he meant the former, since otherwise my argument doesn't really make any sense. Looking at his question again, it does appear he's asking it so that my comment applies: for all finite sets S, does there exist a TM? Not: does there exist a TM, such that for all finite sets S, ... –  Patrick87 Feb 11 at 22:14

A problem is decidable if there is a TM which decides instances of the problem.

Given two arbitrary CFGs $G_1$ and $G_2$, we know this for certain: either $L(G_1) = L(G_2)$, or $L(G_1) \neq L(G_2)$. Consider the first case: a TM that accepts on the input $(G_1, G_2)$ correctly decides this problem. Consider the second case: a TM that rejects on the input $(G_1, G_2)$ would be correct in that case.

Now, consider a finite set of grammars $\{G_1, G_2, ..., G_n\}$. For any two grammars $G_i$ and $G_j$ in this set, we know that either $L(G_i) = L(G_j)$ or $L(G_i) \neq L(G_j)$.

Consider the following TMs:

TM #1:

if input is (G_1, G_1) then accept
if input is (G_1, G_2) then accept
...
if input is (G_n, G_n) then accept


TM #2:

if input is (G_1, G_1) then accept
if input is (G_1, G_2) then accept
...
if input is (G_n, G_n) then reject


...

TM #N

if input is (G_1, G_1) then reject
if input is (G_1, G_2) then reject
...
if input is (G_n, G_n) then reject


Each TM can either accept or reject for each of the $n^2$ (ordered) inputs; so there are $N = 2^{n^2}$ different TMs (as many as binary strings of length $n^2$).

One of these TMs is guaranteed to correctly decide your problem. I can't tell you which one, but one of these gives all the correct answers. Since there is a TM for your problem, it's decidable.

-
My hero. Thank you very much, it's all clear now! –  user222 Feb 11 at 22:35
The existential argument is also found in other contexts. –  Raphael Feb 12 at 17:26
Actually the undecidable problem is a generalization of this question, since the undecidable problem is "given two grammars, do they produce the same language?", which translates to "$\exists$ a TM $M$ such that $\forall G_1, G_2$, $M$ decides whether $L(G_1) = L(G_2)$?" In the question of user222, the quantifiers are switched, so there may be many TMs, each only deciding equality of a specific finite subset of all grammars. –  G. Bach Feb 11 at 22:56