[antlr-interest] V3: How to translate one language to another

Andy Tripp antlr at jazillian.com
Fri Dec 29 09:49:42 PST 2006


Bill Mayfield wrote:
> Hi,
>
> I'm working on a project that requires me to translate a domain 
> specific query language to a plain SQL statements. This domain 
> specific language is object-oriented instead of relational like SQL.
>
> For example:  (each employee is member of a department)
>
> SELECT firstname + ' ' + lastname, department.name FROM employees
>
> needs to be translated into something like:
>
> SELECT e.firstname + ' ' + e.lastname, d.name from employees e LEFT 
> JOIN departments d ON e.departmentid = d.id
>
>
>
> I have the grammer for my parser figured out and I can build an AST 
> that represents the language 
Actually, an AST just represents a particular input in thelanguage. 
You're building a grammar that will tell ANTLR how to generate
code that generates such ASTs.

> but I'm kind of STUCK on what to do next. I need to semantically 
> validate the query; but how?
I assume you mean here that you need to semantically *understand* the 
query, not validate it.
You need to know things like "does this SELECT statement in my input 
language require me to generate an SQL JOIN?",
not "Is this input actually reasonable?"


> Do I do this in a treewalker? Do I need a symbol table? And how can I 
> translate the query into SQL statements? Do I use templates? 
> TemplateString?
I would start by writing down, say, 10 example translations for 
SELECT...so you really get a field for what you'll need
to do to map your DSL to SQL. Then figure out what your logic needs to 
be (e.g. "SELECT in my DSL always maps
to SQL SELECT", "any time I see <otherTableName>.<fieldName> in my DSL, 
I'll need to generate a JOIN", etc).
Then plug your examples into ANTLR and make sure you're generating 
reasonable ASTs and make sure you understand
the structure of the ASTs ("e.g. a SELECT node's first child is a 
FIELDS_TO_SELECT NODE and its second child
is a FROM node", and the optional third child is a WHERE node").

I then would start writing a little vanilla Java code that walks the 
AST, doing things in whatever way seems right to you.
After a day or two of that, step back and say, "Am I really just doing 
some simple action at each particular place in the AST?"
If so, then a treewalker might be simpler and easier. If not, just keep 
going.
>
>
> I'm not so much interested in any 'specific' help (altough that would 
> be appreciated). I'm interested in the steps that are needed to get to 
> the solution. How many treewalkers? What does each do etc...
I don't see the need for a "validating treewalk" unless you really 
aren't sure that the input is valid, which seems
weird to me. Obviously, there are always cases where you might get 
invalid input, but presumably your one-and-only
treewalker will give an error in that case.
>
>
> If you have any idea, it would be greatly appreciated!
>
>
> Bill
>
>
>
> BTW: I've decided to use ANTLR v3
>



More information about the antlr-interest mailing list