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,在此任务上仍面临困难。当前,代码LLM通过多编程语言源代码进行训练,因而具备多语言能力。本文探究此类多语言能力是否可用于增强代码翻译。为实现此目标,我们提出InterTrans——一种基于LLM的自动化代码翻译方法。与现有方法不同,InterTrans通过利用跨编程语言的中间翻译来弥合源语言与目标语言之间的语法和语义鸿沟。InterTrans包含两个阶段:首先采用新颖的代码翻译树算法规划给定源语言与目标语言之间的传递性中间翻译序列,随后按特定顺序验证这些序列。我们在包含六种编程语言的三个基准数据集上,使用三种开源LLM对InterTrans进行评估。结果表明,在10次尝试中,InterTrans相比直接翻译在计算准确率上实现了18.3%至43.3%的绝对提升。性能最佳的InterTrans变体在三个基准数据集上达到了87.3%-95.4%的平均计算准确率。