[antlr-interest] Re: caseSensitive for one rule

antlrlist antlrlist at yahoo.com
Wed Jul 16 10:19:31 PDT 2003


Hello!

If you're trying to turn CaseSensitive *on some parser rules* I cannot
help you - the AssertLowerCase solution is the best one I can think of.

However, if you want to turn CaseSensitive on and off on some *tokens*
you should modify the *Lexer*, not the *Parser*.

( Sorry if I sound rude. Sometimes when I try to stress something it
seems I'm angry - this is not the case :) )

Namely you should turn off CaseSensitive (or CaseSentiviteLiterals;
that would be more efficient, but maybe not enough powerful) and then
override testLiteralsTable in your lexer (I'll asume java mode):

<pre>
options {
caseSensitiveLiterals=false; // all literals are case-insensitive
testLiterals=false; // Activate in IDENT
}
tokens{
   END="end";     // case insensitive
   START="start"; // case *sensitive*
   FOO="foo";     // case *sensitive*
   ...
}

{
    /** override testLiteralsTable **/
    public int testLiteralsTable(String text, int oldtype)
    {
        int newtype = super.testLiteralsTable(text, oldtype);
        switch(ttype)
        {
           // case sensitive tokens
           case START : case FOO: case ... :
              // return IDENT-or-whatever if is not
              // completely lowercase (this could be more refined)
              if(isLowerCase(text)) return oldtype;
              return newtype;
           // case insensitive tokens
           default:
              return newtype;
        }
    }
}
...

// Surely you'll have an IDENT-like rule, where reserved words have to
// be recognized. 
IDENT
:  options { testLiterals=true; }: 
   LETTER (LETTER|DIGIT)*
;
</pre>

I have not tried this out, but it should work fine. Please report!

Enrique.

PS: as a side note, I don't like using unnamed tokens - writing
"start" in parser rules instead of START - with this solution you
might not be able to use them.


--- In antlr-interest at yahoogroups.com, "Wirth. Michael" <M.Wirth at s...>
wrote:
> Hi,
> 
> is there a way to set caseSensitive to false in one rule in the parser?
> Something like I wrote below.
> I want that the "end", "End", "eNd", ... ist matched. Only in this rule.
> "start" should only be matched as "start", not as "Start".
> 
> 
> ruleOne : "start" ruleTwo ;
> 
> ruleTwo :	
>         options 
>         {	
>             caseSensitive = false;
>         }:
>         tok1:"end"
>     ;
> 
> Is this possible?
> 
> 
> Greetings,
> 	Michael


 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 




More information about the antlr-interest mailing list