[antlr-interest] Can't define rules with same name in different modes.

Peter Boughton boughtonp at gmail.com
Fri Jan 20 14:46:16 PST 2012


Using Antlr v4 to create a modal lexer, and been banging my head
against a wall wondering what was wrong for a while, before working
out the problem was due to two rules having the same name.

I'm still not sure if this is by design or a bug - if the former it
should probably be throwing an error when parsing the grammar.

See this grammar snippet:

	mode IN_ATTRIBUTES;
	TAG_CLOSE  : ENDCLOSE_TAG { popMode();  };
	TAG_END    : END_TAG  { popMode(); pushMode(IN_BODY); };
	...
	HASHEXPR : HASH   { pushMode(IN_HASHEXPR); };

	mode IN_BODY;
	BODY_END : CLOSE_TAG WORDCHAR+ END_TAG { popMode(); };
	HASHEXPR : HASH { pushMode(IN_HASHEXPR); };

The lexer kept throwing "token recognition error at: '#'" errors when
I was expecting it to match the second HASHEXPR rule.

After renaming the rules to ATTR_HASHEXPR and BODY_HASHEXPR
respectively it started behaving.

So if having two same named rules (in different modes) is not allowed,
I would expect an error to be thrown, (but if it is allowed then
there's a bug).


A follow-up question:
This HASHEXPR rule is going to crop up all over the place, in at least
half a dozen different modes, but it's not valid everywhere. Is there
a better way to define it, to be less repetitive and more readable?


More information about the antlr-interest mailing list