The Role of Template Engines in Code Generation

The documentation for systems that generate or translate programs often use exemplars to describe their output format. These exemplars should become part of the translator itself as formal “templates” (output grammars) to describe the structure of output sentences.

While this talk will focus on code generation and source translation issues, my StringTemplate engine actually emerged while building the dynamic pages for large commercial websites. To my great surprise, the simple embedded language evolved to have a distinctly functional flavor! I will begin in that domain to motivate the key distinguishing feature of StringTemplate: enforced strict separation of logic from presentation. All benefits such as ease of development, flexibility, reduced maintenance costs, parallel code, HTML generation etc… derive from this single principle of separation.

There are a few interesting side effects and properties of restricted templates. In the web domain, enforced separation equals enforced security for look-n-feel templates exposed to end-users of hosted applications. In the language translation domain, enforced separation yields a guarantee that your system is retargetable because there can be no logic embedded in the target language specification and there are no print statements strewn throughout the logic code.

I will demonstrate my StringTemplate engine at work solving some nontrivial generation tasks and show how StringTemplate is used in the code generator for the upcoming ANTLR 3.0 parser generator.

Speaker Details

Terence Parr is a professor of computer science at the University of San Francisco where he continues to work on his ANTLR parser generator, http://www.antlr.org. Terence recently returned from years in industry where he co-founded jGuru.com. He herded programmers and implemented the large jGuru developers web site, during which time he developed and refined the StringTemplate engine. Terence has consulted for and held various technical positions at companies such as IBM, Lockheed Missiles and Space, NeXT, and Renault Automation.Terence holds a Ph.D. in Computer Engineering from Purdue University and was a postdoctoral fellow at the Army High-Performance Computing Research Center at the University of Minnesota where he built parallelizing FORTRAN source-to-source translators.

Date:
Speakers:
Terence Parr
Affiliation:
University of San Francisco
    • Portrait of Jeff Running

      Jeff Running