skip to main content
research-article
Open Access

Derivative grammars: a symbolic approach to parsing with derivatives

Published:10 October 2019Publication History
Skip Abstract Section

Abstract

We present a novel approach to context-free grammar parsing that is based on generating a sequence of grammars called derivative grammars from a given context-free grammar and input string. The generation of the derivative grammars is described by a few simple inference rules. We present an O(n2) space and O(n3) time recognition algorithm, which can be extended to generate parse trees in O(n3) time and O(n2logn) space. Derivative grammars can be viewed as a symbolic approach to implementing the notion of derivative languages, which was introduced by Brzozowski.

Might and others have explored an operational approach to implementing derivative languages in which the context-free grammar is encoded as a collection of recursive algebraic data types in a functional language like Haskell. Functional language implementation features like knot-tying and lazy evaluation are exploited to ensure that parsing is done correctly and efficiently in spite of complications like left-recursion. In contrast, our symbolic approach using inference rules can be implemented easily in any programming language and we obtain better space bounds for parsing.

Reifying derivative languages by encoding them symbolically as grammars also enables formal connections to be made for the first time between the derivatives approach and classical parsing methods like the Earley and LL/LR parsers. In particular, we show that the sets of Earley items maintained by the Earley parser implicitly encode derivative grammars and we give a procedure for producing derivative grammars from these sets. Conversely, we show that our derivative grammar recognizer can be transformed into the Earley recognizer by optimizing some of its bookkeeping. These results suggest that derivative grammars may provide a new foundation for context-free grammar recognition and parsing.

References

  1. Michael D. Adams, Celeste Hollenbeck, and Matthew Might. 2016. On the Complexity and Performance of Parsing with Derivatives. In Proceedings of the 37th ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI ’16).Google ScholarGoogle ScholarDigital LibraryDigital Library
  2. Alfred V. Aho, Ravi Sethi, and Jeffrey D. Ullman. 1986. Compilers: principles, techniques, and tools. Addison Wesley.Google ScholarGoogle ScholarDigital LibraryDigital Library
  3. Jonathan Immanuel Brachthäuser, Tillmann Rendel, and Klaus Ostermann. 2016. Parsing with First-class Derivatives. In Proceedings of the 2016 ACM SIGPLAN International Conference on Object-Oriented Programming, Systems, Languages, and Applications (OOPSLA 2016).Google ScholarGoogle ScholarDigital LibraryDigital Library
  4. Janusz A. Brzozowski. 1964. Derivatives of regular expressions. Journal of the ACM 11, 4 (October 1964), 481–494.Google ScholarGoogle ScholarDigital LibraryDigital Library
  5. Nils Anders Danielsson. 2010. Total Parser Combinators. SIGPLAN Not. 45, 9 (Sept. 2010).Google ScholarGoogle ScholarDigital LibraryDigital Library
  6. Jay Earley. 1970. An efficient context-free parsing algorithm. Commun. ACM 13, 2 (Feb. 1970), 94–102.Google ScholarGoogle ScholarDigital LibraryDigital Library
  7. Matthew Flatt and PLT. 2010. Reference: Racket. Technical Report PLT-TR-2010-1. PLT Design Inc. https://racket- lang.org/tr1/ .Google ScholarGoogle Scholar
  8. John E. Hopcroft and Jeffrey D. Ullman. 1979. Introduction to Automata Theory, Languages and Computability (1st ed.). Addison-Wesley Publishing Co., Inc., Boston, MA, USA.Google ScholarGoogle ScholarDigital LibraryDigital Library
  9. Jeffrey Kegler. 2017. Marpa–R2. https://github.com/jeffreykegler/Marpa- - R2 .Google ScholarGoogle Scholar
  10. Joop M.I.M. Leo. 1991. A general context-free parsing algorithm running in linear time on every LR(k) grammar without using lookahead. Theoretical Computer Science 82, 1 (1991), 165 – 176. Google ScholarGoogle ScholarDigital LibraryDigital Library
  11. Matthew Might, David Darais, and Daniel Spiewak. 2011. Parsing with derivatives: A functional pearl. In International Conference on Functional Programming.Google ScholarGoogle ScholarDigital LibraryDigital Library
  12. Terence Parr and Kathleen Fisher. 2011. LL(*): The Foundation of the ANTLR Parser Generator. In Proceedings of the 32Nd ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI ’11). ACM, New York, NY, USA, 425–436. Google ScholarGoogle ScholarDigital LibraryDigital Library
  13. Elizabeth Scott. 2008. SPPF-Style Parsing From Earley Recognisers. Electronic Notes in Theoretical Computer Science 203, 2 (2008), 53 – 67.Google ScholarGoogle ScholarDigital LibraryDigital Library
  14. Seppo Sippu and Eljas Soisalon-Soininen. 1988. Parsing theory. Springer-Verlag.Google ScholarGoogle Scholar
  15. Peter Thiemann. 2017. Partial Derivatives for Context-Free Languages. In Proceedings of the 20th International Conference on Foundations of Software Science and Computation Structures - Volume 10203. Springer-Verlag New York, Inc., New York, NY, USA, 248–264. Google ScholarGoogle ScholarDigital LibraryDigital Library
  16. Larry Wall. 2000. Programming Perl (3rd ed.). O’Reilly & Associates, Inc., Sebastopol, CA, USA.Google ScholarGoogle Scholar

Index Terms

  1. Derivative grammars: a symbolic approach to parsing with derivatives

    Recommendations

    Comments

    Login options

    Check if you have access through your login credentials or your institution to get full access on this article.

    Sign in

    Full Access

    • Published in

      cover image Proceedings of the ACM on Programming Languages
      Proceedings of the ACM on Programming Languages  Volume 3, Issue OOPSLA
      October 2019
      2077 pages
      EISSN:2475-1421
      DOI:10.1145/3366395
      Issue’s Table of Contents

      Copyright © 2019 Owner/Author

      This work is licensed under a Creative Commons Attribution International 4.0 License.

      Publisher

      Association for Computing Machinery

      New York, NY, United States

      Publication History

      • Published: 10 October 2019
      Published in pacmpl Volume 3, Issue OOPSLA

      Permissions

      Request permissions about this article.

      Request Permissions

      Check for updates

      Qualifiers

      • research-article

    PDF Format

    View or Download as a PDF file.

    PDF

    eReader

    View online with eReader.

    eReader