[antlr-interest] Filtering Java expressions

Bart Kiers bkiers at gmail.com
Sat Aug 7 12:16:24 PDT 2010


Forgot to reply to the entire group...


On Fri, Aug 6, 2010 at 8:03 PM, Bill Andersen <bill.andersen at mac.com> wrote:

>
> Is there a good way to do a partial parse of Java code to pick out
> declarations (or any other structure).  If it helps, I'm limiting this to
> pretty vanilla Java assignment and declaration expressions - no class defs.
>
>

You could create a lexer that parses only those tokens you define by adding:

@options { filter = true; }


at the top of your lexer.

For example, the following lexer:

lexer grammar Foo;

@options { filter = true; }

Int : '0'..'9'+ ;


will only create tokens consisting of one or more digits and will ignore the
rest of the input.

Note that you cannot (!) create a combined grammar (parser & lexer), only a
lexer-grammar.

Good luck!

Regards,
Bart.


More information about the antlr-interest mailing list