Static Inference of Regular Grammars for Ad Hoc Parsers
Michael Schröder, Jürgen Cito
Abstract
Parsing—the process of structuring a linear representation according to a given grammar—is a fundamental activity in software engineering. While formal language theory has provided theoretical foundations for parsing, the most common kind of parsers used in practice are written ad hoc. They use common string operations without explicitly defining an input grammar. These ad hoc parsers are often intertwined with application logic and can result in subtle semantic bugs. Grammars, which are complete formal descriptions of input languages, can enhance program comprehension, facilitate testing and debugging, and provide formal guarantees for parsing code. But writing grammars—e.g., in the form of regular expressions—can be tedious and error-prone. Inspired by the success of type inference in programming languages, we propose a general approach for static inference of regular input string grammars from unannotated ad hoc parser source code. We use refinement type inference to synthesize logical and string constraints that represent regular parsing operations, which we then interpret with an abstract semantics into regular expressions. Our contributions include a core calculus ?? Σ for representing ad hoc parsers, a formulation of (regular) grammar inference as refinement inference, an abstract interpretation framework for solving string refinement variables, and a set of abstract domains for efficiently representing the constraints encountered during regular ad hoc parsing. We implement our approach in the Panini system and evaluate its efficacy on a benchmark of 204 Python ad hoc parsers. Compared with state-of-the-art approaches, Panini produces better grammars (100 % precision, 93 % average recall) in less time (0.82 ± 2.85 s) without prior knowledge of the input space.
Ask about this paper
Your agent reads all of it.
Lune indexed this paper to the last equation, along with the top-tier papers that cite it. Ask a question and the answer quotes them.
Your agent calls
Luneget_paper_fulltext
Free to start. No credit card required.
Terminal
Install the CLIlune papers fulltext 5ffaba5e-3d03-492d-b05e-3ee8a64ef31eBuilds on13
- Mining input grammars from dynamic control flowRahul Gopinath, Björn Mathis, Andreas ZellerFSE 2020 · 60 citations
- Symbolic Boolean derivatives for efficiently solving extended regular expression constraintsCaleb Stanford, Margus Veanes, Nikolaj S. BjørnerPLDI 2021 · 38 citations
- An SMT Solver for Regular Expressions and Linear Arithmetic over String LengthMurphy Berzish, Mitja Kulczynski, Federico Mora, Florin Manea et al.CAV 2021 · 37 citations
- Z3str4: A Multi-armed String SolverFederico Mora, Murphy Berzish, Mitja Kulczynski, Dirk Nowotka et al.FM 2021 · 30 citations
- Learning Highly Recursive Input GrammarsNeil Kulkarni, Caroline Lemieux, Koushik SenASE 2021 · 24 citations
Related papers
- One down, 699 to go: or, synthesising compositional desugaringsSándor Bartha, James Cheney, Vaishak BelleOOPSLA 2021 · 2 citations
- Intrinsic Verification of Parsers and Formal Grammar Theory in Dependent Lambek CalculusSteven Schaefer, Nathan Varner, Pedro Henrique Azevedo de Amorim, Max S. NewPLDI 2025
- Incremental Context-free Grammar Inference in Black Box SettingsFeifei Li, Xiao Chen, Xi Xiao, Xiaoyu Sun et al.ASE 2024 · 1 citation
- Context-Free Grammar Inference for Complex Programming Languages in Black Box SettingsFeifei Li, Xiao Chen, Xiaoyu Sun, Xi Xiao et al.ICSE 2026
- Inferring and Applying Type ChangesAmeya Ketkar, Oleg Smirnov, Nikolaos Tsantalis, Danny Dig et al.ICSE 2022 · 17 citations
