cs.PLJan 30, 2026

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

Authors: Shihao Xia, Mengting He, Haomin Jia, Xinyan Zhao, Linhai Song

Organizations: The Pennsylvania State University · University of the Chinese Academy of Sciences · Institute of Computing Technology, Chinese Academy of Sciences

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. 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.

Explore similar work

CardsList
  1. Verus-SpecGym: An Agentic Environment for Evaluating Specification Autoformalization

    May 26, 2026Anmol Agarwal, Natalie Neamtu, Pranjal Aggarwal +6AutoformalizationRaw Judge Outputs

  2. Natural Language based Specification and Verification

    May 11, 2026Zhaorui Li, Chengyu SongLarge Language Model SafetyVulnerable Code

  3. SpecPylot: Python Specification Generation using Large Language Models

    Apr 17, 2026Ragib Shahariar Ayon, Shibbir AhmedLlm-Driven Code SynthesisFormal Verification