ROCODE: Integrating Backtracking Mechanism and Program Analysis in Large Language Models for Code Generation
Authors:
- 26 April 2025
- conference paper
- Published by Institute of Electrical and Electronics Engineers (IEEE)
- p. 334-346
Abstract
Large language models (LLMs) have achieved impressive performance in code generation recently, offering programmers revolutionary assistance in software development. However, due to the auto-regressive nature of LLMs, they are susceptible to error accumulation during code generation. Once an error is produced, LLMs can merely continue to generate the subsequent code conditioned on it, given their inability to adjust previous outputs. Existing LLM-based approaches typically consider post-revising after code generation, leading to the challenging resolution of accumulated errors and the significant wastage of resources. Ideally, LLMs should rollback and resolve the occurred error in time during code generation, rather than proceed on the basis of the error and wait for post-revising after generation. In this paper, we propose Rocode,which integrates the backtracking mechanism and program analysis into LLMs for code generation. Specifically, we employ program analysis to perform incremental error detection during the generation process. When an error is detected, the backtracking mechanism is triggered to priming rollback strategies and constraint regeneration, thereby eliminating the error early and ensuring continued generation on the correct basis. Experiments on multiple code generation benchmarks show that ROCODE can significantly reduce the errors generated by LLMs, with a compilation pass rate of 99.1 %. The test pass rate is relatively improved by up to 23.8% compared to the best baseline approach. Compared to the post-revising baseline, the token cost is reduced by 19.3%. Moreover, our approach is model-agnostic and achieves consistent improvements across nine representative LLMs.Keywords
All Related Versions
Funding Information
- National Natural Science Foundation of China (62192733,61832009,62192731,62192730,62072007)
This publication has 19 references indexed in Scilit:
- Self-Planning Code Generation with Large Language ModelsACM Transactions on Software Engineering and Methodology, 2024
- Enchanting Program Specification Synthesis by Large Language Models Using Static Analysis and Program Verification2024
- Detecting hallucinations in large language models using semantic entropyNature, 2024
- Towards Greener Yet Powerful Code Generation via Quantization: An Empirical Study2023
- CODEP: Grammatical Seq2Seq Model for General-Purpose Code Generation2023
- Survey of Hallucination in Natural Language GenerationACM Computing Surveys, 2023
- Competition-level code generation with AlphaCodeScience, 2022
- Incorporating domain knowledge through task augmentation for front-end JavaScript code generation2022
- Jigsaw2022
- Pose Transformers (POTR): Human Motion Prediction with Non-Autoregressive Transformers2021