用神经符号方法自动翻译整个代码仓库并验证功能正确性。
AlphaTrans: A Neuro-Symbolic Compositional Approach for Repository-Level Code Translation and Validation
- 将程序分解为片段,逆调用顺序翻译以降低复杂度。
- 96.4%翻译片段语法正确,27.03%通过运行时验证,25.14%功能正确。
- 适合大规模开源项目迁移,提供错误报告供开发者修复。
代码翻译将程序从一种编程语言转换为另一种。传统规则式转译器因语言演化易过时且无法泛化。近期研究使用大语言模型(LLM)自动化翻译,但在真实项目中因依赖、自定义类型和语言特性而失效。本文提出AlphaTrans,一种神经符号方法实现仓库级代码翻译与验证。该方法同时翻译源码与测试代码,并通过多级验证确保功能一致。为降低LLM处理难度,AlphaTrans利用程序分析将代码拆解为片段,按逆调用顺序翻译。我们用AlphaTrans成功翻译了10个真实开源项目,包含<836, 8575, 2719>个类、方法和测试,共生成17874个片段。96.40%的片段语法正确,27.03%通过运行时行为验证,25.14%通过功能正确性验证。平均耗时34小时完成一项目,具备实际可扩展性。对错误翻译,系统生成包含原始翻译、堆栈信息、测试失败或断言异常的报告。我们邀请两名开发者修复四个项目的翻译错误,平均耗时20.1小时,最终所有测试通过。
原文摘要 · Abstract (English)
Code translation transforms programs from one programming language (PL) to another. Several rule-based transpilers have been designed to automate code translation between different pairs of PLs. However, the rules can become obsolete as the PLs evolve and cannot generalize to other PLs. Recent studies have explored the automation of code translation using Large Language Models (LLMs). One key observation is that such techniques may work well for crafted benchmarks but fail to generalize to the scale and complexity of real-world projects with dependencies, custom types, PL-specific features, etc. We propose AlphaTrans, a neuro-symbolic approach to automate repository-level code translation. AlphaTrans translates both source and test code, and employs multiple levels of validation to ensure the translation preserves the functionality of the source program. To break down the problem for LLMs, AlphaTrans leverages program analysis to decompose the program into fragments and translates them in the reverse call order. We leveraged AlphaTrans to translate ten real-world open-source projects consisting of <836, 8575, 2719> classes, methods, and tests. AlphaTrans breaks down these projects into 17874 fragments and translates the entire repository. 96.40% of the translated fragments are syntactically correct, and AlphaTrans validates the translations' runtime behavior and functional correctness for 27.03% and 25.14% of fragments. On average, the integrated translation and validation take 34 hours to translate a project, showing its scalability in practice. For the incorrect translations, AlphaTrans generates a report including existing translation, stack trace, test errors, or assertion failures. We provided these artifacts to two developers to fix the translation bugs in four projects. They were able to fix the issues in 20.1 hours on average and achieve all passing tests.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。