Abstract
Novice programmers often struggle with the formal syntax of programming languages. To assist them, we design a novel programming language correction framework amenable to reinforcement learning. The framework allows an agent to mimic human actions for text navigation and editing. We demonstrate that the agent can be trained through self-exploration directly from the raw input, that is, program text itself, without any knowledge of the formal syntax of the programming language. We leverage expert demonstrations for one tenth of the training data to accelerate training. The proposed technique is evaluated on 6975 erroneous C programs with typographic errors, written by students during an introductory programming course. Our technique fixes 14% more programs and 29% more compiler error messages relative to those fixed by a state-of-the-art tool, DeepFix, which uses a fully supervised neural machine translation approach.
Introduction
Syntax errors are a significant hurdle for beginners learning to code. Standard compilers often provide cryptic error messages that may point to lines far from the actual mistake. While automated tools like DeepFix have attempted to solve this using Neural Machine Translation (NMT), they often operate at a coarse line-level granularity. This makes them brittle; if a model cannot perfectly reconstruct a complex line, the fix fails.
We introduce RLAssist, a framework that treats code correction as a sequential decision-making process. By using Deep Reinforcement Learning (RL), RLAssist learns to navigate through a program and make surgical, token-level edits. This approach mimics the way a human programmer might scan a file for a missing semicolon or a misplaced brace.
Methodology
RLAssist models the correction task as an agent interacting with an environment (the program and the compiler).
Agent Actions and Environment
The agent views the program as a sequence of tokens. It maintains a "cursor" and can perform two types of actions:
- Navigation: Moving the cursor right or down to traverse the code.
- Editing: Performing operations (insertion, deletion, or replacement) on a specific set of "mutable tokens," such as semicolons, parentheses, and braces.
The agent's state—comprising the program text and cursor position—is processed through a Long Short-Term Memory (LSTM) network to capture contextual dependencies.
Learning via Exploration and Demonstration
The agent is trained using the Asynchronous Advantage Actor-Critic (A3C) algorithm. It receives rewards based on its ability to reduce the total number of compiler errors. To overcome the "cold start" problem of pure RL, the researchers utilized Learning from Demonstration. Expert-guided paths were provided for 10% of the training data, allowing the agent to learn basic syntax patterns quickly before transitioning to self-exploration for the remaining 90%.
Experimental Evaluation
We evaluated RLAssist on a dataset of 6,975 student-written C programs containing over 16,000 compilation errors. These programs spanned 93 different programming problems, ranging from basic control flow to recursion.
Comparative Performance
RLAssist was compared against DeepFix, the previous state-of-the-art. RLAssist successfully fixed 26.6% of the programs, a 14% relative improvement over DeepFix's 23.3%. More impressively, it resolved 39.7% of all individual error messages, outperforming DeepFix by 29%.
As shown in the training curves, RLAssist achieves a significantly higher fix percentage and reward compared to baseline models that lack the same combination of RL and expert demonstrations.
Discussion and Insights
A key advantage of RLAssist is its treatment of the compiler as a "black box." The agent does not require explicit knowledge of C grammar; it learns the rules of the language purely through the feedback loop of the compiler.
Furthermore, the token-level granularity allows for high precision. Unlike NMT models that might struggle to regenerate a long printf statement while fixing a single character, RLAssist only modifies the necessary token. This iterative nature allows the agent to recover from unsuccessful edits and continue searching for a valid solution.
Limitations and Future Work
Currently, RLAssist focuses on "typographic" errors involving punctuation. It does not yet handle logic errors or keyword-based syntax issues (e.g., missing int declarations). Future work will involve expanding the edit vocabulary and exploring multi-pass correction strategies to handle more complex semantic errors.
Summary updated