[antlr-interest] Lexer matching non-matching rule

Micha micha-1 at fantasymail.de
Sun May 17 04:56:31 PDT 2009


On Sunday 17 May 2009 13:22:37 Jesper Larsson wrote:
> On Sun, 2009-05-17 at 12:48 +0200, Micha wrote:
> > On Sunday 17 May 2009 11:01:36 Jesper Larsson wrote:
> > > 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:
>
> Hm, as far as I can tell your variant has exactly the same problems as
> mine. The following grammar, incorporating the trick from yours, can
> match "foobar" as FOO BAR but fails on "foofoobar":

*Argh*  you need the synpred again:


> ============================================
> grammar Y;
> options { output=AST; }

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

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

fragment
FOO	:	;

BAR	:	'bar';

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


 Michael


More information about the antlr-interest mailing list