[Paper Review] Inferring Input Grammars from Dynamic Control Flow
This paper presents Mimid, a novel algorithm that infers human-readable, accurate context-free grammars from dynamic control flow in stack-based recursive descent parsers, without relying on data flow or heuristics. It analyzes how input characters are accessed across parser locations, successfully recovering grammars for programs like expr, URLparse, and microJSON with high precision and readability.
A program is characterized by its input model, and a formal input model can be of use in diverse areas including vulnerability analysis, reverse engineering, fuzzing and software testing, clone detection and refactoring. Unfortunately, input models for typical programs are often unavailable or out of date. While there exist algorithms that can mine the syntactical structure of program inputs, they either produce unwieldy and incomprehensible grammars, or require heuristics that target specific parsing patterns. In this paper, we present a general algorithm that takes a program and a small set of sample inputs and automatically infers a readable context-free grammar capturing the input language of the program. We infer the syntactic input structure only by observing access of input characters at different locations of the input parser. This works on all program stack based recursive descent input parsers, including PEG and parser combinators, and can do entirely without program specific heuristics. Our Mimid prototype produced accurate and readable grammars for a variety of evaluation subjects, including expr, URLparse, and microJSON.
Motivation & Objective
- To address the lack of accurate, readable, and up-to-date formal input models for programs, which are critical for fuzzing, reverse engineering, and software testing.
- To overcome limitations of existing grammar inference tools that rely on data flow or heuristics, which fail when data is not stored or when common parsing patterns (e.g., PEG-style alternatives) are used.
- To develop a general, parser-agnostic method that infers context-free grammars solely from dynamic character access patterns in recursive descent parsers.
- To produce grammars that are not only accurate but also editable and comprehensible to humans, enabling broader use in program analysis and refactoring.
Proposed method
- The algorithm observes dynamic control flow and tracks which input characters are accessed at different points in the parser, using runtime instrumentation to log access patterns.
- It constructs a control flow graph of the parser and maps input character accesses to specific grammar rules based on the sequence and context of access.
- Using a generalized tree acceptor algorithm, it infers a grammar by generalizing over multiple access traces, identifying nonterminal structures from repeated access patterns.
- The method operates entirely without data flow analysis, avoiding reliance on variable propagation or storage of parsed data.
- It supports all stack-based recursive descent parsers, including PEG and parser combinators, by focusing on access order and control structure rather than data flow.
- The approach is implemented in Python and integrated into a self-contained Jupyter notebook for reproducibility and extensibility to other languages.
Experimental results
Research questions
- RQ1Can a grammar inference algorithm recover accurate and readable context-free grammars without relying on data flow analysis?
- RQ2Can such an algorithm generalize across diverse input parsers, including PEG and parser combinators, without requiring program-specific heuristics?
- RQ3Does the method outperform existing tools like Autogram in handling cases with no data flow or complex parsing patterns such as PEG-style alternatives?
- RQ4Can the inferred grammars be both human-readable and precise, enabling practical use in fuzzing, reverse engineering, and refactoring?
Key findings
- Mimid successfully inferred accurate and readable grammars for a range of programs, including expr, URLparse, and microJSON, with no false positives or missing rules.
- The method outperformed Autogram in both precision and recall, particularly in scenarios where data flow was absent or misleading, such as in PEG-style parsing with fallback rules.
- The algorithm correctly recovered grammars even when parsed data was not stored, demonstrating robustness to the absence of data flow.
- The approach generalized well across different input structures, including nested JSON-like formats and complex token sequences, as shown in the microjson.py case study.
- The grammar for microjson.py was reconstructed as a correct and human-readable EBNF grammar, matching the expected input syntax.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.