[antlr-interest] Transformation to new grammar

Monty Zukowski monty at codetransform.com
Mon Aug 15 11:41:53 PDT 2005


On Aug 15, 2005, at 5:03 AM, Barry McGillin wrote:


> I have a grammar for one version of SQL that I'm mapping into  
> Oracle SQL.
> We can walk the trees and generate what we need to however,  I have  
> a full version of the target grammar available.  Is is possible to  
> map from one grammar to another, so that it is possible to verify  
> that tree n is correct wrt the target grammar as shown below.
>
> |Source Grammar |  tree 1 | ... | tree n | Target grammar|
> |    o          |    o    |     |    o   |       o       |
> |   / \         |   / \   |     |   / \  |      / \      |
> |  o   o        |  o   o  |     |  o   o |     o   o     |
> |     / \       | /     \ |     | / \    |    / \        |
> |    o   o      |o       o|     |o   o   |   o   o       |
>
> Today, we can walk the source tree and make transformations to it  
> to move us from tree 1 towards tree n, but when do we know we're  
> finished and correct? I'm not looking for a specific solution, but  
> rather some ideas about how this can be done efficiently if at all :-)
>


To test the transformations done by a certain pass, I would run the  
result tree through a basic tree grammar with no actions in it, just  
to make sure it was still grammatically valid.  I structured all of  
my transformations to conform to a "super" grammar which was really  
the union of the input tree grammar and the output tree grammar.   
That's impossible to do with a regular grammar, but with a tree  
grammar it is quite convenient.  All you have to do is make sure that  
each language uses it's own unique token types.

As far as knowing you are finished and correct, write tests!  For  
each mini translation you do, write a complete example program and  
what you expect upon output.  Write a test program to do your  
transformation and diff the result of that against what you expect.   
It's really handy and important to do, because it's so easy to change  
something in one place and see the effect of it somewhere else when  
you are dealing with grammars.

By the way, I'm an ANTLR consultant and could give much more in depth  
advice if you hire me.  Also come to the ANTLR conference this year.   
I'll be there, as will many other experts, and we just sit around and  
complain until someone smarter comes up with a good solution :)

Monty





More information about the antlr-interest mailing list