arXiv:2410.03351cs.CLcs.PL2024-10被引 2

用自省机制让大模型生成代码等价表达,提升软件开发效率

Generating Equivalent Representations of Code By A Self-Reflection Approach

  • 两模型互检反思生成代码等价表示,无需预设约束
  • 在无约束下揭示大模型对语法、接口、计算的理解机制
  • 支持自然注释、伪代码等多形式生成,适配多种工程任务

代码的等价表示(ERs)是语义与原代码一致的文本表达,如自然语言注释和伪代码,在软件开发与维护中至关重要。然而,自动生成代码等价表示仍是开放挑战。本文提出一种自省方法,使两个大语言模型通过相互反思生成ER。该方法可在开放与受约束两种设置下生成ER:开放设置中允许模型自由表达,揭示大模型对代码语法结构、API调用及数值计算的理解;受约束设置中施加自然语言注释、伪代码、流程图等限制,验证其能有效生成符合要求的表达,支撑多样软件工程任务。实验共发现八项结论,为后续研究提供方向,包括推导代码生成的中间语言、探索适合大模型的需求描述等。本工作有望激发更多相关研究。

原文摘要 · Abstract (English)

Equivalent Representations (ERs) of code are textual representations that preserve the same semantics as the code itself, e.g., natural language comments and pseudocode. ERs play a critical role in software development and maintenance. However, how to automatically generate ERs of code remains an open challenge. In this paper, we propose a self-reflection approach to generating ERs of code. It enables two Large Language Models (LLMs) to work mutually and produce an ER through a reflection process. Depending on whether constraints on ERs are applied, our approach generates ERs in both open and constrained settings. We conduct a empirical study to generate ERs in two settings and obtain eight findings. (1) Generating ERs in the open setting. In the open setting, we allow LLMs to represent code without any constraints, analyzing the resulting ERs and uncovering five key findings. These findings shed light on how LLMs comprehend syntactic structures, APIs, and numerical computations in code. (2) Generating ERs in the constrained setting. In the constrained setting, we impose constraints on ERs, such as natural language comments, pseudocode, and flowcharts. This allows our approach to address a range of software engineering tasks. Based on our experiments, we have three findings demonstrating that our approach can effectively generate ERs that adhere to specific constraints, thus supporting various software engineering tasks. (3) Future directions. We also discuss potential future research directions, such as deriving intermediate languages for code generation, exploring LLM-friendly requirement descriptions, and further supporting software engineering tasks. We believe that this paper will spark discussions in research communities and inspire many follow-up studies.

代码生成大模型自省机制软件工程

Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。