skip to main content
research-article

Embedded parser generators

Authors Info & Claims
Published:22 September 2011Publication History
Skip Abstract Section

Abstract

We present a novel method of embedding context-free grammars in Haskell, and to automatically generate parsers and pretty-printers from them. We have implemented this method in a library called BNFC-meta (from the BNF Converter, which it is built on). The library builds compiler front ends using metaprogramming instead of conventional code generation. Parsers are built from labelled BNF grammars that are defined directly in Haskell modules. Our solution combines features of parser generators (static grammar checks, a highly specialised grammar DSL) and adds several features that are otherwise exclusive to combinatory libraries such as the ability to reuse, parameterise and generate grammars inside Haskell.

To allow writing grammars in concrete syntax, BNFC-meta provides a quasi-quoter that can parse grammars (embedded in Haskell files) at compile time and use metaprogramming to replace them with their abstract syntax. We also generate quasi-quoters so that the languages we define with BNFC-meta can be embedded in the same way. With a minimal change to the grammar, we support adding anti-quotation to the generated quasi-quoters, which allows users of the defined language to mix concrete and abstract syntax almost seamlessly. Unlike previous methods of achieving anti-quotation, the method used by BNFC-meta is simple, efficient and avoids polluting the abstract syntax types.

Skip Supplemental Material Section

Supplemental Material

_talk10.mp4

References

  1. E. Axelsson, K. Claessen, G. Dévai, Z. Horváth, K. Keijzer, B. Lyckegård, A. Persson, M. Sheeran, J. Svenningsson, and A. Vajdax. Feldspar: A domain specific language for digital signal processing algorithms. In MEMOCODE, pages 169--178, 2010.Google ScholarGoogle ScholarDigital LibraryDigital Library
  2. H. G. Baker. Pragmatic parsing in Common Lisp; or, putting defmacro on steroids. SIGPLAN Lisp Pointers, IV: 3-15, April 1991. ISSN 1045-3563. Google ScholarGoogle ScholarDigital LibraryDigital Library
  3. A. Bawden. Quasiquotation in LISP. In O. Danvy, editor, Proceedings of the Workshop on Partial Evaluation and Semantics-Based Program Manipulation, pages 88--99, San Antonio, 1999. University of Aarhus, Dept. of Computer Science.Google ScholarGoogle Scholar
  4. Boost. The Boost initiative for free peer-reviewed portable C++ source libraries. http://www.boost.org, 2009.Google ScholarGoogle Scholar
  5. K. Czarnecki, T. O'Donnell, John, J. Striegnitz, and W. Taha. DSL Implementation in MetaOCaml, Template Haskell, and C++, volume 3016 of LNCS, pages 51--72. Springer-Verlag, Berlin, Heidelberg, 2003. 10.1007/b98156.Google ScholarGoogle Scholar
  6. J. de Guzman and H. Kaiser. Boost spirit. http://www.boost.org/doc/libs/1_46_1/libs/spirit/.Google ScholarGoogle Scholar
  7. D. Devriese and F. Piessens. Explicitly recursive grammar combinators -- a better model for shallow parser DSLs. In Practical Aspects of Declarative Languages (PADL) 2011, volume 6539 of LNCS. Springer, Jan. 2011. Google ScholarGoogle ScholarDigital LibraryDigital Library
  8. M. Forsberg and A. Ranta. The BNF converter: A high-level tool for implementing well-behaved programming languages. In NWPT'02 proc., Proc. Estonian Academy of Sciences, December 2003.Google ScholarGoogle Scholar
  9. D. Leijen and E. Meijer. Parsec: Direct style monadic parser combinators for the real world. Technical Report UU-CS-2001-35, Departement of Computer Science, Utrecht University, 2001. http://www.cs.uu.nl/ daan/parsec.html.Google ScholarGoogle Scholar
  10. G. Mainland. Why it's nice to be quoted: quasiquoting for Haskell. In Proc. ACM SIGPLAN workshop on Haskell, Haskell '07, pages 73--82, New York, NY, USA, 2007. ACM. ISBN 978-1-59593-674-5. Google ScholarGoogle ScholarDigital LibraryDigital Library
  11. G. Mainland. The haskell package language-c-quote. http://hackage.haskell.org/package/language-c-quote, 2009.Google ScholarGoogle Scholar
  12. R. C. Moore. Removing left recursion from context-free grammars. In Proc. 1st North American chapter of the Association for Computational Linguistics conference, pages 249--255, San Francisco, CA, USA, 2000. Morgan Kaufmann Publishers Inc. Google ScholarGoogle ScholarDigital LibraryDigital Library
  13. S. Owens, M. Flatt, O. Shivers, and B. Mcmullan. Lexer and parser generators in scheme. In 2004 Scheme Workshop, 2004. URL http://repository.readscheme.org/ftp/papers/sw2004/owens.ps.gz.Google ScholarGoogle Scholar
  14. A. Ranta. Grammatical framework. J. Funct. Program., 14: 145--189, March 2004. ISSN 0956-7968. Google ScholarGoogle ScholarDigital LibraryDigital Library
  15. T. Rendel and K. Ostermann. Invertible syntax descriptions: unifying parsing and pretty printing. In Proc. third ACM Haskell symposium, Haskell '10, New York, NY, USA, 2010. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  16. T. Sheard and S. P. Jones. Template meta-programming for Haskell. In Proceedings of the Haskell workshop, pages 1--16. ACM Press, 2002. ISBN 1-58113-605-6. Google ScholarGoogle ScholarDigital LibraryDigital Library
  17. J. Svensson, K. Claessen, and M. Sheeran. GPGPU kernel implementation and refinement using Obsidian. Procedia Computer Science, 1 (1): 2065 -- 2074, 2010. ISSN 1877-0509. 10.1016/j.procs.2010.04.231. ICCS 2010.Google ScholarGoogle Scholar
  18. S. D. Swierstra. Combinator Parsing: A Short Tutorial, pages 252--300. Springer-Verlag, Berlin, Heidelberg, 2009. ISBN 978-3-642-03152-6. 10.1007/978-3-642-03153-3_6. Google ScholarGoogle ScholarDigital LibraryDigital Library

Index Terms

  1. Embedded parser generators

    Recommendations

    Reviews

    Jacques Carette

    Recently, there have been many exciting experiments that focus on embedding an advanced functionality as a domain-specific language (DSL) in a convenient host language. Long ago, work was done to show that this was feasible. Today, the effort is on demonstrating that it is both well understood and convenient. Interestingly, it seems that most of these experiments are conducted in two languages: Haskell and Scala. It is always good conversation, at least between programming language people, to theorize as to why that is. In this work, the functionality studied is parsing--more specifically, parser generators--and the language is Haskell. The foundation of the authors' work is well known: all the information one needs to process a language given via Backus-Naur form (BNF) is in the BNF grammar. Making this effective and usable in practice is not a trivial engineering feat, which the authors detail. Key to this is (restricted) reflection, as made available through Template Haskell. In fact, they go one level further: from the BNF, they automatically generate a quasi-quoter, which allows users to use concrete syntax when embedding terms in Haskell itself. Furthermore, the authors show that this embedding, which they call BNFC-meta, provides some additional features, such as grammar reuse (through compositionality) and improved error handling. The details of how all of this is achieved are clearly and succinctly laid out. This is followed by a convincing experimental analysis of the efficiency and scalability effects of their work. Essentially, the authors conclude that, as compared to parser combinators (Parsec in particular), runtime parsing is faster with BNFC-meta, but compilation time goes up somewhat and compiled binary size goes up quite a lot (but this may be a fixable artifact of the Glasgow Haskell Compiler (GHC)). This thoroughly enjoyable, clearly written paper nicely demonstrates the power of internalizing a well-known technology. It is a must-read for anyone interested in the practical aspects of programming language design. Online Computing Reviews Service

    Access critical reviews of Computing literature here

    Become a reviewer for Computing Reviews.

    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 ACM SIGPLAN Notices
      ACM SIGPLAN Notices  Volume 46, Issue 12
      Haskell '11
      December 2011
      129 pages
      ISSN:0362-1340
      EISSN:1558-1160
      DOI:10.1145/2096148
      Issue’s Table of Contents
      • cover image ACM Conferences
        Haskell '11: Proceedings of the 4th ACM symposium on Haskell
        September 2011
        136 pages
        ISBN:9781450308601
        DOI:10.1145/2034675

      Copyright © 2011 ACM

      Publisher

      Association for Computing Machinery

      New York, NY, United States

      Publication History

      • Published: 22 September 2011

      Check for updates

      Qualifiers

      • research-article

    PDF Format

    View or Download as a PDF file.

    PDF

    eReader

    View online with eReader.

    eReader
    About Cookies On This Site

    We use cookies to ensure that we give you the best experience on our website.

    Learn more

    Got it!