[antlr-interest] StringTemplate stack overflow exception, stringtemplate-3.0

Hardy, Stephen Stephen_Hardy at rabbit.com
Fri Jul 20 19:14:20 PDT 2007


I managed to resolve this problem.  There was no circular reference, but
I made the mistake of blithely using null list pointers as template
parameters, when the grammar used "zero or more" arity (* operator).
Silly me, I already knew this was a problem for the '?' operator, but I
thought that lists were somehow handled differently.

The odd thing is that the null List objects were not causing null
pointer exceptions (as one would expect) but instead would cause
"random" data to be substituted into the template expansion or, in other
cases, would create infinite reference loops.

So now, the tree grammar is interspersed with stuff like

direct_abstract_declarator
    : abstract_declarator  (ads+=abstract_declarator_suffix)*
        -> template(
               ad={$abstract_declarator.st},
               ads={($ads==null?"":$ads)}) 
           "(<ad>)<ads; separator=\" \">"
;

Before, I had simply ads={$ads} which was no good.

Having to put in conditional expressions is not that much of a burden,
however it seems much cleaner to fix string templates so they check for
null, and substitute an empty string in this case -- this would be a
reasonable default.

Regards,
SJH


> -----Original Message-----
[cut]
> >
> > My tree grammar template rewrite rules, on the other hand, are
> > entirely
> > constructed using in-line (anonymous) templates -- look mum, no
> > java --
> > and hence if there are any circular references then they are
> > created by
> > the magic of ANTLR.
> >
> > If there is a recursive rule (say, compound statement in a C
grammar)
> > then nested compound statements get their own instance of the
> > template,
> > hence there can be no circular references at the java "pointer"
level?
> > If I am wrong, would you kindly provide a small counterexample?
> >
> > With respect, I would say that this potential for stack overflow
> > exceptions in an ANTLR-generated parser with no "open code" is a
> > serious
> > bug, especially if the bug is only revealed at run-time.
> 
> Well, this
> 
> ds={$declaration_specifiers.st}
> 
> is not exactly ANTLR code...you can refer to whatever you want... ;)
> 
> >> Those refs are what attributes are used in that template instance.
> >
> > There is something fishy going on.  If this is the list of
attributes,
> > then the given trace did not make sense.
> 
> You can easily create a template that includes itself using
> references to prior attributes and so on.	I'm afraid that ST is
> probably catching a correct cycle in your ST containment hierarchy.
> Is always caught my problems correctly.
> 
> Ter


More information about the antlr-interest mailing list