On Wed, 2008-09-17 at 21:51 -0700, Matthieu Riou wrote:
> Hi,
> 
> I'm trying to write a grammar where 2.3 would be a float style number
> but where the same group of characters in foo.2.3 would be recognized
> as two distinct numbers (like foo[2][3]). I changed my number lexer
> rule to be:
> 
> NUM       : ~'.' DIGIT+ ('.' DIGIT+)? ;
> 
> However this doesn't seem to work, strangely expressions like [2,3,4]
> get rejected with a no viable alternative at input ',3'. Does someone
> have a clue why and how I should tweak my rules to do what I want?


You are going to have to keep state in the lexer, somethign like this:

@lexer::members
{
   boolean returnInt = false;
}
ID : ('a'..'z')+

        (
              ('.' ('0'..'9'))=> { returnInt = true; }
            |
        )
;

DOT : '.';
NUM : ('0'..'9')
          (
             {returnInt == true}?=>
                      (
                            ('.' ('0'..'9'))=> { returnInt = true; }
                          | {returnInt = false; }
                      )
          )
;

And so on, but it will get a lot more complicated. If this is your
language, then don't do this, if it is not, then shoot the designer ;-)

Other approaches might be this kind of thing:

ID : ('a'..'z')+ { input.mark(); }

         (  ('.' ('0'..'9'))=> '.' ('0'..'9')+ { elementCount++; } )*
        { input.release(); }
;

DOT : '.' ;
NUM :  {elementCount > 0}?=> ('0'..'9')+ { elementCount--; };
 

Or you could override the emit() method and return more than one token
per lexer rule invocation.

Take a look at wiki FAQ on parsing numerics, and the article on emitting
more than one token from a lexer rule.

Jim


> 
> Thanks,
> Matthieu
> 
> 
> List: http://www.antlr.org:8080/mailman/listinfo/antlr-interest
> Unsubscribe: 
> http://www.antlr.org:8080/mailman/options/antlr-interest/your-email-address
> 

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en
-~----------~----~----~----~------~----~------~--~---

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

Reply via email to