arXivDaily arXiv每日学术速递 周一至周五更新
稍安勿躁,官方尚未更新
arXiv 2412.08035cs.PLcs.SE
收藏

使用大语言模型实现整个项目的可扩展、经验证的代码翻译

Scalable, Validated Code Translation of Entire Projects using Large Language Models

Hanliang Zhang, Cristina David, Meng Wang, Brandon Paulsen, Daniel Kroening

AI总结:

针对大语言模型代码翻译可扩展性差、长代码成功率低的问题,提出含特性映射与类型兼容性的模块化翻译方法,在Go转Rust任务中实现了更高的函数验证成功率。

摘要:

大语言模型(LLMs)凭借生成地道代码的能力,在代码翻译领域展现出应用前景。然而,将LLMs用于代码翻译时存在一个显著局限:可扩展性不足——现有研究表明,当代码行数超过约100行时,翻译成功率会出现下降。我们通过开发一种模块化翻译方法突破了这一限制:该方法将代码拆分为多个小型代码片段,这些片段可独立翻译并进行语义验证(即检查输入/输出(I/O)等价性)。\n我们发现,若直接套用这种方法,LLMs在翻译源语言中与目标语言无直接映射关系的语言特性时并不可靠,且LLM在尝试修复错误时经常陷入修复循环。为解决这些问题,我们提出两个核心概念:(1)特性映射(feature mapping):将预定义的翻译规则与基于LLM的翻译相结合,引导LLM处理细微的语言差异,生成语义准确的代码;(2)类型兼容性(type-compatibility):支持在函数签名层面进行局部检查,及早发现错误,从而缩小潜在修复范围。\n我们将该方法应用于真实世界Go代码库到Rust的翻译任务,结果表明:对于最多含6600行代码、369个函数的项目,我们的方法能够持续生成可靠的Rust翻译版本,平均有73%的函数通过I/O等价性验证,这一表现远优于现有所有研究工作。

英文摘要:

Large language models (LLMs) show promise in code translation due to their ability to generate idiomatic code. However, a significant limitation when using LLMs for code translation is scalability: existing works have shown a drop in translation success rates for code exceeding around 100 lines. We overcome this limitation by developing a modular approach to translation, where we partition the code into small code fragments which can be translated independently and semantically validated (that is, checking I/O equivalence). When this approach is applied naively, we discover that LLMs are unreliable when translating features of the source language that do not have a direct mapping to the target language, and that the LLM often gets stuck in repair loops when attempting to fix errors. To address these issues, we introduce two key concepts: (1) feature mapping, which integrates predefined translation rules with LLM-based translation to guide the LLM in navigating subtle language differences and producing semantically accurate code; and (2) type-compatibility, which facilitates localized checks at the function signature level to detect errors early, thereby narrowing the scope of potential repairs. We apply our approach to translating real-world Go codebases to Rust, demonstrating that we can consistently generate reliable Rust translations for projects up to 6,600 lines of code and 369 functions, with an average of 73% of functions successfully validated for I/O equivalence, considerably higher than any existing work.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑