Can Emulating Semantic Translation Help LLMs with Code Translation? A Study Based on Pseudocode

Songqiang Chen, Congying Xu, Jingyi Chen, Jialun Cao, Jiarong Wu, Shing-Chi CheungarXiv 2025 · DOI

TL;DR

Direct code-to-code translation often trips up large language models (LLMs) because they try to handle semantic understanding and target-language generation simultaneously. By introducing an intermediate pseudocode step—emulating human semantic translation—LLMs can decouple these burdens, leading to significant accuracy gains. When combined with direct translation in a hybrid approach, this strategy proves exceptionally powerful, particularly when translating from flexible languages like Python to rigid ones like Rust.

What Problem Does This Paper Address?

Automated code translation (or transpilation) is critical for migrating legacy software and building cross-platform applications. While LLMs have emerged as powerful tools for this task, standard practice relies on "direct translation"—converting code from language A to language B in a single step.

The catch? Direct translation forces an LLM to implicitly perform two complex tasks at once: deciphering the core logic of the source program and mapping it to the unique syntax, type systems, and APIs of the target language. This often leads to subtle bugs, API hallucinations, or structural mismatches, especially when translating between languages with vastly different paradigms (e.g., moving from a dynamically typed language to a strict, memory-safe one).

Methods

Inspired by how human translators first grasp the meaning of a sentence before rendering it in another language, the authors investigate pseudocode-based code translation. They systematically evaluate five prompting strategies across five popular LLMs (including Qwen2.5-Coder, Phi-4, and GPT-4o-mini):

  1. Direct Translation ($\mathcal{D}$): The standard one-step baseline.
  2. Transitive Translation via Pseudocode ($\mathcal{P}$): A two-step process where the LLM first generates a language-agnostic description of the logic (pseudocode) and then implements the target code from it.
  3. Transitive Translation via Pseudocode with Original Program as Context ($\mathcal{PC}$): The same two-step process, but the original source code is provided alongside the pseudocode as a reference during final code generation.
  4. Hybrid Strategies ($\mathcal{D}\&\mathcal{P}$ and $\mathcal{D}\&\mathcal{PC}$): Combining attempts from both direct and pseudocode methods to leverage their complementary strengths via test-based selection.

Additionally, the authors test the upper bounds of this approach by replacing the LLMs' self-generated pseudocode with high-quality pseudocode generated by DeepSeek-R1.

Datasets

The authors evaluated these strategies on 9,690 translation tasks derived from 323 LeetCode problems hosted on LiveCodeBench-v5.

  • Languages covered: C++, Go, Java, JavaScript, Python, and Rust (6 source $\times$ 5 target languages).
  • Contamination mitigation: The problems chosen were released after May 2023, ensuring they post-date the training cutoffs of the studied models.
  • Complexity: Tasks span three difficulty levels (Easy, Medium, Hard). As shown in the paper's structural analysis, harder problems exponentially increase the complexity of the abstract syntax trees (ASTs) and API calls that the models must navigate.
Table 1. AST Node Counts and API Invocation Counts of Programs for Different Difficulty-Level Problems

Key Insights and Ideas

  • Decoupling Cognitive Load: For dense, compact code (like Python one-liners or complex recursive chains), direct translation often misses steps. Pseudocode breaks the problem into two cleaner sub-problems: abstract understanding and concrete implementation.
  • The "Flexible-to-Rigid" Boost: The pseudocode approach shines brightest when translating from flexible, dynamically typed languages (Python, JavaScript) to stricter, statically typed languages (Rust, Go). Pseudocode strips away source-specific syntax sugar that might otherwise distract the model.
  • A Lifeline for Low-Resource Languages: Parallel code data for newer or less common languages like Rust is scarce in LLM training sets. However, natural-language-to-Rust data (documentation, tutorials) is abundant. Pseudocode effectively transforms a code-to-code task into a code-to-text-to-code task, tapping into this rich text resource.
  • Complementary Failure Modes: Pure pseudocode translation isn't a silver bullet—it can introduce its own errors, like omitting type precision. However, because its mistakes are entirely different from direct translation, a hybrid approach yields the best overall performance.

Key Results

  • Substantial Accuracy Gains: The hybrid strategy ($\mathcal{D}\&\mathcal{PC}$) consistently outperformed direct translation alone, improving the average pass@10 rate by 4.10% on easy tasks, 7.44% on medium tasks, and 13.76% on hard tasks.
  • Universal Intermediary: Pseudocode proved to be a more effective universal bridge across diverse language pairs than using a concrete intermediate programming language (e.g., translating Python to Java via Rust).
  • The Power of Quality Pseudocode: When models were fed high-quality pseudocode from DeepSeek-R1, performance skyrocketed, achieving pass rates up to 96–98% on easy tasks and 67–82% on hard tasks. This highlights that the generation of the pseudocode itself is currently the primary bottleneck.

Limitations and Future Work

The authors identify three main limitations of pseudocode-based translation:

  1. Error Propagation: If the LLM generates incorrect pseudocode, the final translation is almost certainly doomed (accounting for 66% of pseudocode-based failures).
  2. Information Loss: Crucial low-level details, such as floating-point precision requirements or specific algorithmic edge cases, are often omitted in concise pseudocode.
  3. Natural Language Ambiguity: Phrases like "from 0 to length of nums" introduce ambiguity regarding loop boundaries (inclusive vs. exclusive).

Future Directions: The study opens up exciting avenues, including the development of fine-grained pseudocode generation rules tailored specifically for translation, automated validation/repair loops to debug pseudocode before final code generation, and the integration of structured reasoning traces (like those in o1/R1) to prevent "overthinking" degradation.

Summary updated

BibTeX
@misc{chen2025can,
  author = {Songqiang Chen and Congying Xu and Jingyi Chen and Jialun Cao and Jiarong Wu and Shing-Chi Cheung},
  title = {{Can Emulating Semantic Translation Help LLMs with Code Translation? A Study Based on Pseudocode}},
  year = {2025},
  eprint = {2510.00920},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2510.00920},
}
Figure