[antlr-interest] What to do when you need to semantically validate an expression?

Kay Roepke kroepke at classdump.org
Thu Dec 14 14:23:34 PST 2006

Hi Bill!

On 14. Dec 2006, at 3:31 , Bill Mayfield wrote:

> 1. Do I let ANTLR create an AST automatically or do I create a tree  
> by hand with more meaningful types like Expression, Sum, etc...

You can go either way, but unless you have really specific  
requirements, a homogeneous tree (all nodes are of the same class)  
should do it.
You'd differentiate the subtrees by their root node (most of them  
will be imaginary tokens).

> 2. The parsed result needs to be semantically validated. I presume  
> I need to write custom validation logic in each type for this???  
> This might answer question 1?

See question 3 ;)

> 3. Would it be better to write a translater by hand to translate  
> the expression tree to SQL or would it be better to use a  
> treewalker to do this?

A treewalker approach is the easiest to modify, IMHO, and can save  
you a lot of custom code. Furthermore you have the additional benefit  
of having a LL-parser for trees, too.
The semantic checks would be done in actions in that (or those) tree  

> I'm new to ANTLR, have just read (most of) the spec, and I could  
> use some guidance.

Have a look at the Mantra language Terence is working on <http:// 
linguamantra.org>. It is using ANTLR v3, although the principle is  
to v2, also. Not sure which version you will be using.



Kay Röpke

More information about the antlr-interest mailing list