I'm interested in comparing code to find matches, i.e. to see if two different pieces of code are equivalent. For example, here are 4 matches for a method that returns the sum of two numbers (in Java).
int sum(int a, int b){
return a + b;
}
int sum(int a, int b){
return b + a;
}
int sum(int a, int b){
int sum = a + b;
return sum;
}
int sum(int a, int b){
int total = a + b;
return total;
}
While it's easy to do a text comparison of two pieces of source code, its difficult to write code that will recognize the above matches. This seems to be a job for a Parser or Compiler, but it doesn't need to be 'perfect', since it's just looking for matches.
This is for a Rails website, so ideally it should be able to work within Ruby, but I can also run a separate service. Treetop is a language for describing grammars, but describing the grammars is difficult too. Is there an existing tool to compare source code for multiple languages (such as Java, C++, Ruby and Python)?
It just needs to find matches between source code in one language at a time, though it would be cool if it could find matches between source codes in different languages too.
Update: A match isn't any code that produces the same result, it's code that uses the same process and steps to get the same result. The tool doesn't need to find every possible match, but it should be able to recognize code that is identical except for small differences, like variable names or order (as in above examples).