[antlr-interest] Lexer matching non-matching rule

Micha micha-1 at fantasymail.de
Sun May 17 03:48:06 PDT 2009


On Sunday 17 May 2009 11:01:36 Jesper Larsson wrote:
> > If you want the longest match, then left factor everything and let it
> > do that:
> >
> > A ( B (C|) |) ;
> >
> > And set the token type at the appropriate points.
>
> Not always so easy, however. My original example was, even more
> simplified, something like this:
>
> FOO:    'foo';
> BAR:    'bar';
> FOOZ:   'foo'* 'z';
>

that works with the method mentioned earlier:

start	:	( FOO { System.out.println("foo, ");} 
                | FOOZ { System.out.println("FOOZ, ");} )* EOF;

WS	:	(' ' | '\t' | '\n')+ { skip(); };

fragment FOO	: ;

fragment FOOZ: ;
	
FOO_OR_FOOZ
	:	(
			'foo' { $type = FOO; }
			('foo'* 'z' { $type = FOOZ;} )?	
		) 
		| 'z' { $type = FOOZ;};
		

 Michael



More information about the antlr-interest mailing list