arXivDaily arXiv每日学术速递 周一至周五更新
感恩节,周五至周末无更新,大家顺遂。周一再来吧
arXiv 2602.16106cs.SE
收藏

基于算法的可靠且意图保留的代码翻译管道

Algorithm-Based Pipeline for Reliable and Intent-Preserving Code Translation with LLMs

University of Saskatchewan(萨斯喀彻温大学)

Shahriar Rumi Dipto, Saikat Mondal, Chanchal K. Roy

AI总结:

本文提出一种基于算法的代码翻译管道,通过引入语言中立的中间规范提升翻译准确性和可靠性,实验结果显示其在多个指标上均优于直接翻译方法。

摘要:

代码翻译,即自动将程序在不同语言之间转换,是大型语言模型(LLMs)日益增长的应用场景。然而,直接的一次性翻译往往无法保留程序意图,导致控制流、类型处理和I/O行为的错误。我们提出了一种基于算法的管道,引入一种语言中立的中间规范,以在代码生成之前捕获这些细节。本研究经验性地评估了结构化规划在提高翻译准确性和可靠性方面相对于直接翻译的程度。我们进行了一种自动化配对实验——直接和基于算法的翻译,使用五个广泛使用的LLMs在Avatar和CodeNet数据集上进行Python和Java之间的翻译。对于每种组合(模型、数据集、方法和方向),我们编译并执行翻译的程序并运行提供的测试。我们记录编译结果、运行行为、超时(例如无限循环)和测试结果。我们从这些测试中计算准确性,只有在编译、运行无异常或超时且通过所有测试的情况下才将翻译视为正确。然后,我们将每个失败的编译时和运行时案例映射到一个统一的、语言感知的分类法,并比较直接和基于算法的方法之间的子类型频率。总体而言,基于算法的方法将微平均准确性从67.7%提高到78.5%(提高10.8%)。它消除了100%的词法和标记错误,减少了不完整结构的72.7%和结构和声明问题的61.1%。它还显著降低了运行时依赖和入口点失败的78.4%。这些结果表明,基于算法的管道能够实现更可靠、意图保留的代码翻译,为稳健的多语言编程助手提供了基础。

英文摘要:

Code translation, the automatic conversion of programs between languages, is a growing use case for Large Language Models (LLMs). However, direct one-shot translation often fails to preserve program intent, leading to errors in control flow, type handling, and I/O behavior. We propose an algorithm-based pipeline that introduces a language-neutral intermediate specification to capture these details before code generation. This study empirically evaluates the extent to which structured planning can improve translation accuracy and reliability relative to direct translation. We conduct an automated paired experiment - direct and algorithm-based to translate between Python and Java using five widely used LLMs on the Avatar and CodeNet datasets. For each combination (model, dataset, approach, and direction), we compile and execute the translated program and run the tests provided. We record compilation results, runtime behavior, timeouts (e.g., infinite loop), and test outcomes. We compute accuracy from these tests, counting a translation as correct only if it compiles, runs without exceptions or timeouts, and passes all tests. We then map every failed compile-time and runtime case to a unified, language-aware taxonomy and compare subtype frequencies between the direct and algorithm-based approaches. Overall, the Algorithm-based approach increases micro-average accuracy from 67.7% to 78.5% (10.8% increase). It eliminates lexical and token errors by 100%, reduces incomplete constructs by 72.7%, and structural and declaration issues by 61.1%. It also substantially lowers runtime dependency and entry-point failures by 78.4%. These results demonstrate that algorithm-based pipelines enable more reliable, intent-preserving code translation, providing a foundation for robust multilingual programming assistants.

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

↑