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.
Supplemental Material
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
- Boost. The Boost initiative for free peer-reviewed portable C++ source libraries. http://www.boost.org, 2009.Google Scholar
- 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 Scholar
- J. de Guzman and H. Kaiser. Boost spirit. http://www.boost.org/doc/libs/1_46_1/libs/spirit/.Google Scholar
- 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 Scholar
Digital Library
- 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 Scholar
- 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 Scholar
- 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 Scholar
Digital Library
- G. Mainland. The haskell package language-c-quote. http://hackage.haskell.org/package/language-c-quote, 2009.Google Scholar
- 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 Scholar
Digital Library
- 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 Scholar
- A. Ranta. Grammatical framework. J. Funct. Program., 14: 145--189, March 2004. ISSN 0956-7968. Google Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
- 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 Scholar
Digital Library
Index Terms
Embedded parser generators
Recommendations
LibDSL: a library for developing embedded domain specific languages in d via template metaprogramming
GPCE '14This paper presents a library called LibDSL that helps the implementer of an embedded domain specific language (EDSL) effectively develop it in D language. The LibDSL library accepts as input some kinds of ``specifications'' of the EDSL that the ...
Embedded parser generators
Haskell '11: Proceedings of the 4th ACM symposium on HaskellWe 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). ...
LR Automatic Parser Generator and LR(1) Parser
LR is an LR(1) parser generation system. It is written entirely in portable ANS1 standard Fortran 66 and has been successfully operated on a number of computers. LR uses a powerful algorithm of Pager's to generate a space efficient parser for any LR(1) ...









Comments