arXiv:2508.15866cs.PLcs.LG2025-08被引 8

用约束解码生成正确代码,确保一次运行就对。

Correctness-Guaranteed Code Generation via Constrained Decoding

  • 引入上下文感知解析器,每步输出正则引导下一步生成
  • 在sLua上实现语法正确性,支持指定脚本API
  • 适用于游戏机制等需一次正确的高危场景

语言模型用于代码生成时,保证程序正确性仍是重大挑战。尽管开发中不完美代码可接受,但视频游戏和机器人等领域要求关键组件一次运行即正确。本文提出一种约束解码算法,结合上下文敏感解析器,在每一步输出满足关键非扩展性质的正则表达式,以引导生成可延续至正确程序的词元序列。为构建该解析器,提出动态解析树(ToP)框架,每个解析器对应一个带上下文信息(如变量作用域、类型约束)的模块化上下文无关文法,树分支代表未来代码段的歧义。通过sLua(强类型Lua变体)验证方法,可生成符合任意指定脚本API的语义正确程序。进一步表明,经精心设计,语义保证可延伸至运行时正确性,已在生成类银河城游戏机制中验证。

原文摘要 · Abstract (English)

Language Models (LMs) are increasingly being used for code generation, but ensuring the correctness of generated programs remains a significant challenge. Although imperfect code may be acceptable during software development with human oversight, domains such as video games and robotics require one-shot correctness for runtime-critical components. We present a constrained decoding algorithm for generating semantically correct programs that incorporates a context-sensitive parser, which, at each step, outputs a regular expression that satisfies a critical non-extensible property to guide the generation of the next token sequence that can continue to a correct program. To build such a context-sensitive parser, we propose a framework of a dynamic tree of parsers (ToP) during parsing, where each parser corresponds to a modular context-free grammar enriched with contextual information such as variable scopes and type constraints, with tree branches representing ambiguity in the future code segment. We demonstrate our approach through sLua, a strongly typed variant of Lua, showing that our method can generate semantically correct programs conforming to any prescribed scripting API. We further show that, with careful design, our semantic guarantees extend to runtime correctness, as validated in the application of generating game mechanics for a roguelike video game.

代码生成约束解码语义正确游戏开发

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