Skip to Main Content
It is important for improving program to detect duplicated code (also known as code clone) in the field of software development and maintenance. The existing solutions can not effectively analyze the complex semantics of programs, and so can not effectively detect functionally equivalent code fragments that execute the same operations but with different syntax. This paper proposes a method to detect functionally equivalent code fragments via k-nearest neighbor algorithm. Firstly, the source code is statically analyzed to get the information of control dependency and data flow; then, k-nearest neighbor clustering algorithm is performed to form functionally independent code fragments; next, the input and output variables of the code fragments are identified, and the code fragments are transformed to compilable and executable functions, then the automatic random assignment for the input variables of functions are carried out. At last, the functions are dynamically tested and are classified into different clusters according to the testing outputs. Experimental results show that this method can effectively get functionally equivalent code fragments with a lower time complexity.