[antlr-interest] Lexing problem I cannot resolve

Loring Craymer lgcraymer at yahoo.com
Wed Aug 6 10:18:03 PDT 2008


Try
fragment DIGIT: '0'..'9';
fragment DOTDOT : '..' ;
RANGE: DOTDOT ;
INT
   : DIGIT+ 
    (     (DOTDOT) => |
          ( '.' DIGIT* { $type = FLOAT; } )? )
   ;
   
fragment FLOAT : DIGIT;     

and then editing to see how close you can get to the original.  The DOTDOT takes advantage of ANTLR 3's lack of FOLLOW sets (INT does not look for a '.' when evaluating the synpred).  Otherwise, your last version is technically correct.

--Loring



----- Original Message ----
> From: Carter Cheng <carter_cheng at yahoo.com>
> To: antlr-interest at antlr.org
> Sent: Wednesday, August 6, 2008 8:37:54 AM
> Subject: Re: [antlr-interest] Lexing problem I cannot resolve
> 
> Thanks for the reply. You are quite correct I added the ? to the lexer rule. 
> Unfortunately it still complains of a no viable alt exception on the '.'. I 
> reduced the grammar to this to try to locate the problem. I ran it through 
> AntlrWorks and still seems to return a FLOAT token for "1..". Hopefully I am 
> still in error here somehow :-).
> 
> Thanks again,
> 
> Carter.
> 
> 
> 
> --- On Wed, 8/6/08, Gavin Lambert wrote:
> 
> > From: Gavin Lambert 
> > Subject: Re: [antlr-interest] Lexing problem I cannot resolve
> > To: carter_cheng at yahoo.com, antlr-interest at antlr.org
> > Date: Wednesday, August 6, 2008, 4:23 AM
> > At 15:06 6/08/2008, Carter Cheng wrote:
> >  >I apologize for the delayed reply here but I have been
> > trying
> >  >various things to try to get this to work without much
> > luck. I 
> > have
> >  >a simpler case which is a part of my grammar which is-
> > [...]
> >  >INTEGER_V    
> >  >    : Digit+ ( ('..') => | '.' Digit*
> > { $type = FLOAT_V; } )
> >  >    ;
> > 
> > You're missing a ?.  Right at the end of the rule you
> > need a ? 
> > after the closing parenthesis, since Digits are not
> > necessarily 
> > followed by any kind of dot.
> > 
> > INTEGER_V    
> >    : Digit+ ( ('..') => | '.' Digit* {
> > $type = FLOAT_V; } )?
> >    ;
> > 
> > Follow through each part of that and you should see how it
> > works:
> >    - first match a sequence of one or more Digits
> >    - optionally followed by one of:
> >      - if you see two dots in lookahead: nothing
> >      - otherwise, match a dot and zero or more Digits and
> > call it 
> > a FLOAT_V
> > 
> > Without the ?, ANTLR will complain if it can't see a
> > dot following 
> > the initial Digits, since it doesn't have anywhere else
> > to go.



      



More information about the antlr-interest mailing list