[antlr-interest] v3: Example of using tokens and $type?

Kay Roepke kroepke at classdump.org
Fri Mar 2 03:47:29 PST 2007

Hi Mark!

On Mar 2, 2007, at 7:29 AM, Mark Mandel wrote:

> I've got a situation in my grammar in which I want to be able to  
> change the type of the Node depending on certain parser conditions.

This sounds suspiciously like you are trying to influence the lexer  
from the parser. I'd reconsider that, because it often leads to nasty  
entanglement between the two.

> I had thought it was going to be as simple as defining a token  
> within a tokens{} block, and then writing a small piece of  
> { $type=mynewToken } and I would be okay, but it looks like my  
> assumptions were poorly based on fact.

No, you are quite right, that's the way to do it (unless you want to  
call emit() to build the token yourself).
Using $type = MyToken; in an action allows ANTLR to correctly  
translate the $type to the target language. Right now that
means it will end up in the Java code as { _type = MyToken; } but  
that name might change or differ between targets. Hence the $type  

> Is there anywhere I can look that gives a clear explanation /  
> example of how to correctly use tokens and the usage of $type.  I  
> want to make sure I'm not barking up the wrong tree here.
> (I'm going to get the strange feeling that I'm going to get a  
> repsonse of 'grab a copy of the book')

While that's a very good idea, there's still the list ;)


Kay Röpke

More information about the antlr-interest mailing list