[论文解读] Semantic Alignment-Enhanced Code Translation via an LLM-Based Multi-Agent System
TransAGENT 是一个基于多智能体的大语言模型系统,通过四个专业智能体迭代修复语法和语义错误,从而提升代码翻译质量:初始代码翻译器、语法错误修复器、代码对齐器和语义错误修复器。通过利用执行对齐的代码块定位技术,该系统在小规模大语言模型上相较于 UniTrans 显著提升了翻译准确率与效率,尤其在 LLaMA-7B 上进行 Java 到 Python 的翻译任务时,准确率提升了 0.65% 的绝对值。
Code translation converts code from one programming language to another while maintaining its original functionality, which is crucial for software migration, system refactoring, and cross-platform development. Traditional rule-based methods rely on manually-written rules, which can be time-consuming and often result in less readable code. To overcome this, learning-based methods have been developed, leveraging parallel data to train models for automated code translation. More recently, the advance of Large Language Models (LLMs) further boosts learning-based code translation. Although promising, LLM-translated program still suffers from diverse quality issues (e.g., syntax errors and semantic errors). In particular, it can be challenging for LLMs to self-debug these errors when simply provided with the corresponding error messages. In this work, we propose a novel LLM-based multi-agent system TRANSAGENT, which enhances LLM-based code translation by fixing the syntax errors and semantic errors with the synergy between four LLM-based agents, including Initial Code Translator, Syntax Error Fixer, Code Aligner, and Semantic Error Fixer. The main insight of TRANSAGENT is to first localize the error code block in the target program based on the execution alignment between the target and source program, which can narrow down the fixing space and thus lower down the fixing difficulties. To evaluate TRANSAGENT, we first construct a new benchmark from recent programming tasks to mitigate the potential data leakage issue. On our benchmark, TRANSAGENT outperforms the latest LLM-based code translation technique UniTrans in both translation effectiveness and efficiency; additionally, our evaluation on different LLMs show the generalization of TRANSAGENT and our ablation study shows the contribution of each agent.
研究动机与目标
- 为解决在使用小规模模型时,基于大语言模型的代码翻译中持续存在的语法与语义错误问题。
- 通过使大语言模型能够以结构化、协作式智能体工作流的方式进行自我调试,提升翻译的正确性与效率。
- 开发一种稳健且可泛化的跨语言程序映射方法,能够抵御代码重排与结构变化的影响。
- 通过利用源程序与目标程序之间的执行对齐,定位问题代码块,降低错误修复的难度。
- 在新构建的基准测试上进行评估,以避免数据泄露,并验证系统在不同大语言模型上的泛化能力。
提出的方法
- 该系统采用四个基于大语言模型的智能体:初始代码翻译器利用测试用例生成初始翻译;语法错误修复器通过分析错误信息并生成补丁来修复编译错误。
- 代码对齐器利用控制流图将源程序与目标程序划分为逻辑代码块,并通过大语言模型进行映射,实现细粒度的错误定位。
- 语义错误修复器通过比较程序执行结果来识别功能差异,并利用执行对齐的数值指导针对性修复。
- 错误定位通过执行对齐实现——在相同测试输入下比较运行时行为,以隔离出故障代码块。
- 智能体以迭代方式协作:在修复语法错误后,重新评估执行结果,并在反馈循环中处理语义错误。
- 从近期的编程任务中构建了一个新基准,以防止数据泄露并确保评估的公平性。

实验结果
研究问题
- RQ1多智能体系统是否能通过系统性地处理语法与语义错误,提升基于大语言模型的代码翻译正确性?
- RQ2执行对齐在跨不同编程语言定位易错代码块方面有多有效?
- RQ3所提出的多智能体工作流是否在准确率与效率上均优于现有的迭代修复方法(如 UniTrans)?
- RQ4TransAGENT 在不同规模与架构的大语言模型上具有多大程度的泛化能力?
- RQ5各智能体在整体性能提升中分别起到了何种贡献?
主要发现
- 在新基准上,TransAGENT 在翻译效果与效率方面均优于 UniTrans,展现出更优的错误纠正能力。
- 在 LLaMA-7B 上,TransAGENT 将 Java 到 Python 翻译的准确率从 31.25% 提升至 31.90%,实现了 0.65% 的绝对提升,表明其在小规模模型上同样有效。
- 消融实验确认,每个智能体均对最终性能有显著贡献,其中代码对齐器与语义错误修复器在定位与功能修正中发挥关键作用。
- 用户研究表明,TransAGENT 的代码映射准确率显著高于当前最先进的基于大语言模型的映射方法 TransMap。
- 该系统在不同大语言模型间表现出良好的泛化能力,表明其不仅兼容性强,且具备鲁棒性,不局限于单一模型架构。
- 通过控制流块映射实现的执行对齐,通过将搜索空间缩小至语义相关的代码段,显著降低了错误修复的难度。

阅读本论文
在 Nubint 阅读器中打开原文,边读边就任意段落提问。
在 Nubint 中阅读本论文本解读由 AI 根据论文原文生成,引用前请核对原文。