Abstract
Large Language Models (LLMs) excel at code generation but often struggle with underrepresented programming languages due to a lack of high-quality training data and relevant few-shot examples. We present PERC (Plan-as-query Example Retrieval for Code generation), a framework that retrieves examples based on algorithmic logic rather than surface-level syntax. By converting queries and example pools into pseudocode "plans," PERC bridges the "modality gap" between high-resource and low-resource languages, significantly boosting performance in niche coding tasks.
Introduction: The Challenge of Underrepresented Languages
While LLMs are proficient in high-resource languages like Python, their performance degrades significantly for niche languages such as Julia, Lua, or R. Retrieval-Augmented Generation (RAG) typically mitigates this by providing few-shot examples. However, standard RAG relies on natural language or raw code similarity. In low-resource settings, the retrieval pool for the target language is often empty or insufficient. Retrieving from a high-resource language like Python often fails because the syntactic differences—the "modality gap"—obscure the underlying algorithmic similarities, leading to the selection of irrelevant examples.
Methodology: The PERC Framework
PERC shifts the retrieval focus from syntax to semantics through a two-stage process:
1. Plan-As-Query Retrieval
Instead of querying with raw problem descriptions, PERC employs an LLM to generate a high-level algorithmic plan in pseudocode. Simultaneously, the example pool (e.g., Python solutions) is pre-processed into similar pseudocode plans. Retrieval is then performed by matching the query plan against the example plans, effectively filtering out syntactic noise.
2. Code Generation with Few-shot Prompting
After identifying a logically relevant example from a high-resource language, PERC translates it into the target underrepresented language. The final prompt includes the target problem, the retrieved plan, and the translated code, providing a clear reasoning chain for the LLM.
Pseudocode as a Universal Intermediate
The core innovation of PERC is the use of pseudocode as a universal intermediate representation. Pseudocode is detailed enough to capture complex logic but abstract enough to ignore language-specific quirks. This allows the system to "borrow" logic from Python datasets to solve problems in languages like Lua or R by projecting both into a shared "plan space."
Key Results and Performance
PERC was evaluated across benchmarks like MultiPL-E and CodeContests using models such as GPT-4o-mini and Llama-3.1-8B. The framework consistently outperformed state-of-the-art RAG methods like CEDAR and RepoCoder.
- Significant Performance Gains: On the MultiPL-E benchmark for the language R, PERC improved the Pass@1 score from 14.78% (zero-shot) to 34.35%.
- Competitive Programming: In the challenging CodeContests dataset, PERC achieved a Pass@1 of 8.48%, outperforming existing retrieval methods.
- Cross-Language Transfer: The framework proved robust even when the source examples were in C++ or Java, demonstrating that the logic-first approach is language-agnostic.
Conclusion
PERC demonstrates that the "modality gap" in code retrieval can be overcome by abstracting code into algorithmic plans. By leveraging high-resource language data through pseudocode bridging, PERC enables high-quality code generation for underrepresented languages without requiring massive specialized datasets. Future work will focus on improving retrieval monotonicity and cost-efficiency for large-scale deployments.
Summary updated