[antlr-interest] Case insensitive reserved words.

Sam Harwell sharwell at pixelminegames.com
Fri Mar 20 09:43:18 PDT 2009


Performance will suffer in a big way. Use a class like the following for
your input.

 

Sam

 

public class CaseInsensitiveString : ANTLRStringStream

{

    // the string used for lookahead (performance improvement by not
having to call char.ToLowerInvariant())

    string _lastring;

 

    public CaseInsensitiveString( string input, string sourceName )

        : base( input, sourceName )

    {

        _lastring = input.ToLowerInvariant();

    }

 

    public override int LA( int i )

    {

        if ( i == 0 )

            return 0;

 

        if ( i < 0 )

        {

            // e.g., translate LA(-1) to use offset i=0; then
data[p+0-1]

            i++;

            if ( ( p + i - 1 ) < 0 )

            {

                // invalid; no char before first char

                return CharStreamConstants.EOF;

            }

        }

 

        if ( ( p + i - 1 ) >= n )

        {

            return CharStreamConstants.EOF;

        }

        return _lastring[p + i - 1];

    }

}

 

-----Original Message-----
From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Peter C. Chapin
Sent: Friday, March 20, 2009 8:48 AM
To: ANTLR Mailing List
Subject: [antlr-interest] Case insensitive reserved words.

 

 

Hi again...

 

My new project entails building a parser for a language with case

insensitive reserved words. I see the page here:

 

http://www.antlr.org/wiki/pages/viewpage.action?pageId=1782

 

describing how to write a custom look-ahead to deal with this. However,

David Haubenstricker commented on that page with an alternative
approach:

 

SELECT : S E L E C T

 

fragment A: ('A'|'a')

fragment B: ('B'|'b')

fragment C: ('C'|'c')

... etc.

 

To be honest, David's approach looks pretty good to me. I'm wondering if

there are any disadvantages to it that I should know about.

 

Thanks!

 

Peter

 

List: http://www.antlr.org/mailman/listinfo/antlr-interest

Unsubscribe:
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090320/b6c518e0/attachment.html 


More information about the antlr-interest mailing list