Synsema: Syntax-Guided Learning of Semantically Valid Programs

Levin Winter, Cong Li, Hao Sun, Zhendong SuNeurIPS 2026

TL;DR

Finding bugs in compilers requires generating weird, creative programs that still obey strict language rules—a task where hand-coded generators take years to build and standard AI models waste vast effort just learning where the semicolons go. SYNSEMA splits the job: it uses readily available language grammars to guarantee valid syntax by construction, freeing a small, randomly initialized reinforcement learning (RL) agent to focus entirely on learning semantics from compiler feedback. The result is a lightweight fuzzer that learns from scratch to write valid Rust and Java code—uncovering weird, unvisited corners of production compilers that even giant, pretrained LLMs miss.

What Problem Does This Paper Address?

To stress-test a compiler (like rustc or javac), you need to feed it diverse, valid programs. If your test program has a syntax typo, the compiler’s front-end parser immediately rejects it, leaving the complex optimization and code-generation machinery untouched. Even if the syntax is right, the program must still obey semantic rules—like type checking, variable scoping, and Rust’s notorious ownership constraints.

Semantically valid programs occupy a tiny fraction of all possible text inputs.

Historically, engineers had two painful choices:

  1. Hand-craft a generator: Spend months encoding an 800-page language specification into custom generation logic.
  2. Train a language model: Have a neural network predict tokens from scratch, forcing it to simultaneously figure out syntax and semantics in an astronomical search space—leading to >99.9% of early training attempts being rejected for trivial syntax errors.

The authors ask: What if we stop making the AI learn syntax, since we already have formal grammars for that?

Methods

SYNSEMA reformulates program generation as a syntax-guided reinforcement learning task, built on an asynchronous Deep Q-Learning setup (Ape-X) using a randomly initialized 124M-parameter GPT-2 architecture.

Figure 2: SYNSEMA consists of four components: actors, the compiler, replay buffer, and learner.

Here is what is standard vs. what is new:

  • Constrained Action Spaces (Systematic Exploration): Using standard Context-Free Grammars (CFGs), SYNSEMA masks out invalid moves so the agent cannot make a syntax error. The authors implement and compare three classic parsing algorithms as RL action spaces: LL (top-down) and LR (bottom-up), where actions are grammar rules/operations, and Earley (chart-based), where actions are valid next code tokens.
  • Fine-Grained Semantic Rewards (Novel): Compilers don't just say "pass/fail"; they point to exact line and column spans where errors occur. When compilation fails, SYNSEMA maps those error spans back to the exact RL actions that generated the offending code and penalizes them directly (-1 split across offending steps).
  • Novelty Bonus: If a program compiles, the final action gets a positive reward (up to 10, decaying linearly with repetition) based on whether it triggered new execution paths (edge coverage) inside the compiler itself.
Figure 3: Reward calculation for a fictitious program. A negative reward maps to the offending action due to a compilation failure, whereas a compiler coverage-based novelty score is assigned to the final action for a semantically valid program.

Datasets

Because SYNSEMA learns directly from interacting with rustc and Java 21 (javac) from scratch, no training datasets are used. It relies only on existing ANTLR/standard CFGs and a predefined subword vocabulary for identifiers and literals.

For post-hoc analysis of how unusual the generated programs were, the authors compared SYNSEMA's outputs against the Rust compiler regression test suite and programs generated by pretrained LLMs (DeepSeek-V4-Pro and Gemma-3-4B-IT).

Key Insights and Ideas

  • Ignorance is a Fuzzing Superpower: When compared against Fuzz4All powered by DeepSeek-V4-Pro (given an equivalent ~$100 compute budget), DeepSeek achieved higher overall compiler code coverage, yet found only 1 crash compared to SYNSEMA's 12 Rust bugs. Scoring the programs with an independent base model (Qwen3-8B) revealed why: pretrained LLMs stay on the beaten path of "human-like" code. Starting from random weights with zero human priors lets SYNSEMA stumble into bizarre combinations (like putting a diverging loop {} inside an unsized slice const parameter) that compiler devs never anticipated.
  • Blame the Culprit, Not the Messenger: Ablating the reward mechanism showed that mapping compiler error spans directly back to the exact actions that caused them increased valid-program compiler coverage by 25.2% compared to simply penalizing the final step of the episode.
  • Parser Choice Shapes Learning: Chart-based (Earley) token masking gave the best overall bug-finding and coverage, while bottom-up (LR) parsing achieved the highest compilation success rate in Rust (14.16%) despite needing ~3x more steps per token to build parse trees.

Key Results

Evaluated over five-day runs on Rust and Java 21 against an unconstrained token-by-token RL baseline:

  • Compilation Success: SYNSEMA achieved an average 7.29% semantic validity rate across languages/parsers (peaking at 14.16% for Rust Bottom-Up), crushing the unconstrained baseline's 0.03% average.
  • Compiler Exploration: Syntax-guided agents triggered 2.6× higher source coverage on average than the baseline (up to 4.05× with the Chart parser on Rust).
  • Real-World Impact: SYNSEMA uncovered 14 distinct crashes and hangs (12 in rustc, 2 in javac), including 5 previously unknown bugs in production compilers. The unconstrained baseline found zero.

Limitations and Future Work

  • Small Scale & Vocabulary: The prototype caps episodes at 128 actions (auto-completing unfinished prefixes with the shortest valid suffix), omits code comments, and restricts variable names/literals to a fixed vocabulary.
  • No Pretraining for Operations: While token-based chart parsing could theoretically wrap a pretrained LLM, operation-based agents (LL and LR) speak in grammar transitions, for which no pretrained models exist.
  • The Oracle Catch-22: Because the compiler under test is also the teacher, SYNSEMA can find compiler crashes, but it cannot tell if a compiler silently miscompiles invalid code without crashing. Future work could incorporate differential testing (comparing multiple compilers against each other) or runtime execution rewards.

Summary updated

Abstract

Generating effective inputs for testing systems that process highly structured data, such as compilers, is difficult due to the combination of syntactic and semantic constraints. Traditional approaches rely on manually crafted rules that require deep domain expertise and are difficult to maintain, while modern machine learning methods have to learn syntax and semantics simultaneously, which is challenging. We present Synsema, a novel approach that combines insights from both worlds by reformulating the generation of semantically valid programs as a syntax-guided reinforcement learning task. With syntax guaranteed by construction, even a small, randomly initialized model can rapidly learn semantic constraints in a drastically reduced space, enabling effective compiler testing. We implement our approach in the context of Java and Rust, which both exhibit strict semantic requirements, and train an agent to generate interesting and well-formed programs using fine-grained semantic feedback. Applied to compiler testing, we systematically compare different syntax-guided generation procedures with an unconstrained baseline and show that, by restricting the sampling space, Synsema learns semantic rules efficiently. While an average of only 0.03% of programs generated by the baseline are semantically valid, Synsema produces a diverse set of programs with a 7.29% success rate that trigger 2.6x more compiler behaviors. In addition, Synsema discovered 5 previously unknown bugs in production compilers, validating the practical impact of our approach.
BibTeX
@inproceedings{winter2026synsema,
  author = {Levin Winter and Cong Li and Hao Sun and Zhendong Su},
  title = {{Synsema: Syntax-Guided Learning of Semantically Valid Programs}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2026},
  url = {https://openreview.net/forum?id=HR79HDRXwx},
}
Figure