用提示词方法自动定位修复代码逻辑错误,效果优于传统工具。
LecPrompt: A Prompt-based Approach for Logical Error Correction with CodeBERT
- 用CodeBERT计算困惑度和概率,定位错误的符号和代码行。
- 将修复任务转为掩码语言建模,自动替换错误代码片段。
- 轻量级提示微调,适合资源有限场景,支持多语言修复。
编程中的逻辑错误不会触发编译警告,难以发现,可能导致程序运行异常。本文提出LecPrompt,一种基于提示词的逻辑错误检测与修复方法,利用CodeBERT模型计算困惑度与日志概率,从符号和行级别识别异常模式,定位潜在错误源。通过将修复任务转化为掩码语言建模(MLM)问题,使用CodeBERT自回归生成修正后的代码。同时引入软提示微调策略,在低计算成本下适配特定修复任务。为评估性能,构建了在QuixBugs数据集上注入逻辑错误的QuixBugs-LE数据集。实验表明:在Python上,顶1修复准确率达74.58%(符号级),程序级修复准确率为27.4%;在Java上,符号级准确率为69.23%,程序级为24.7%。
原文摘要 · Abstract (English)
Logical errors in programming don't raise compiler alerts, making them hard to detect. These silent errors can disrupt a program's function or cause run-time issues. Their correction requires deep insight into the program's logic, highlighting the importance of automated detection and repair. In this paper, we introduce LecPrompt to localize and repair logical errors, an prompt-based approach that harnesses the capabilities of CodeBERT, a transformer-based large language model trained on code. First, LecPrompt leverages a large language model to calculate perplexity and log probability metrics, pinpointing logical errors at both token and line levels. Through statistical analysis, it identifies tokens and lines that deviate significantly from the expected patterns recognized by large language models, marking them as potential error sources. Second, by framing the logical error correction challenge as a Masked Language Modeling (MLM) task, LecPrompt employs CodeBERT to autoregressively repair the identified error tokens. Finally, the soft-prompt method provides a novel solution in low-cost scenarios, ensuring that the model can be fine-tuned to the specific nuances of the logical error correction task without incurring high computational costs. To evaluate LecPrompt's performance, we created a method to introduce logical errors into correct code and applying this on QuixBugs to produce the QuixBugs-LE dataset. Our evaluations on the QuixBugs-LE dataset for both Python and Java highlight the impressive capabilities of our method, LecPrompt. For Python, LecPrompt achieves a noteworthy 74.58% top-1 token-level repair accuracy and 27.4% program-level repair accuracy. In Java, LecPrompt delivers a 69.23\% top-1 token-level repair accuracy and 24.7% full program-level repair accuracy.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。