[论文解读] Lost in Translation: A Study of Bugs Introduced by Large Language Models while Translating Code
本研究调查了大型语言模型(LLMs)在五种编程语言间翻译代码的可靠性,发现 LLMs 仅在 2.1% 至 47.3% 的情况下生成正确翻译。研究识别出 15 种不同的翻译错误类别,对比了基于 LLM 与传统非 LLM 技术的方法,并提出一种上下文感知的提示工程策略,使翻译准确率平均提升 5.5%。
Code translation aims to convert source code from one programming language (PL) to another. Given the promising abilities of large language models (LLMs) in code synthesis, researchers are exploring their potential to automate code translation. The prerequisite for advancing the state of LLM-based code translation is to understand their promises and limitations over existing techniques. To that end, we present a large-scale empirical study to investigate the ability of general LLMs and code LLMs for code translation across pairs of different languages, including C, C++, Go, Java, and Python. Our study, which involves the translation of 1,700 code samples from three benchmarks and two real-world projects, reveals that LLMs are yet to be reliably used to automate code translation -- with correct translations ranging from 2.1% to 47.3% for the studied LLMs. Further manual investigation of unsuccessful translations identifies 15 categories of translation bugs. We also compare LLM-based code translation with traditional non-LLM-based approaches. Our analysis shows that these two classes of techniques have their own strengths and weaknesses. Finally, insights from our study suggest that providing more context to LLMs during translation can help them produce better results. To that end, we propose a prompt-crafting approach based on the symptoms of erroneous translations; this improves the performance of LLM-based code translation by 5.5% on average. Our study is the first of its kind, in terms of scale and breadth, that provides insights into the current limitations of LLMs in code translation and opportunities for improving them. Our dataset -- consisting of 1,700 code samples in five PLs with 10K+ tests, 43K+ translated code, 1,748 manually labeled bugs, and 1,365 bug-fix pairs -- can help drive research in this area.
研究动机与目标
- 评估通用型和代码专用型 LLM 在五种编程语言(C、C++、Go、Java、Python)间翻译代码的有效性。
- 识别并分类 LLM 在源代码翻译中引入的翻译失败的根本原因。
- 从正确性和错误特征两个方面,对比基于 LLM 的代码翻译与非 LLM 方法的优劣。
- 探究在迭代提示工程中提供翻译错误的上下文反馈,是否能够提升 LLM 的性能。
- 发布一个全面的数据集,包含 1,700 个代码样本、10,000 多个测试用例、43,000 多个翻译、1,748 个标记的错误以及 1,365 对错误修复样本,以支持未来基于 LLM 的代码翻译研究。
提出的方法
- 从三个基准和两个真实项目中收集了 1,700 个可执行代码样本,覆盖五种编程语言。
- 使用最先进的通用型和代码专用型 LLM 将每个代码样本翻译成多种目标语言。
- 使用数据集中提供的自动化测试套件评估翻译正确性,并对边缘情况进行了人工验证。
- 对 1,748 次失败的翻译进行人工分析,识别并标记了 15 种不同的翻译错误类别。
- 设计并评估了一种提示工程技术,该技术利用先前错误的症状,为 LLM 在重新翻译过程中提供上下文反馈。
- 对比基于 LLM 的翻译与非 LLM 方法(如转换器)在翻译质量与错误特征方面的相对优劣。

实验结果
研究问题
- RQ1RQ1.1:最先进的通用型和代码专用型 LLM 在多种编程语言间的代码翻译中表现如何?
- RQ2RQ1.2:翻译失败的结果是什么?它们表现出哪些类型的错误?
- RQ3RQ2.1:基于 LLM 的代码翻译失败的根本原因(即翻译错误)是什么?
- RQ4RQ2.2:这些错误类别在失败翻译中有多普遍?
- RQ5RQ2.3:真实项目中的翻译错误与合成基准中的错误有何不同?
- RQ6RQ3:非 LLM 方法在正确性和错误特征方面与基于 LLM 的方法相比如何?
- RQ7RQ4:基于错误症状的迭代提示工程在多大程度上能够提升基于 LLM 的代码翻译性能?
主要发现
- 在所研究的模型和语言对中,基于 LLM 的代码翻译仅在 2.1% 至 47.3% 的测试用例中实现了功能正确性。
- 共识别出 15 种不同的翻译错误类别,包括语义误解、类型处理错误以及控制流结构错误等。
- 真实项目中的错误模式比合成基准更复杂、更多样,表明真实代码对 LLM 构成了更大的挑战。
- 非 LLM 方法(如转换器)在正确性方面优于 LLM,并且在复杂代码结构中引入的语义错误更少。
- 所提出的提示工程技术——利用先前错误的症状指导重新翻译——使平均翻译准确率提升了 5.5%。
- 本研究发布的数据集包含 1,700 个代码样本、10,000 多个测试用例、43,000 多个翻译、1,748 个标记的错误以及 1,365 对错误修复样本,可公开获取,以支持未来基于 LLM 的代码翻译研究。

更好的研究,从现在开始
从阅读论文到最终审阅,大幅缩短您的研究时间。
无需绑定信用卡
本解读由 AI 生成,并经人工编辑审核。