[antlr-interest] ANTLR 3.0ea3 released

Andy Tripp atripp at comcast.net
Fri Jun 24 18:58:14 PDT 2005


>
>
>>> On Jun 24, 2005, at 8:18 AM, Gerald B. Rosenberg wrote: > Even  
>>> where carry forward would work, look-back may simply be a >  
>>> cleaner conceptual model in some situations. hi. My experience is  
>>> that if you need to look back, it's just as easy to keep a ptr in  
>>> each rule of interest you enter so you can simply ask for the tree  
>>> location for the rule above you.
>> Doesn't that mean that in general, you have to keep a ptr for  
>> essentially all rules?
>> In other words, if I want to know if the current statement is  
>> somewhere within a
>> "for" block, I need to keep a ptr at each rule in which I might end  
>> up with an AST
>> that has a "statement" somewhere under a "for".
>
>Hi.  Actually you just need a single pointer: currentStatement and  
>define it once in stat rule I think.
>
I mean anywhere under the "for", as in:
for (;;) {
    statement;
    { 
         while(true) {
                if (false) {
                     your_statement_here_is_somewhere_under_a_for;
                }
          }
      }
}

>
>> That seems a lot more difficult than just overriding CommonAST to  
>> provide a parent and
>> being done with it.
>
>A lot slower to do the walk though... ;)
>
Just a few steps to the top of the tree. Almost always less than, say, 
10 or so.
Compare that to storing data (ok, we're in a "for loop now").
I think the walking's going to be faster, especially when parsing lots 
of code but
only walking up the tree rarely.

>
>>> Remember that when you want to look up, you mean you want to know  
>>> your context.
>> Not always. I often had to look at other places in the AST, and a  
>> stack with the current
>> context wouldn't have done it. For example, when manipulating C  
>> code, I see a "%d"
>> somewhere, look up the tree for the "printf" node, and then look  
>> down for the appropriate
>> child to get the "printf" argument that matches my "%d". Context is  
>> not enough.
>
>currentExpression will get that for you. ;)  Walk down from  
>currentExpression.
>
The place where you are ("%d") and the thing you're looking for ("i") 
are not in the same
expression:
printf("%d\n", i);




More information about the antlr-interest mailing list