[antlr-interest] Match token {n} times

The Researcher researcher0x00 at gmail.com
Sun Aug 28 07:33:01 PDT 2011


> On Sun, Aug 28, 2011 at 3:20 PM, Jonne Zutt <jonne.zutt.ml at gmail.com>
> wrote:
>
> > Dear all,
> >
> > Is it possible with antlr to match a token exactly n times?
> > Something like the following:
> >
> > ACCOUNT : NUMBER{8};
> >
>

Hi Jonne,

Bart is correct in that matching a exact quantity is not built in to ANTLR,
and most likely never will. Exact quantities is a feature of Regular
Expressions.

If you are willing to work with gated semantic predicates it can be done.

>From the grammars page http://www.antlr.org/grammar/list download the Java
.class file grammar and take a look at classfile.g; specifically rule for
constant_utf8_info.

I believe it is a older v 2.x grammar but will put you in the ball park.

With this I was able to parse binary files, but eventually took a different
route that didn't use ANTLR.

If you are going to be doing lots of these types of rules in a single
grammar then you should really step back and consider if you want to abuse
ANTLR, and instead take a different approach.

Good Luck,
Eric


More information about the antlr-interest mailing list