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

CoTran: An LLM-Based Code Translator Using Reinforcement Learning with Feedback from Compiler and Symbolic Execution

Prithwish Jana, Piyush Jha, Haoyang Ju, Gautham Kishore, Aryan Mahajan, Vijay Ganesh

TL;DR

CoTran introduces a novel framework for whole-program code translation by fine-tuning Large Language Models (LLMs) using reinforcement learning with fine-grained feedback from compilers and symbolic execution tools. By shifting from standard text-matching losses to execution-aware rewards, it significantly boosts both compilation rates and functional correctness in Java-to-Python and Python-to-Java translations.

What Problem Does This Paper Address?

Translating code from one high-level language to another (transpilation) is a vital task for legacy software migration and cross-platform interoperability. Traditional rule-based transpilers are incredibly expensive to build and maintain. While modern LLMs show great promise in generating code, they struggle with whole-program translation.

The root of the problem lies in how they are trained: standard supervised fine-tuning uses cross-entropy loss, which treats code like natural language text. This approach rewards token-matching rather than syntactic validity or functional correctness. As a result, translated code frequently fails to compile or, worse, compiles but produces incorrect behavior. Existing attempts to fix this use simple binary (yes/no) compiler feedback, which doesn't provide a rich enough signal for an LLM to learn how to fix its mistakes.

Methods

CoTran addresses these shortcomings by introducing a parameter-efficient reinforcement learning (RL) fine-tuning pipeline that leverages two distinct types of symbolic feedback:

  1. Fine-Grained Compiler Feedback (CF): Instead of a binary pass/fail, CoTran uses a continuous reward based on the position of the first compilation error. If an error occurs near the end of the file, the model receives a higher reward than if it occurs at the beginning, signaling that it is close to a perfect compilation. It also includes a length-penalization factor to prevent the model from "gaming" the system by outputting trivial, error-free code (like a "Hello World" script).
  2. Symbolic Execution Feedback (SF): To ensure the translated code actually works as intended, the authors use a back-to-back (b2b) translation loop ($Source \rightarrow Target \rightarrow Source$). They employ an industrial symbolic execution tool (Symflower) on the source code to automatically generate a comprehensive suite of unit tests. The backward model translates the target code back to the source language, and the generated unit tests are executed on this reconstructed code to calculate how many test cases pass.

The training loop alternates between RL steps (using Proximal Policy Optimization, or PPO) to maximize these feedback rewards and standard Supervised Fine-Tuning (SFT) steps to ensure the model doesn't lose its foundational language capabilities. Low-Rank Adaptation (LoRA) is used to keep the training process space- and time-efficient.

CoTran Training Architecture

Additionally, the authors introduce a Keyword Tokenizer (kw-Tok) that prevents programming-specific keywords (like volatile in Java or elif in Python) from being split into meaningless sub-tokens, preserving code structure right from the tokenization phase.

Datasets

The authors introduce AVATAR-TC, a large-scale, well-curated dataset consisting of over 57,000 Java-Python code pairs spanning 9,198 distinct problems. Built on top of the existing AVATAR dataset, AVATAR-TC cleans up the original data by manually fixing faulty code pairs and, crucially, appends human-written test cases collected from competitive programming platforms (such as AtCoder, Codeforces, and LeetCode). These human-written test cases are used exclusively for evaluating the final translation quality.

Key Insights and Ideas

  • Fine-grained rewards accelerate learning: Continuous reward functions (like tracking the exact token index of a syntax error) give the RL agent a clear gradient of improvement, making it vastly superior to binary feedback loops.
  • The power of the "Round-Trip": Checking functional equivalence across two entirely different languages is incredibly difficult. By translating code back to its original language ($S \rightarrow T \rightarrow S$), the framework can leverage mature, single-language testing ecosystems (like JUnit for Java) to verify semantic correctness.
  • Static to dynamic is easier than vice-versa: The authors noted that Python-to-Java translation is significantly harder than Java-to-Python. In Python-to-Java, the model must implicitly deduce and declare variable types that do not exist in the source Python code—a challenge even for experienced human programmers.

Key Results

CoTran (built on top of the 220-million-parameter CodeT5-base) was evaluated against 14 competing tools, including traditional transpilers, unsupervised models, and ChatGPT (GPT-3.5).

  • Python-to-Java (P2J): CoTran achieved 48.68% Functional Equivalence Accuracy (FEqAcc) and 76.98% Compilation Accuracy (CompAcc), outperforming the nearest open-source competitor (PLBART-base) by over 10% in functional correctness.
  • Beating ChatGPT: Remarkably, CoTran outperformed ChatGPT on all metrics in Python-to-Java translation, and beat it on all metrics except one (FEqAcc) in Java-to-Python translation, demonstrating that targeted symbolic feedback can allow much smaller models to punch far above their weight class.

Limitations and Future Work

  • Undecidability of Equivalence: As a fundamental computer science constraint, proving absolute program equivalence is undecidable. CoTran relies on testing, which can confidently prove inequivalence but can only infer equivalence based on the coverage of the test suite.
  • Language Constraints: The current implementation of CoTran is strictly evaluated on Java and Python, though the authors note it can be adapted to any language pair that possesses a compiler and an automated test-generation tool.
  • Future Directions: The authors plan to scale CoTran to larger base models and focus on the highly practical software engineering challenge of translating legacy enterprise codebases into modern programming languages.

Summary updated

BibTeX
@inproceedings{jana2024cotran,
  author = {Prithwish Jana and Piyush Jha and Haoyang Ju and Gautham Kishore and Aryan Mahajan and Vijay Ganesh},
  title = {{CoTran: An LLM-Based Code Translator Using Reinforcement Learning with Feedback from Compiler and Symbolic Execution}},
  booktitle = {ECAI},
  year = {2024},
  doi = {10.3233/faia240968},
}
Figure