通过模拟语义翻译能否帮助LLMs进行代码翻译?基于伪代码的研究
Can Emulating Semantic Translation Help LLMs with Code Translation? A Study Based on Pseudocode
AI总结:
本研究通过基于伪代码的翻译方法提升LLMs在代码翻译中的表现,发现其在复杂程序处理中具有优势,但受限于伪代码的准确性。
摘要:
尽管大型语言模型(LLMs)在代码翻译中展现出巨大的潜力,但它们仍然在使用通常采用的直接代码到代码翻译方法时面临困难,该方法将原始程序在一步内转换为目标编程语言(PL)。受在解决挑战性任务中引入中间步骤的成功启发,本研究探讨了基于伪代码的代码翻译方法。这种方法通过首先将原始程序的意图和逻辑转换为伪代码,然后在目标PL中实现,以模拟人类的语义翻译。为了了解这种较少研究的方法的有效性,我们对基于伪代码的代码翻译进行了系统性的实证研究,旨在调查其在增强直接翻译方法中的帮助性,阐明其有效使用方法,并识别其限制。通过在六个PLs和五个流行的LLMs上比较直接和基于伪代码的翻译,共9690个翻译任务,我们发现基于伪代码的翻译可以有效补充直接翻译,特别是在从灵活到刚性的PL翻译以及处理低训练资源PL时。基于这些发现,我们建议将两种方法的翻译结果结合用于基于测试的选择,以利用它们的互补优势。我们还揭示了基于伪代码的翻译在复杂程序中解耦代码理解和生成负担以及减轻原始程序中PL特定实现的干扰的优势,以及由于伪代码不正确、不完整或模糊而导致的限制。我们的研究为基于伪代码的翻译的有效使用提供了启示,并提供了证据以帮助提高LLMs在代码翻译中的能力。
英文摘要:
Although large language models (LLMs) show promising potential in code translation, they still struggle to generate accurate translations using the commonly adopted direct code-to-code translation approach, which converts an original program into the target programming language (PL) in a single step. Inspired by the success of incorporating intermediate steps to guide LLMs in resolving challenging tasks, in this study, we explore pseudocode-based code translation. This approach emulates human semantic translation by first interpreting the original program's intent and logic into pseudocode and then implementing it in the target PL. To understand the effectiveness of this underexplored approach, we present a systematic empirical study on pseudocode-based code translation, aiming to investigate its helpfulness in enhancing the direct translation approach, illuminate its effective usage, and identify its limitations. By comparing direct and pseudocode-based translation on 9,690 translation tasks across six PLs with five popular LLMs, we found that pseudocode-based translation can effectively complement direct translation, particularly when translating from flexible to rigid PLs and handling a low-training-resource PL. Based on the findings, we suggest combining the translation results of both approaches for test-based selection to leverage their complementary strengths. We also reveal the advantages of pseudocode-based translation in decoupling the code understanding and generation burden on complicated programs and mitigating distractions from PL-specific implementations in original programs, as well as its limitations due to incorrect, incomplete, or ambiguous pseudocode. Our study sheds light on the effective use of pseudocode-based translation and provides evidence to help enhance LLMs in code translation.
机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。