[antlr-interest] How to implement case insensitive keyword in antlr3

Thomas Brandon tbrandonau at gmail.com
Fri Aug 17 02:04:07 PDT 2007


On 8/17/07, Gavin Lambert <antlr at mirality.co.nz> wrote:
> At 12:00 17/08/2007, Hub Dog wrote:
> >         I am just porting an antlr v2 project to antlr3.The
> > keyword of language is  case insensitive.In the antlr v2, there
> > is a caseSensitiveLiterals option for lexer.
Or probably better, implement a case insensitive input stream. You
just need to override LA in CharStream, or better delegate to a passed
in CharStream and alter the handling of LA to return either lower or
upper case versions of the base streams return.
Token text is retrieved through the substring() method so will not be affected.

Tom.

> >But it seems there is not such an option in v3. Does someone know
> >how to implement case insensitive keyword  in antlr3 .
>
> The two main approaches I've heard mentioned for this are:
>
> 1. override the standard lexer matching and lookahead routines and
> get them to do case-insensitive matching
>
> 2. write your rules in this sort of format:
>
>    PRINT: ('P'|'p')('R'|'r')('I'|'i')('N'|'n')('T'|'t');

>
> (This last one is a bit of a hassle but even if you've got lots of
> tokens it's pretty easy to knock up a one-shot codegen program
> that takes a list of keywords and outputs something like the
> above.)
>
> Not that I've ever actually done either of these, since for the
> most part I'm only parsing languages I've invented myself, so I
> chose to make them case-sensitive :)
>
>


More information about the antlr-interest mailing list