[antlr-interest] philosophy about translation

Andy Tripp antlr at jazillian.com
Sat Oct 7 10:40:07 PDT 2006


Terence Parr wrote:

>
> Andy: how do you deal with references to grammatical constructs such  
> as <expr>? ANTLR you only work with tokens, period?    How do you  
> deal with extreme context like you need to do a translation on this  
> expression but only if it's the expression within a while within a  
> particular function?


I've built up a nice library of functionality that works on token 
sequences. My Source class represents
all the (evolving through phases) source code, and has all this 
functionality. Something like:

boolean match(Source source) {
  if (source.isWithinWhile(source.currentToken)) {
      Token func = getContainingFunction(source.currentToken);
      if (func.getText().equals("foo")) {
         ....
      }
  }
}

Here, the isWithinWhile() method is something I just made up...I haven't 
had a need for it yet, but
would create it if needed. The getContainingFunction() method is real. 
It looks backwards
(while balancing matching braces) for something that looks like a 
function declaration: a "{"
preceeded by a ")", with a matching "(" that's preceeded by an ID.

Crazy, I know :)

Andy


More information about the antlr-interest mailing list