Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

From 2004 article

Introduction
My previous article for Code Generation Network, Generating Java and XML Using StringTemplate, illustrated using StringTemplate with a single model and multiple views to have a Java program dump itself out in multiple forms such as Java and XML. A more common use of template engines, however, involves language or data translation where you need to parse as well as "unparse".

This article demonstrates how to translate a very simple C dialect, which I will call "C-" in honor of the grades I received in college, to three different languages, again using a single model and controller with multiple views. The targets are Java, Python, and bytecodes similar to the Jasmin Java bytecode assembler format. Merely swapping in a new template file generates completely different output without even recompiling the translator! This limited dialect of C is not particularly functional, but is complicated enough to be interesting from a translation point of view.

The goal is to translate C- input such as:

char c;
int x;
int foo(int y, char d) {
int i;
for (i=0; i<3; i=i+1)

Unknown macro: { x=3; y=5; }

}
to (the mostly identical) Java:

  • No labels