[antlr-interest] New article on StringTemplates and Treewalkers

Andy Tripp atripp at comcast.net
Tue Jan 10 20:26:21 PST 2006


>
>
>On Tue, 2006-01-10 at 22:25 -0500, Andy Tripp wrote:
>> 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.
>
>How the heck would you write a pattern based rule for static *flow
>analysis* to check if there is a path that does not end in return?
>
>I'll be so bold as to say that you can't unless you hand-coded it.
>
Yes, I do. Sorry, didn't mean to imply otherwise. I'm not saying that 
pattern-based-rules can
do a lot. I'm just saying that very few of the translation rules are 
simple, so the "embed simple
transforms in a grammar" approach doesn't apply.

>
>> >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."
>
>Hardly arbitrary. Is your software open source? I'd like to take a look
>at how you managed to do all these wonderful things without writing
>code.
>
By "arbitrary" here, I mean that if I ask "where is the 
missing-return-check rule invoked?", it's
not at all obvious (to others...obviously it is obvious to the person 
who wrote it). Same for all the
other rules. In my approach, my rules are just fired sequentially, 
there's just a single, flat list
of them.

Again, I am writing code, and my simple pattern matching does not do 
anything magical.
My point here was that if I've got 150 rules, I'd like to see them 
listed, in order, like this:
Rule[] rules = {
symbolTableBuilderRule,
preprocessorHandlingRule,
gotoRemoverRule,
keywordRemoverRule,
...
}

I think that's a lot easier than:

                       // Here we are at the METHOD_CALL node in the 
ANTLR C Grammar,
                      // so here are 40 rules that should be invoked at 
that point in the AST:
                      ...

>
>> 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".
>
>So rather than use existing languages than tools, invent your own?
>
No, I'm just using Java code (as shown with the "rule list" above).

As for invoking a pattern-matching-rule, text files can contain rules, 
one per line, like this:
"v = v + 1; --> v++;"
And a rule is created with ordinary Java code:
new SnippetRule("myfile.txt");

My code is not open source, but I have just finished a design document 
which I may publish.
Andy




More information about the antlr-interest mailing list