[antlr-interest] Equivalent of TOKEN{x,y}

Jim Idle jimi at temporal-wave.com
Wed Nov 11 11:40:44 PST 2009

Just add counters in the parser rules (scope probably), let there be any number of them, then issue errors if the counts are wrong. You really don't want to do things like this at the grammar level - the messages you will issue will be confusing to users "Error at 'y', expecting '}'" leaves you to guess the meaning. It is generally better to produce a CommonTree, verify it and output whatever you need. Every task is different though.

You can also use gated predicates or ordinary predicates perhaps, but you will end up with a messy grammar that isn't as easy to deal with.


> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Kaleb Pederson
> Sent: Wednesday, November 11, 2009 11:35 AM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] Equivalent of TOKEN{x,y}
> Is there an easy way to enforce in the grammar that a lexical token
> must be present a certain number of times?
> For example, with TOKEN{x,y} as you might see in an extended regular
> expression, it would require that TOKEN be present at minimum x times
> and at most y times.
> I know I can easily check during semantic analysis, but in this case
> I'd prefer to do so at the grammar level.
> Thanks.
> --
> Kaleb Pederson
> Blog - http://kalebpederson.com
> Twitter - http://twitter.com/kalebpederson
> 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