Post-Incorporating Code Structural Knowledge into LLMs via In-Context Learning for Code Translation

Yali Du, Hui Sun, Ming Li · DOI

Abstract

Large Language Models (LLMs) have shown remarkable capabilities in code generation but often struggle with the rigid syntactic requirements of programming languages during translation tasks. Retraining these models to incorporate structural knowledge is computationally expensive and often impossible for proprietary systems. This paper presents CAST (Coverage of Abstract Syntax Tree), a training-free, model-agnostic approach that leverages In-Context Learning (ICL) to inject structural programming knowledge. By selecting exemplars that maximize the coverage of a query's abstract syntax tree subtrees, CAST significantly improves translation accuracy across various model architectures.

Introduction

The translation of source code between programming languages (e.g., C++ to Python) requires more than just semantic mapping; it demands strict adherence to nested logic, type systems, and control flow structures. While LLMs excel at natural language, they frequently produce code with subtle syntactic errors. Existing solutions often involve architectural modifications or specialized training, which do not scale well with the rapid release of new, massive models. This work explores how to optimize the selection of few-shot exemplars to provide the necessary structural "hints" to an off-the-shelf LLM.

Methodology: Information Coverage and CAST

The core hypothesis is that standard similarity-based exemplar selection (e.g., BM25) leads to redundant information. Instead, the authors propose maximizing Information Coverage (IC). As illustrated in Figure 1, IC aims to select a set of exemplars that collectively cover the diverse structural components of the test code, rather than just finding the single most similar snippet.

Conceptual framework of Information Coverage and its application to AST subtrees.

To implement this, the CAST metric is introduced:

  1. AST Extraction: Code is parsed into Abstract Syntax Trees (ASTs), focusing on node types to capture syntax while ignoring variable names.
  2. Fingerprinting: Subtrees are hashed into unique identifiers.
  3. Submodular Maximization: A greedy algorithm is employed to select exemplars. Because the coverage function is submodular, this greedy approach efficiently finds a near-optimal set of examples that provide the broadest structural coverage.

System Architecture

The CAST retrieval process is integrated into a standard ICL pipeline. As shown in Figure 2, the system first pre-recalls a set of candidate source codes from a database. These candidates, along with the query code, are parsed into AST subtrees. The CAST Maximization module then selects the $k$ most informative exemplars, which are formatted into a prompt template for the LLM.

The end-to-end CAST retrieval and In-Context Learning pipeline.

Experimental Results

The method was evaluated using benchmarks like the Parallel Function Dataset and AVATAR across several model families, including LLaMA, DeepSeek, and GPT-4o.

  • Performance Boost: CAST-F (Fixed) consistently outperformed baselines. For instance, it improved Gemma2-27B's translation accuracy by 1.9% over similarity-based methods.
  • Scaling Insights: Interestingly, the need for explicit structural injection persists even as models scale to 70B+ parameters, suggesting that structural understanding does not purely emerge from size.
  • Efficiency: The adaptive variant, CAST-A, reduces token usage by dynamically stopping exemplar selection once a sufficient coverage threshold is reached.

Conclusion

CAST provides a robust, model-agnostic framework for enhancing LLM performance on code-related tasks without retraining. By shifting the focus from similarity to structural coverage, it ensures that the model receives a diverse set of logic templates, effectively reducing common syntactic failures in code translation. Future work may explore more complex representations like Control Flow Graphs to further enrich the structural context provided to the models.

Summary updated

BibTeX
@article{du2025post,
  author = {Yali Du and Hui Sun and Ming Li},
  title = {{Post-Incorporating Code Structural Knowledge into LLMs via In-Context Learning for Code Translation}},
  journal = {IEEE Trans. Software Eng.},
  year = {2025},
  doi = {10.1109/tse.2025.3605768},
}
Figure