用自然语言规范重构代码,让大模型改代码更可控、更可信。
Specification-Based Code-Text-Code Reengineering for LLM-Mediated Software Evolution
- 把代码转成中性文本规范,保留语义不丢逻辑
- 实验验证能有效降低转换错误率,提升可追溯性
- 适合需要安全演进的工业级代码维护场景
直接的代码到代码转换难以控制,可能引发语义漂移、行为隐藏变化、可追溯性丢失、非惯用实现或领域逻辑不完整等问题。本文提出一种基于规范的Code2Text2Code重构框架,用于大模型驱动的软件演化。核心思想是将源代码转化为中性文本规范,捕捉程序行为、标识符、计算流程、条件、副作用、数据依赖和领域特定意图,而不直接传递源语言语法。框架融合事实上下文提取、Code2Text生成、源代码与文本规范间的迭代验证、Text2Code生成、目标代码验证、检索增强定位、语义感知分块及转换损失估计。知识表示层整合来自抽象语法树(AST)、基于图的依赖结构、中性自然语言规范、技术文档、业务文档和架构级表示的元数据。实验包括多语言和SQL方言构建的Code2Text2Code数据集、中间表示对比、检索评估、文档转换评估以及使用DSPy的提示调优。采用图形式化方法,基于结构保真度、逆向兼容性、接口稳定性与整体图相似性来估算转换损失。结果支持该方法并非简单代码转换,而是一种受控的、基于规范的重构过程,适用于大模型辅助的软件演化。
原文摘要 · Abstract (English)
Direct Code2Code transformation remains challenging to control because it can preserve surface-level syntax while introducing semantic drift, hidden behavioral changes, loss of traceability, non-idiomatic target implementations, or incomplete reconstruction of domain logic. This paper proposes a specification-based Code2Text2Code reengineering framework for LLM-mediated software evolution. The central idea is to transform source code into a neutral textual specification that captures program behavior, identifiers, computational flow, conditions, side effects, data dependencies, and domain-specific intent without directly transferring the source language syntax. The proposed framework combines factual context extraction, Code2Text generation, iterative verification between source code and text specification, Text2Code generation, target code verification, retrieval-augmented grounding, and semantic-aware chunking, and transformation loss estimation. The knowledge representation layer integrates metadata derived from AST, graph-based dependency structures, neutral natural language specifications, technical documentation, business documentation, and architecture-level representations. The conducted experiments include a Code2Text2Code dataset built from multiple programming languages and SQL dialects, comparison of intermediate representations, retrieval evaluation, documentation transformation evaluation, and prompt tuning using DSPy. A graph formalization using structural preservation, reverse compatibility, interface stability, and total graph similarity is implemented to estimate transformation losses. The results support the interpretation of the Code2Text2Code approach not as a simple code transformation, but as a controlled specification-based reengineering process for LLM-mediated software evolution.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。