Table of Contents
Fetching ...

InterTrans: Leveraging Transitive Intermediate Translations to Enhance LLM-based Code Translation

Marcos Macedo, Yuan Tian, Pengyu Nie, Filipe R. Cogo, Bram Adams

TL;DR

INTERTRANS is introduced, an LLM-based automated code translation approach that, in contrast to existing approaches, leverages intermediate translations to bridge the syntactic and semantic gaps between source and target PLs.

Abstract

Code translation aims to convert a program from one programming language (PL) to another. This long-standing software engineering task is crucial for modernizing legacy systems, ensuring cross-platform compatibility, enhancing performance, and more. However, automating this process remains challenging due to many syntactic and semantic differences between PLs. Recent studies show that even advanced techniques such as large language models (LLMs), especially open-source LLMs, still struggle with the task. Currently, code LLMs are trained with source code from multiple programming languages, thus presenting multilingual capabilities. In this paper, we investigate whether such multilingual capabilities can be harnessed to enhance code translation. To achieve this goal, we introduce InterTrans, an LLM-based automated code translation approach that, in contrast to existing approaches, leverages intermediate translations across PLs to bridge the syntactic and semantic gaps between source and target PLs. InterTrans contains two stages. It first utilizes a novel Tree of Code Translation (ToCT) algorithm to plan transitive intermediate translation sequences between a given source and target PL, then validates them in a specific order. We evaluate InterTrans with three open LLMs on three benchmarks (i.e., CodeNet, HumanEval-X, and TransCoder) involving six PLs. Results show an absolute improvement between 18.3% to 43.3% in Computation Accuracy (CA) for InterTrans over Direct Translation with 10 attempts. The best-performing variant of InterTrans (with Magicoder LLM) achieved an average CA of 87.3%-95.4% on three benchmarks.

InterTrans: Leveraging Transitive Intermediate Translations to Enhance LLM-based Code Translation

TL;DR

INTERTRANS is introduced, an LLM-based automated code translation approach that, in contrast to existing approaches, leverages intermediate translations to bridge the syntactic and semantic gaps between source and target PLs.

Abstract

Code translation aims to convert a program from one programming language (PL) to another. This long-standing software engineering task is crucial for modernizing legacy systems, ensuring cross-platform compatibility, enhancing performance, and more. However, automating this process remains challenging due to many syntactic and semantic differences between PLs. Recent studies show that even advanced techniques such as large language models (LLMs), especially open-source LLMs, still struggle with the task. Currently, code LLMs are trained with source code from multiple programming languages, thus presenting multilingual capabilities. In this paper, we investigate whether such multilingual capabilities can be harnessed to enhance code translation. To achieve this goal, we introduce InterTrans, an LLM-based automated code translation approach that, in contrast to existing approaches, leverages intermediate translations across PLs to bridge the syntactic and semantic gaps between source and target PLs. InterTrans contains two stages. It first utilizes a novel Tree of Code Translation (ToCT) algorithm to plan transitive intermediate translation sequences between a given source and target PL, then validates them in a specific order. We evaluate InterTrans with three open LLMs on three benchmarks (i.e., CodeNet, HumanEval-X, and TransCoder) involving six PLs. Results show an absolute improvement between 18.3% to 43.3% in Computation Accuracy (CA) for InterTrans over Direct Translation with 10 attempts. The best-performing variant of InterTrans (with Magicoder LLM) achieved an average CA of 87.3%-95.4% on three benchmarks.

Paper Structure

This paper contains 22 sections, 5 figures, 2 tables, 2 algorithms.

Figures (5)

  • Figure 1: Running example of InterTrans with maxDepth=3 for translating Python to Java, showing a successful translation through C++ after exploring various translation paths. Red nodes represent unsuccessful translations, blue nodes indicate explored translations, green nodes denote successful translations, and grey nodes are skipped translations. The number along with each edge is the execution order of the translations.
  • Figure 2: Performance of InterTrans with varying $maxDepth$ on three datasets.
  • Figure 3: HeatMap showing the mean absolute decrease in CA (%) when removing a programming language from the intermediates used in our approach, compared to not removing any PL (across all datasets and models). Framed cells annotated with "(sig.)" indicate statistically significant results. The "n" value in the x-axis labels indicates the sample size for each translation pair. For each translation pair, one cell is empty because (by definition) the target PL can not be removed.
  • Figure 4: Average performance of InterTrans with varying number of intermediate PLs on three datasets.
  • Figure 5: Example of translation from Python to Java via Rust.