Beyond Functional Correctness: A Knowledge-Enhanced Iterative Framework for Generating Criteria-Compliant Code
While Large Language Models (LLMs) show great potential in code generation, their application in safety-critical domains is strictly constrained by mandatory standards like MISRA C. Our empirical study confirms this critical gap: state-of-the-art LLMs universally fail to comply with MISRA C criteria, and naïve self-refinement often leads to a “refinement-worsens-violation” dilemma. To address the dual challenge, we propose HI-MISRA, a Hybrid Iterative Refinement Framework. HI-MISRA tackles these challenges through two key innovations: (1) Knowledge-Enhanced Prioritized Refinement, which first applies deterministic fixes for simple violations, then guides the LLM to refine severity-prioritized batches of complex violations by injecting dynamic knowledge; and (2) Iterative Refinement with Regression Prevention, which employs a three-stage “compilation-functionality-compliance” verification pipeline to ensure that each modification yields a net improvement, while a dynamic suspension strategy prevents the refinement from getting stuck in ineffective loops. Experiments across LLMs, including GPT-5, Gemini-2.5-Flash, Claude-3.7-Sonnet, and DeepSeek-V3.1, demonstrate the superior performance of HI-MISRA. For instance, HI-MISRA achieves a 92.5% triple-pass rate (compilation, functionality, compliance) based on GPT-5, far surpassing the baseline (62.0%) at a similar cost and outperforming the best existing method (78.5% with higher costs). Experimental results offer a practical pathway for the deployment of LLMs in the safety-critical domain.
Authors
- J Zhang
- Y Zhang
- J Liu
- Siyu Zhang (ORCID: https://orcid.org/0000-0003-3249-2743)
- Wei Dong (ORCID: https://orcid.org/0000-0002-2213-4435)
- Shangwen Wang (ORCID: https://orcid.org/0000-0003-1469-2063)
Institutions
- National University of Defense Technology (CN)
Publication Details
- Journal
- ACM Transactions on Software Engineering and Methodology
- Published
- 2026-05-23
- DOI
- https://doi.org/10.1145/3817057
- Primary Topic
- Topic Modeling
- Type
- article
- Field-Weighted Citation Impact
- 0.00