NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Exploring and Unleashing the Power of Large Language Models in Automated Code Translation

Zhen Yang, Fang Liu, Zhongxing Yu, Jacky Wai Keung, Jia Li, Shuo Liu, Yifan Hong, Xiaoxue Ma, Zhi Jin, Ge Li

Abstract

Automated code translation, or transpilation, is a critical task for modernizing legacy software systems. While Large Language Models (LLMs) have demonstrated remarkable capabilities in code generation, their performance in translating logic between programming languages remains inconsistent. This paper explores the limitations of zero-shot LLM translation and introduces UniTrans, a framework designed to improve translation accuracy through automated test-case generation and iterative repair. By leveraging test cases as functional specifications, UniTrans significantly boosts the computational accuracy of both proprietary and open-source models.

Introduction

The migration of legacy codebases to modern programming languages is a perennial challenge in software engineering. Traditional rule-based transpilers are difficult to build and maintain, requiring deep expertise in both source and target language grammars. While early machine-learning approaches reduced the need for manual rules, they relied heavily on massive, high-quality parallel datasets which are often unavailable for niche or older languages.

Large Language Models (LLMs) offer a promising alternative by performing "zero-shot" translation. However, recent evaluations show that even state-of-the-art models like GPT-3.5 frequently produce code that is syntactically correct but logically flawed. This research identifies the root causes of these failures and proposes a framework to "unleash" the latent reasoning capabilities of LLMs to bridge the gap between source and target languages.

The Problem: Why LLMs Fail at Translation

Through a detailed error analysis, the authors identified three primary categories of failure in LLM-based code translation:

  1. Logical Comprehension (38.51% of failures): The model fails to grasp the underlying algorithmic logic of the source program, leading to translations that do not produce the correct output for a given input.
  2. Type Mismatches: When moving from dynamically typed languages (e.g., Python) to statically typed languages (e.g., C++ or Java), LLMs often struggle to infer the correct data types for variables and function signatures without explicit guidance.
  3. Language Discrepancies (41.38% of failures): Models often overlook subtle semantic differences between languages. For example, the behavior of integer division or specific library API calls can vary significantly, leading to "silent" bugs that are difficult to detect without execution.

The UniTrans Framework

To address these issues, the authors developed UniTrans, a three-phase framework that shifts the focus from syntax-to-syntax mapping to functional equivalence.

1. Test Case Generation

Instead of proceeding directly to translation, UniTrans first prompts the LLM to generate a diverse set of inputs for the source program. The source program is then executed with these inputs to capture the "ground truth" outputs. This process creates a suite of test cases that define the expected behavior of the code.

2. Translation Augmentation

In the second phase, the LLM is provided with the source code, the generated test cases, and explicit information regarding input/output types. This transforms the task from a blind translation into a guided implementation based on a functional specification. The test cases act as a bridge, helping the model focus on maintaining logical parity across the language divide.

3. Iterative Translation Repair

If the initial translation fails to pass the generated test cases, UniTrans enters an automated repair loop. An "Error Analyzer" captures the specific failure—whether it is a compilation error, a runtime crash, or a logical mismatch—and feeds this feedback back to the LLM. The model then uses this diagnostic information to iteratively refine and fix the code.

Dataset Refinement

Evaluation was conducted using a popular benchmark of parallel functions in Python, Java, and C++. A significant contribution of this work is the rigorous manual cleaning of the existing benchmark. The authors identified 252 errors in the original dataset, including broken unit tests and logic inconsistencies. By releasing this cleaned version, the study provides a more reliable foundation for future research in the field.

Key Results and Insights

UniTrans demonstrated substantial improvements across a variety of models, particularly for smaller, open-source models that typically struggle with complex translation tasks:

  • GPT-3.5: Achieved an average 87.92% Computational Accuracy (CA). UniTrans improved its performance by 4.02% in CA and 13.28% in Exact Match (EM) accuracy.
  • LLaMA-13B: Showed a significant boost, with a 19.20% improvement in CA and a 36.42% improvement in EM.
  • LLaMA-7B: Experienced the most dramatic gains, with a 28.58% improvement in CA and a 71.22% improvement in EM.

The Power of Feedback

The research highlights that larger models like GPT-3.5 are highly efficient at the repair phase, often resolving bugs in just one or two iterations. While smaller models require more rounds of feedback, the relative gain in their performance suggests that the "self-testing" mechanism effectively compensates for lower initial reasoning capacity.

Limitations and Future Work

Despite its success, UniTrans faces challenges with "Precision" errors—subtle differences in floating-point mathematics or rounding behaviors between languages that are difficult for LLMs to perceive even with error messages. Furthermore, the current study focuses on function-level translation. Real-world software migration often involves project-level complexities, including cross-file dependencies and external library integrations, which remain an open area for future exploration.

Conclusion

UniTrans demonstrates that the key to successful code translation lies not just in better models, but in better frameworks. By treating tests as specifications and implementing an automated feedback loop, we can significantly improve the reliability of LLMs in software engineering tasks, moving closer to fully automated, high-fidelity code migration.

Summary updated

BibTeX
@article{yang2024exploring,
  author = {Zhen Yang and Fang Liu and Zhongxing Yu and Jacky Wai Keung and Jia Li and Shuo Liu and Yifan Hong and Xiaoxue Ma and Zhi Jin and Ge Li},
  title = {{Exploring and Unleashing the Power of Large Language Models in Automated Code Translation}},
  journal = {Proc. ACM Softw. Eng.},
  year = {2024},
  doi = {10.1145/3660778},
}
Figure