[antlr-interest] New article on StringTemplates and Treewalkers

Andy Tripp atripp at comcast.net
Tue Jan 10 19:25:38 PST 2006


>
>
>On Jan 10, 2006, at 2:29 PM, Andy Tripp wrote:
>> Ter,
>> If I understand you right, you've inadvertently proved my point  
>> exactly.
>
>Uh oh! ;)
>
>> Your code above (if I read it right) only checks that the function  
>> contains at least one "return".
>> But it may find a return inside some "if", for example, whereas  
>> javac does static flow analysis
>> and will see that there exists a path through the function that  
>> does not end with a "return".
>> I mention this in my article.
>
>Ah.  Sorry.  If you want flow analysis then yes you must right it.   
>Then my boolean is a function call checking flow results.  Either I'd  
>walk the tree building flow graphs or I'd build another structure and  
>walk it.  What does this have to do with grammars and ST though?  
>

The point is that in this case, the tree walker isn't "guiding general 
output generation". Yes,
the "check-for-missing-return logic kicks in at each 
Function-declaration-type node in the tree,
but there's a lot of code there, and I don't really see the benefit of 
it being inside of a treewalker.
With the treewalker, you are grouping together the 100 rules that all 
happen to kick in
at a Function-declaration-type node in the tree, at that's not the best 
way to group them.

>You  
>must compute it, period, right?  Use a tree, use a flow graph, use  
>flat text, up to you.  Once you have a result to spit out (i.e,. "add  
>a return statement or not"), you can use ST to say that.  
>
As I say in the article, it's almost never that simple. In the case of a 
missing return, I also go
look at all the calls to the function and if the return value is ever 
used, I give a warning, because I
really don't know what value to return. Also, if the function is the 
special "main" function, I need to
do a System.exit() rather than a return.


>ST has  
>nothing to do with that.  The tree walker merely guides general  
>output generation...it would reference a previous pass over the input  
>that computed the flow analysis.
>
Why not just compute the flow analysis as needed?

My general objection to the tree walker is that I don't see it as 
"guiding general output generation", but
rather as "embedding calls to the code that does all the work at 
arbitrary places in a parser grammar
amidst ANTLR syntax."

I'd rather have code that says "Here are my 200 rules, in the order they 
should fire". That seems like
the better way to "guide output generation".

>
>Ter
>



More information about the antlr-interest mailing list