[Paper Review] Recognising and Generating Terms using Derivatives of Parsing Expression Grammars
This paper introduces a derivative-based technique for recognizing and generating terms in Parsing Expression Grammars (PEGs), extending Brzozowski's derivative concept—previously used for regular expressions and CFGs—to PEGs. By defining PEG derivatives and leveraging them for sentence generation, the method enables precise, tool-supported grammar analysis, revealing hidden bugs (e.g., in Ford’s $a^n b^n c^n$ grammar) and offering a general framework applicable to any formalism with a derivative definition.
Grammar-based sentence generation has been thoroughly explored for Context-Free Grammars (CFGs), but remains unsolved for recognition-based approaches such as Parsing Expression Grammars (PEGs). Lacking tool support, language designers using PEGs have difficulty predicting the behaviour of their parsers. In this paper, we extend the idea of derivatives, originally formulated for regular expressions, to PEGs. We then present a novel technique for sentence generation based on derivatives, applicable to any grammatical formalism for which the derivative can be defined--now including PEGs. Finally, we propose applying derivatives more generally to other problems facing language designers and implementers.
Motivation & Objective
- To address the lack of tool support for debugging PEGs, which are prone to subtle, hard-to-detect bugs due to complex interactions among lookahead and prioritized choice.
- To extend the derivative technique—successful in regular expressions and CFGs—to Parsing Expression Grammars (PEGs), enabling recognition and generation within a unified framework.
- To provide language designers and implementers with a practical, automated method to generate example sentences from PEGs, improving understanding and early detection of grammar flaws.
- To explore the broader applicability of derivatives in language engineering, including testing, equivalence checking, and supporting advanced features like left recursion and pattern matching on structured data.
Proposed method
- Define derivatives for PEGs by extending the core PEG expressions with wildcard ($\_$) and failure ($\emptyset$) to support systematic derivative computation.
- Adapt the derivative-based recognition algorithm to PEGs, ensuring that choices are evaluated in parallel and lookahead constraints are preserved during derivative computation.
- Use the derivative of a PEG expression to systematically explore all possible derivations, enabling the generation of valid sentences by traversing the derivative tree.
- Leverage the derivative-based generation technique to produce concrete examples from any PEG, including those with negative and positive lookahead, and prioritized alternation.
- Apply the method to detect grammar flaws by generating test sentences—e.g., revealing that Ford’s $a^n b^n c^n$ grammar also accepts $aaa$—demonstrating its utility in debugging.
- Generalize the approach to support advanced language features, such as OMeta-style pattern matching on structured data, enabling random object generation for testing.
Experimental results
Research questions
- RQ1Can the derivative technique, effective for regular expressions and CFGs, be extended to Parsing Expression Grammars (PEGs), which include complex features like prioritized choice and lookahead?
- RQ2How can derivatives be formally defined for PEGs to preserve semantic correctness, especially in the presence of negative and positive lookahead?
- RQ3Can a derivative-based approach be used to generate valid sentences from any PEG, even those with complex syntactic constraints?
- RQ4To what extent can derivative-based sentence generation improve tool support for language designers, particularly in detecting subtle grammar bugs?
- RQ5Can the derivative framework be extended to support advanced language features such as left recursion, OMeta-style pattern matching, and probabilistic equivalence checking?
Key findings
- The paper successfully defines derivatives for PEGs, including support for all core constructs such as $\epsilon$, terminals, nonterminals, sequences, alternations, repetitions, and both positive and negative lookahead.
- The derivative-based sentence generation technique correctly identifies unintended language members, such as $aaa$ in Ford’s $a^n b^n c^n$ grammar, revealing a previously undetected flaw.
- The method enables real-time grammar feedback in IDEs, where example sentences are generated dynamically as the grammar is edited, improving developer understanding and early bug detection.
- The approach supports probabilistic equivalence checking of PEGs by generating random sentences from both sides of a potential identity and validating match consistency.
- The derivative framework can be extended to support OMeta-style pattern matching, enabling random generation of structured data (e.g., objects, arrays) for testing purposes.
- The technique provides a foundation for resolving open questions in PEG semantics, such as the natural interpretation of mixed left- and right-recursive rules, by enabling parallel evaluation of choices via derivatives.
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.