[antlr-interest] tree construction, associative operators, NullPointerException

Johan Grande johan.grande at crans.org
Thu Aug 7 16:21:19 PDT 2008


Gavin Lambert a écrit :
> At 01:39 8/08/2008, Johan Grande wrote:
>  >The solution I use until now (non binary trees) is not at all
>  >satisfying, because not only I would have binary trees, but my
>  >"solution" doesn't work for rules with more than one operator, for
>  >example :
>  >,----
>  >| equalityExpression
>  >|     :   instanceOfExpression ( ('==' | '!=') instanceOfExpression
>  >)*
>  >|     ;
>  >`----
> 
> If you change that to use ('==' | '!=')^ (note the extra caret) then the 
> operator will become the root node -- this will also automatically 
> produce a left-associative binary tree.
> 
Actually, this is the first possibility showed in the documentation, and 
not exactly what I want to do.

The idea is to have a set of classes generated by TOM and to construct a 
tree only with it, in order to be able to use tree manipulation tools 
(the main goal of TOM), pattern matching in particular.

TOM generates 3 files (AstAdaptor.java, AstTokenList.txt, AstTree.java) 
which are meant to make antlr able to construct trees using the classes 
TOM generates.
(here they are : http://perso.crans.org/~grande/antlr/)


The tree construction page gives this possibility :
,----
| additiveExpression
| 	:	(a=multiplicativeExpression->$a)
|                 (    '+' b=multiplicativeExpression
|                      -> ^('+' $additiveExpression $b)
|                 )*
| 	;
`----

So I tried to replace the " ^('+' " by " ^(Cons And ", where Cons and 
And are 2 classes generated by TOM from a description I wrote (Ast.gom, 
available at the address given above (interesting things lines >840)) :
,----
| andExpression
|     :   (a=equalityExpression -> $a)
|         ( '&' b=equalityExpression -> ^(Cons And $andExpression $b) )*
|    ;
`----
(I tried the designs ^(Cons And lhs rhs) ^(And lhs rhs), but none works.)
Shouldn't that be possible ?
I'm expert neither in Antlr nor in TOM, but as far as I know TOM just 
implements the possibility given by Antlr to build trees using 
separately defined classes.
If it is sure it should, then maybe there is a bug in the TOM Antlr 
adaptor generator. I just want to know if my code is correct, and if 
there is no bug in Antlr.


Thank you for your help,
Johan



More information about the antlr-interest mailing list