🤖 AI Summary
Addressing the challenge of balancing semantic correctness and code readability in code translation, this paper proposes F2STrans—a novel paradigm that decouples functional learning (ensuring semantic equivalence) from style guidance (enhancing readability and consistency). Methodologically, it employs supervised learning to guarantee functional correctness and introduces contrastive learning with positive/negative style exemplars, augmented by a progressive prompting strategy to refine outputs. Contributions include: (1) the first fine-grained benchmark covering multiple programming languages, featuring high-quality human annotations, comprehensive unit tests, and state-of-the-art code; and (2) superior performance of the lightweight Qwen-1.5B model across 20 cross-language translation tasks—outperforming prompt-enhanced Qwen-32B and GPT-4 on average—demonstrating that structured learning enables compact models to achieve higher practical efficacy.
📝 Abstract
Large language models (LLMs) have made significant strides in code translation tasks. However, ensuring both the correctness and readability of translated code remains a challenge, limiting their effective adoption in real-world software development. In this work, we propose F2STrans, a function-to-style guiding paradigm designed to progressively improve the performance of LLMs in code translation. Our approach comprises two key stages: (1) Functional learning, which optimizes translation correctness using high-quality source-target code pairs mined from online programming platforms, and (2) Style learning, which improves translation readability by incorporating both positive and negative style examples. Additionally, we introduce a novel code translation benchmark that includes up-to-date source code, extensive test cases, and manually annotated ground-truth translations, enabling comprehensive functional and stylistic evaluations. Experiments on both our new benchmark and existing datasets demonstrate that our approach significantly improves code translation performance. Notably, our approach enables Qwen-1.5B to outperform prompt-enhanced Qwen-32B and GPT-4 on average across 20 diverse code translation scenarios.