arXiv:2411.01063cs.SEcs.AI2024-11被引 37

通过中间语言翻译链提升代码跨语言转换准确率

InterTrans: Leveraging Transitive Intermediate Translations to Enhance LLM-based Code Translation

  • 利用多步中间语言翻译路径填补源与目标语言的语法语义鸿沟
  • 在三个基准上相比直接翻译提升18.3%至43.3%的计算准确率
  • 特别适合需要高精度跨语言重构的工程场景

代码翻译旨在将程序从一种编程语言(PL)转换为另一种。这一长期存在的软件工程任务对现代化遗留系统、确保跨平台兼容性及提升性能至关重要,但自动化仍面临巨大挑战,主要源于不同语言间的大量语法和语义差异。尽管大型语言模型(LLMs),尤其是开源模型,已具备多语言代码处理能力,但其表现仍有不足。本文提出InterTrans,一种基于LLM的自动代码翻译方法,通过利用多语言间的传递性中间翻译路径,弥合源语言与目标语言之间的差距。该方法包含两个阶段:首先使用新颖的代码翻译树(ToCT)算法规划源与目标语言间的多步中间翻译序列,并按特定顺序验证;在CodeNet、HumanEval-X和TransCoder三个基准上,采用三种开源LLM进行评估,结果表明,与直接翻译(10次尝试)相比,InterTrans在计算准确率(CA)上绝对提升18.3%至43.3%。最佳变体(使用Magicoder LLM)在三个基准上的平均CA达到87.3%–95.4%。

原文摘要 · Abstract (English)

Code translation aims to convert a program from one programming language (PL) to another. This long-standing software engineering task is crucial for modernizing legacy systems, ensuring cross-platform compatibility, enhancing performance, and more. However, automating this process remains challenging due to many syntactic and semantic differences between PLs. Recent studies show that even advanced techniques such as large language models (LLMs), especially open-source LLMs, still struggle with the task. Currently, code LLMs are trained with source code from multiple programming languages, thus presenting multilingual capabilities. In this paper, we investigate whether such multilingual capabilities can be harnessed to enhance code translation. To achieve this goal, we introduce InterTrans, an LLM-based automated code translation approach that, in contrast to existing approaches, leverages intermediate translations across PLs to bridge the syntactic and semantic gaps between source and target PLs. InterTrans contains two stages. It first utilizes a novel Tree of Code Translation (ToCT) algorithm to plan transitive intermediate translation sequences between a given source and target PL, then validates them in a specific order. We evaluate InterTrans with three open LLMs on three benchmarks (i.e., CodeNet, HumanEval-X, and TransCoder) involving six PLs. Results show an absolute improvement between 18.3% to 43.3% in Computation Accuracy (CA) for InterTrans over Direct Translation with 10 attempts. The best-performing variant of InterTrans (with Magicoder LLM) achieved an average CA of 87.3%-95.4% on three benchmarks.

代码生成LLM应用跨语言转换

Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。