Python from expressions — The ANTLR series (Part 3)
A gist of what the process looks like so far
At the third step, I’ll be using the generated expression or the output of intermediate code generation from the previous part and substitute it into a template group file (something that is used by a templating engine) which will allow us to write the rendered function strings to a python file.
The templating engine will complete the goal of code generation by generating an actual usable code in our target language i.e. Python.
The first thing that comes to your mind when I mention templating engine are web frameworks. Almost, all of the modern web frameworks share a common goal of using a templating engine to generate dynamic, business ready web-pages. The end-goal of each templating engine is substitution of the fetched output into a template file for displaying it to the end-user on-the-fly.
Here’s a quick look at all the templating engines compared side-by-side:
I’ll be using a similar templating engine called StringTemplate. It is widely used for web page templating but also supports basic template operations for creating target language code files.
While writing a string template the following syntax cheat sheet is what one must remember:
<attribute>evaluates to the string value of attribute(our expression in this case) if it exists else empty string.
<expression>will be referred by the key
expressionwhile working with StringTemplate object in Java. So, if the user puts any value against the “expression” key, it will be referred as an expression attribute in the template.
- For custom or user defined objects inside the template, use
<attribute.property>, this looks for property of attribute as a property then accessor methods like getProperty() or isProperty() or hasProperty().
<attribute:t1(argument-list): ... :tN(argument-list)>Basically, iterating over a list of objects for the same template substitution. This applies multiple templates in order from left to right. The result of a template application upon a multi-valued attribute is another multi-valued attribute. The overall expression evaluates to the concatenation of all template elements
<! comment !>Comments, as defined, are ignored by StringTemplate.
- Template definitions look like function definitions with untyped arguments:
templateName(arg1, arg2, ..., argN) ::= "single-line template"or
templateName(arg1, arg2, ..., argN) ::= <<multi-line template>>or
templateName(arg1, arg2, ..., argN) ::= <%multi-line template that ignores indentation and newlines%>
Here’s a look at our example for Python StringTemplateExample.stg file:
Notice how I use the second template type from our basics example above in order to maintain the indentation and a two line gap since this would involve following Python PEP8’s rules of having two lines between python methods. Moving onto our Java usage of this template:
a gt b and c gt d
a eq b
We’ve generated our pythonic ready-to-evaluate parameterized function code from a basic boolean expression. There are many such use cases where-in a combination of ANTLR and StringTemplate (or some other templating engine) can be used for translating source language code to target language code which is ready to run.
Here’s a full link to the github project for the entire series: