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

Terence Parr parrt at cs.usfca.edu
Sat Jan 21 09:59:59 PST 2012


adding to think more about.
T
On Jan 20, 2012, at 2:46 PM, Peter Boughton wrote:

> 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?
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address



More information about the antlr-interest mailing list