[antlr-interest] define two tokens with the same allowed characters

Vaclav Barta vbar at comp.cz
Tue Oct 2 23:39:24 PDT 2007


On Wednesday 03 October 2007 01:24:25 OJAY78 at gmx.de wrote:
> My problem is that I do not know how can I have two tokens which could
> contains the same characters. I have a rule ID with all the allowed
> characters, this one will be used correctly but after that rule I define
> ID2 with the same characters and when I try to generate my grammar the
> compiler recognizes that the ID2 token will never be reached.
>
> So can anyone help me, how can I do that correctly.
Have one rule for both tokens and assign the token type explicitly in its 
action, i.e. (from http://www.antlr.org/grammar/1152141644268/Java.g )

ENUM:	'enum' {if ( !enumIsKeyword ) $type=Identifier;}
	;

That's presuming you can distinguish between ID and ID2 somehow, of course - 
if you can't, you probably shouldn't have different tokens.

	Bye
		Vasek


More information about the antlr-interest mailing list