[antlr-interest] a simple (not for me :)) grammar problem

Gavin Lambert antlr at mirality.co.nz
Mon Jan 7 16:53:01 PST 2008


At 01:24 8/01/2008, I wrote:
 >Try doing what I suggested.  You really should handle the floats 

 >in the lexer, since you don't have to worry about whitespace
 >weirdness that way.  And if you do it the way I said, it should
 >work.

Ok, I was a little overly optimistic there; there was still a bit 
of k=1 lookahead that I hadn't accounted for that broke it.  But 
it works if you use a syntactic predicate:

grammar Sample;

tokens {
   FLOAT;
}

start: (FLOAT | INTEGER) DOT IDENTIFIER;

INTEGER
	:	NUMBER
		(	(DOT DIGIT) => DOT NUMBER { $type = FLOAT; }
		|	/* nothing */
		)
	;

IDENTIFIER: LETTER+;
DOT: '.';
fragment NUMBER: DIGIT+;
fragment LETTER: 'a' .. 'z';
fragment DIGIT: '0' .. '9';



More information about the antlr-interest mailing list