Abstract
Domain specific language integration has to provide the right balance between the expressive power of the DSL and the implementation and maintenance cost of the applied integration techniques. In this paper we discuss a DSL integration technique for the C++ programming language. The solution is based on compile-time parsing of the DSL code using the C++ template metaprogramming library called Metaparse. The parser generator is the C++ template metaprogram reimplementation of a runtime Haskell parser generator library. The full parsing phase is executed when the host program is compiled. The library uses only standard C++ language features, thus our solution is highly portable. As a demonstration of the power of this approach, we present a highly efficient and type-safe version of printf and the way it can be constructed using our library. Despite the well known syntactical difficulties of C++ template metaprograms, building embedded languages using Metaparse leads to self-documenting maintenable C++ source code.
This is a preview of subscription content, log in via an institution.
Buying options
Tax calculation will be finalised at checkout
Purchases are for personal use only
Learn about institutional subscriptionsReferences
Abrahams, D., Gurtovoy, A.: C++ Template Metaprogramming, Concepts, Tools, and Techniques from Boost and Beyond. Addison-Wesley, Boston (2004). p. 400. ISBN-0321-22725-6
Andersson, L.: Parsing with Haskell, 28 October 2001. http://www.cs.lth.se/eda120/assignment4/parser.pdf
Gil, Y., Lenz, K.: Simple and safe SQL queries with C++ templates. In: Consela, C., Lawall, J.L. (eds) 6th International Conference on Generative Programming and Component Engineering, GPCE 2007, Salzburg, Austria, pp. 13–24, 1–3 October 2007
Odersky, M., Spoon, L., Venners, B.: Programming in Scala, 2nd edn. Artima (2010). ISBN-10: 0981531644
The POSIX standard, 1003.1-2008 - IEEE Standard for Information Technology - Portable Operating System Interface (POSIX(R)). http://standards.ieee.org/findstds/standard/1003.1-2008.html
Porkoláb, Z., Sinkovics, Á.: Domain-specific language integration with compile-time parser generator library. In: Visser, E., Järvi, J. (eds) Proceedings of the Ninth International Conference on Generative Programming and Component Engineering, GPCE 2010, Eindhoven, The Netherlands, 10–13 October 2010
Sinkovics, Á., Porkoláb, Z.: Domain-specific language Integration with C++ template metaprogramming. In: Mernik, M. (ed) Formal and Practical Aspects of Domain-Specific Languages: Recent Developments, pp. 33–56. Published by Information Science Reference (an imprint of IGI Global), Hershey. ISBN 978-1-4666-2092-6 (hardcover) - ISBN 978-1-4666-2093-3 (ebook) - ISBN 978-1-4666-2094-0 pp. 33-56
Sinkovics, Á., Porkoláb, Z.: Implementing monads for C++ template metaprograms. In: Science of Computer Programming, Available online 23 January 2013. ISSN 0167–6423, doi:10.1016/j.scico.2013.01.002. (http://www.sciencedirect.com/science/article/pii/S0167642313000051)
Sinkovics, Á., Sajó, E., Porkoláb, Z.: Towards more reliable C++ template metaprograms. In: Penjam, J. (ed.) 12th Symposium on Programming Languages and Software Tools (SPLST 2011), Tallinn, Estonia, pp. 260–271, 5–7 October 2011
Sinkovics, Á., Porkoláb, Z.: Expressing C++ template metaprograms as lambda expressions. In: Horváth, Z., Zsók, V., Achten, P., Koopman, P. (eds.) Proceedings of Tenth Symposium on Trends in Functional Programming, Komrno, Slovakia, pp. 97–111, 2–4 June 2009
Visser, E.: Program transformation with Stratego/XT. In: Lengauer, C., Batory, D., Blum, A., Odersky, M. (eds.) Domain-Specific Program Generation. LNCS, vol. 3016, pp. 216–238. Springer, Heidelberg (2004)
The auto specified in C++11. http://en.cppreference.com/w/cpp/language/auto
The Boost. Proto library. http://www.boost.org/doc/libs/1_53_0/doc/html/proto.html
Continuation-passing style on Wikipedia. http://en.wikipedia.org/wiki/Continuation-passing_style
Marlow, S. (ed.): Haskell 2010 Language report (2010). http://www.haskell.org/haskellwiki/Language_and_library_specification
Icon, The Icon Programming Language. http://www.cs.arizona.edu/icon
The ISO C++11 standard, ISO/IEC 14882:2011(E) Information technology - Programming languages - C++. http://www.iso.org/iso/catalogue_detail.htm?csnumber=50372
Katahdin. http://www.chrisseaton.com/katahdin
Milewski, B.: Haskell and C++ template metaprogramming. http://bartoszmilewski.wordpress.com/2009/10/26/haskellc-video-and-slides
Milewski, B., Niebler, E.: Compile-Time/Run-Time Functional Programming in C++. http://2012.cppnow.org/session/variadic-template-metaprogramming-using-monads
The MPL Reference Manual. http://www.boost.org/doc/libs/1_53_0/libs/mpl/doc/refmanual.html
Sinkovics, Á., Porkoláb, Z.: Metaparse - Compile-time parsing with template metaprogramming. http://2012.cppnow.org/session/metaparse-compile-time-parsing-with-template-metaprogramming
The User Manual of mpllibs metaparse. http://abel.web.elte.hu/mpllibs/metaparse/manual.html
Veldhuizen, T., Gannon, D.: Active libraries: rethinking the roles of compilers and libraries. In: Proceedings of the SIAM Workshop on Object Oriented Methods for Inter-Operable Scientific and Engineering Computing OO 1998 (1998)
Veldhuizen, T.: Expression templates. C++ Report 7, 26–31 (1995)
The Stratego Program Transformation Language. http://strategoxt.org/
The Template Haskell programming language. http://www.haskell.org/haskellwiki/Template_Haskell
The xlxpressive library. https://github.com/istvans/mpllibs/tree/master/mpllibs/xlxpressive
The XMF programming language. http://itcentre.tvu.ac.uk/clark/xmf.html
The Boost Xpressive library. http://www.boost.org/doc/libs/1_55_0/doc/html/xpressive.html
Xpressive - “Nested Regexes and Sub-Match Scoping” and “Nested Results”. http://www.boost.org/doc/libs/1_53_0/doc/html/xpressive/user_s_guide.html#boost_xpressive.user_s_guide.grammars_and_nested_matches.nested_regexes_and_sub_match_scoping
Author information
Authors and Affiliations
Corresponding author
Editor information
Editors and Affiliations
A The Grammar
A The Grammar
Rights and permissions
Copyright information
© 2015 Springer International Publishing Switzerland
About this chapter
Cite this chapter
Porkoláb, Z., Sinkovics, Á., Siroki, I. (2015). DSL in C++ Template Metaprogram. In: Zsók, V., Horváth, Z., Csató, L. (eds) Central European Functional Programming School. CEFP 2013. Lecture Notes in Computer Science(), vol 8606. Springer, Cham. https://doi.org/10.1007/978-3-319-15940-9_3
Download citation
DOI: https://doi.org/10.1007/978-3-319-15940-9_3
Published:
Publisher Name: Springer, Cham
Print ISBN: 978-3-319-15939-3
Online ISBN: 978-3-319-15940-9
eBook Packages: Computer ScienceComputer Science (R0)