OrdinalFix: Fixing Compilation Errors via Shortest-Path CFL Reachability

Wenjie Zhang, Guancheng Wang, Junjie Chen, Yingfei Xiong, Yong Liu, Lu Zhang · DOI

Abstract

The development of correct and efficient software can be hindered by compilation errors, which must be fixed to ensure the code's syntactic correctness and program language constraints. Neural network-based approaches have been used to tackle this problem, but they lack guarantees of output correctness and can require an unlimited number of modifications. Fixing compilation errors within a given number of modifications is a challenging task. We demonstrate that finding the minimum number of modifications to fix a compilation error is NP-hard. To address the compilation error fixing problem, we propose OrdinalFix, a complete algorithm based on shortest-path CFL (context-free language) reachability with attribute checking that is guaranteed to output a program with the minimum number of modifications required. Specifically, OrdinalFix searches possible fixes from the smallest to the largest number of modifications. By incorporating merged attribute checking to enhance efficiency, the time complexity of OrdinalFix is acceptable for application. We evaluate OrdinalFix on two datasets and demonstrate its ability to fix compilation errors within a reasonable time limit. Comparing with existing approaches, OrdinalFix achieves a success rate of 83.5%, surpassing all existing approaches (71.7%).

Introduction

Compilation errors—ranging from simple syntactic mistakes like missing semicolons to complex semantic issues like type mismatches—are a constant hurdle in software development. While modern compilers provide error messages, these diagnostics are often inaccurate or misleading, especially when multiple errors interact.

Recent research has explored neural network-based "black box" models to suggest fixes. However, these models suffer from two significant flaws: they do not guarantee that the suggested fix is correct, and they often suggest "noisy" fixes that modify more code than necessary. Ideally, a fix should require the minimum number of modifications (insertions, deletions, or replacements) to make a program valid. However, the authors of this paper prove that once semantic constraints (such as variable definitions and type checking) are considered, finding this minimum fix is an NP-hard problem.

Methods

OrdinalFix is a complete algorithm designed to navigate this NP-hard landscape efficiently. Its architecture relies on two primary pillars:

  1. Shortest-Path CFL Reachability: The program is represented as a weighted modification graph. Original tokens have a weight of 0, while modifications (edits) have a weight of 1. The algorithm searches for the "shortest path" through this graph that conforms to the language's Context-Free Grammar (CFG).
  2. Attribute Grammars for Semantics: To handle non-context-free properties, such as ensuring a variable is declared before use, the authors use attribute grammars. Semantic rules are encoded into attributes that are checked during the reachability analysis.
Figure 1: Representation of a program as a weighted modification graph.

The Ordinal Strategy

The algorithm works iteratively, searching for fixes in increasing order of modification count. It first identifies the group of programs with the absolute minimum number of syntactic modifications. It then performs merged attribute checking to determine if any of these candidates satisfy semantic constraints. If none do, it moves to the next "ordinal" level (e.g., fixes with two edits instead of one) and repeats the process until a valid fix is found.

Algorithm 1: The high-level OrdinalFix procedure.

To prevent the search space from exploding computationally, OrdinalFix implements three key optimizations:

  • Pruning: Dropping search branches as soon as a semantic constraint is violated.
  • Merging: Grouping different program fragments that share the same semantic meaning to avoid redundant checks.
  • Memoization: Caching the results of attribute computations for reuse.

Evaluation and Results

The researchers evaluated OrdinalFix on two distinct datasets: Middleweight Java (MJ), a synthesized dataset of 2,400 mutants, and DeepFix (C), a benchmark of 6,978 real-world student programs known for complex, multi-error issues.

Key Results

  • Superior Accuracy: On the DeepFix C dataset, OrdinalFix achieved an 83.5% success rate, significantly surpassing the previous state-of-the-art neural model, BIFI (71.7%), and nearly tripling the performance of the original DeepFix tool (33.4%).
  • Efficiency: Despite the NP-hard nature of the problem, the algorithm is performant. For C programs, the median fixing time was only 0.6 seconds, with over 84% of programs fixed within 10 seconds.
  • Robustness: Unlike neural models that depend on training data quality, OrdinalFix is a "complete" algorithm. If a fix exists within the search bounds, it is guaranteed to find it.

Conclusion and Future Work

OrdinalFix demonstrates that treating syntactic and semantic errors as a unified reachability problem is both viable and highly effective. While the algorithm's complexity grows with the number of errors, its current performance makes it a practical assistant for developers.

Future work may address current limitations, such as handling complex C macros and implementing tie-breaking logic. When multiple fixes have the same minimum edit distance, statistical models or Large Language Models (LLMs) could be integrated to rank these fixes and select the one that appears most natural to a human developer.

Summary updated

BibTeX
@misc{zhang2023ordinalfix,
  author = {Wenjie Zhang and Guancheng Wang and Junjie Chen and Yingfei Xiong and Yong Liu and Lu Zhang},
  title = {{OrdinalFix: Fixing Compilation Errors via Shortest-Path CFL Reachability}},
  year = {2023},
  eprint = {2309.06771},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2309.06771},
}
Figure