[antlr-interest] could not even do k=1 for decision xx; reason: timed out

Gavin Lambert antlr at mirality.co.nz
Sat Aug 8 15:26:05 PDT 2009


At 06:18 9/08/2009, Tomasz Jastrzebski wrote:
>Now I understand that the only reasonable such declaration must 
>look like:
>
>multExpr
>  : primaryExpr (('*' | '/') primaryExpr)*
>  ;

Actually, you *could* use this:
   multExpr
     : primaryExpr (('*' | '/') multExpr)?
     ;

The recursion would even make it easier to apply the rewrites as 
you want.  The downside of this is that it will only work properly 
for left-associative operators (unlike the original, which can be 
done either way).  Using the * is generally preferred practice 
though; avoids using up stack space :)

>After adding rewrite rules this may look like that:
>addExpr
>  : (multExpr -> multExpr) ( (o='+' | o='-') e=multExpr -> ^($o 
> $addExpr $e) )*
>  ;
>multExpr
>  : (primaryExpr -> primaryExpr) ( (o='*' | o='/') e=primaryExpr 
> -> ^($o $multExpr $e) )*
>  ;

Yes, this is a good way to do it.

>I would think that now I can simply replace:
>-> ^($o $addExpr $e)
>with
>-> ^(BIN_EXPR<BinaryExpression>[o.text, $addExpr.tree, $e.tree]>)
>
>- but not. $addExpr.tree in the second version is always NIL.

Have you tried just using $addExpr instead of $addExpr.tree?  (Not 
sure if that's valid, but it's worth trying.)  Also, your o.text 
should be $o.text; although you're going to lose some context in 
your tree by not keeping the full token objects.



More information about the antlr-interest mailing list