[antlr-interest] validating semantic predicates

Harald M. Müller harald_m_mueller at gmx.de
Fri Nov 30 14:24:17 PST 2007


Hi -

> On 11/30/07, Harald M.  Müller <harald_m_mueller at gmx.de> wrote:
> > Ok, Mark. You forced me to.
> 
> Thanks for all the time you've spent trying to help me! I 
> have more replies below.

Sure :-) - it's a hobby (at the moment), after all!

> > Still, an honest interpreter should, IMVHO pass parameters with 
> > computations' values into the rules and guard each real 
> (side-effect) 
> > action with the result of checking a parameter ...
> 
> Do you mean like the following? 
[...]
> 
> statement
>   : simpleStatement[true]
>   | ^('IF' b=condition simpleStatement[$b.result])
>   ;
> 
> simpleStatement[boolean b]
> 	: ^('ADD' n=NUMBER) { if ($b) value += toInt(n); }
> 	| ^('SUBTRACT' n=NUMBER) { if ($b) value -= toInt(n); }
> 	| 'PRINT' { if ($b) System.out.println(value); }
> 	;
> 

Yes - but the more I look at 

* that code ("my" code)
* your code
* the code at
http://www.antlr.org/wiki/display/ANTLR3/Simple+tree-based+interpeter with
its pushIndex and getIndex

the more I'm sure that I would write some sort of visitor which simply takes
the node's children and does whatever it wants with them.

I once had to write simplification rules for some sort of expression trees;
in my algorithm, you first ran down and up some subtrees to get some
information about used elements; then you would - based on the results of
that first pass - either go down the first and then the second subtree; or
the other way round; or only one of the subtrees. This was very easy to
write with 3 ifs and corresponding visit calls; I cannot imagine how a
fixed-order tree parser would do this easily.
So my rule of thumb now becomes

   If you want to skip nodes or reorder the actions you do on the tree, dont
use a tree grammar.

If ones makes tree grammars LL(1) - which is easy by selecting the right
symbols in the tree builder, I'd say (or at least after one tree rewrite) -,
a hand-written visitor/tree parser is not that much harder to write than
some tree grammar-based traverser (I'd guess ...).

Regards & good night (here near Munich)
Harald




More information about the antlr-interest mailing list