Abstract
Generating regular expressions (regex) from natural language is a challenging task due to program aliasing, where multiple syntactically different expressions are semantically equivalent. Traditional models trained with Maximum Likelihood Estimation (MLE) fail to account for this, punishing models for producing valid but non-matching strings. We present SemRegex, a framework that leverages reinforcement learning to optimize for semantic correctness. By utilizing a DFA oracle and a novel distinguishing test-case generation strategy, SemRegex achieves state-of-the-art performance across multiple benchmarks. Our results demonstrate that semantic rewards provide a superior learning signal, allowing models to learn the underlying logic of a task even when syntactic accuracy plateaus.
Introduction
Regular expressions are a powerful yet notoriously difficult tool for programmers. While deep learning models have shown promise in translating natural language (NL) into code, they often struggle with Program Aliasing—the phenomenon where multiple syntactically distinct programs perform the same semantic function. This paper introduces SemRegex, a framework that shifts the training objective from character-level imitation to semantic correctness.
The Challenge of Program Aliasing
Most existing code-generation models rely on Maximum Likelihood Estimation (MLE), which rewards the model for exactly matching a ground-truth string. However, in regex, [0-9] and \d are semantically identical despite their syntactic differences. MLE punishes models for generating valid but "non-matching" alternatives, hindering the exploration of the solution space and failing to capture the underlying logic of the task. This "syntax-blindness" limits the model's ability to learn the true intent behind the natural language specification.
Methodology: Learning from Semantics
SemRegex utilizes a sequence-to-sequence (Seq2Seq) architecture with an attention mechanism, optimized using Policy Gradient (REINFORCE) to maximize a semantic reward. The core of the framework lies in how it calculates this reward signal.
The DFA Oracle and Distinguishing Test Cases
For regular expressions, semantic equivalence can be determined by converting expressions into Minimal Deterministic Finite Automata (DFAs). If two regexes yield the same minimal DFA, they are functionally identical.
To extend this concept to domains where a perfect oracle is unavailable, the authors propose a distinguishing test case strategy. By sampling the model's top guesses via beam search, the system identifies strings that highlight the logical differences between the model's output and the ground truth. This forces the model to learn the subtle boundaries of the regex logic.
Results and Discussion
SemRegex was evaluated on three benchmarks: KB13, NL-RX-Synth, and NL-RX-Turk. It achieved significant improvements over the previous state-of-the-art (Deep-Regex), most notably on the KB13 dataset, where accuracy increased from 65.6% to 78.2%.
Semantic vs. Syntactic Accuracy
A key finding of the study is that semantic accuracy continues to improve even after syntactic accuracy plateaus. This suggests that the model is learning the logical constraints of the problem rather than just memorizing character sequences. The semantic reward provides a much richer learning signal, which is particularly crucial when training data is scarce.
Conclusion
SemRegex demonstrates that semantic rewards provide a more effective learning signal than simple string matching for code generation tasks. While the DFA oracle is specific to regex, the distinguishing test-case approach offers a generalizable framework for training models to understand code semantics across various programming languages. Future work will explore scaling these techniques to Turing-complete languages and refining reward shaping to further accelerate training.
Summary updated