[antlr-interest] Problem with rewrite rule referencing the previous value of the AST

Mats Behre mb.fgel at ownit.nu
Fri Oct 26 13:04:56 PDT 2007


> You are getting this because you are using the ! operator and the ->
> rewrites at the same time, and you must chose one or the other.

That's what I was afraid of. Apparently v3 does not build the default tree if there is a rewrite rule present, and given the power and possibibilities with the rewrite rules I can see that the behaviour would not necessarily be predictable otherwise.

> You probably want a rewrite like this:
> 
>  	optionCmd: p=OptionKeyword (Dot!)? 
>                  (
>  			  t=StrictKeyword                    ->
> ^(GenericStatement $t)
>  			| a=MaxDurationKeyword b=intData     ->
> ^(GenericStatement $a $b)
>  		     )

Your suggestion is actually a good example of why I don't like doing this; Apart from cluttering the grammar with rewrite rules (all more or less alike), and thus decrease the readability, it is also bug prone - in this case the $p keyword was omitted. As always, tradeoffs may of course be necessary.
However, I guess that it is possible to do what I wanted (add a root node to the default tree with some items omitted) with Java code in the @after section. The good news is that now that I look at the problem again even that may not be necessary - as I put the produced tree into a Java list anyway I can probably just make sure the tree isn't used in higher level rules.

> Though perhaps you would want a different Node type for the two alts?

Not at this stage, the keywords are kept mostly for debugging. The trees are used to check for the availability and correctness of variables and other references.

Thanks anyway,
Mats


More information about the antlr-interest mailing list