Doc2Spec: Synthesizing Formal Programming Specifications from Natural Language via Grammar Induction

Shihao Xia, Mengting He, Haomin Jia, Xinyan Zhao, Linhai Song
1/30/2026
cs.PLcs.AIcs.SE

No confident code implementation found for this paper.

Abstract

Ensuring that API implementations and usage comply with natural language programming rules is critical for software correctness, security, and reliability. Formal verification can provide strong guarantees but requires precise specifications, which are difficult and costly to write manually.

Show full abstract

To address this challenge, we present Doc2Spec, a multi-agent framework that automatically induces a domain-specific grammar from natural-language API rules and uses it to guide specification generation. Doc2Spec fixes a domain-agnostic logical skeleton as a grammar template, prompts LLMs to infer domain-specific predicates and sorts, and formalizes each rule within the resulting grammar, turning an unreliable one-shot translation into a sequence of constrained, checkable steps. Across six benchmarks spanning Solidity and Rust, Doc2Spec improves precision by 0.28 and recall by 0.37 over baselines that lack grammar induction or perform it in one unstaged step, demonstrating the benefits of grammar-guided formalization and the staged pipeline. Moreover, its formalized rules enable symbolic-execution tools to uncover 142 previously unknown rule violations, confirming the rules' correctness and practical usefulness.

View on arXivView PDF

Code Implementations

No confident code match yet

We couldn't find an author-owned or strongly-evidenced community implementation for this paper. Any repos shown below are weak matches — verify before relying on them.

No code implementations found yet.

Know of an implementation? Let us know in the comments below!

Similar Papers

Cite this paper

@article{xia2026docspec,
  title  = {Doc2Spec: Synthesizing Formal Programming Specifications from Natural Language via Grammar Induction},
  author = {Shihao Xia and Mengting He and Haomin Jia and Xinyan Zhao and Linhai Song},
  year   = {2026},
  eprint = {2602.04892},
  archivePrefix = {arXiv},
  url    = {http://arxiv.org/abs/2602.04892v2}
}

Add a badge to your README

Shows whether official code is linked for this paper.

Code of Paper badge
[![Code of Paper](https://codeofpaper.com/badge/2602.04892.svg)](https://codeofpaper.com/paper/2602.04892)

Discussion