[antlr-interest] Can antlr v3 lex star | tristar properly?

Harald Mueller harald_m_mueller at gmx.de
Wed Nov 21 04:05:17 PST 2007


As Johannes pointed out, you can do manual left-factoring - but this somehow defies the reason to use ANTLR ...

A "somewhat less intrusive" way, which avoid the left-factoring, is the following:

stars        :        (STAR | TRISTAR)* EOF;
STAR : '*' ;
TRISTAR : 
   {input.LA(1)=='*' && input.LA(2)=='*' && input.LA(3)=='*'}? =>
   STAR STAR STAR;

{...}? is a semantic predicate; and the => forces checking the predicate as with syntactic predicates.

This way, you could keep any actions inside your grammar - at times a nuisance with left-factoring and a primary reason to use some tool to disambiguate input ...

Regards
Harald

-------- Original-Nachricht --------
> Datum: Wed, 21 Nov 2007 12:54:31 +0200
> Von: Guntis Ozols <guntiso at latnet.lv>
> An: antlr-interest at antlr.org
> Betreff: [antlr-interest] Can antlr v3 lex star | tristar properly?

> Is there a way to lex this simple grammar (I am using ANTLRWorks 1.1.4):
> 
> stars        :        (STAR | TRISTAR)* EOF;
> STAR : '*' ;
> TRISTAR : STAR STAR STAR;
> 
> For '**' I keep getting: mismatched character '<EOF>' expecting '*'.
> 
> I am out of options. Below is what I've tried:
> //tokens { STAR; TRISTAR;}
> //options { backtrack = true; }
> //options { k = 3; }
> //ST        :        ('***') => '***' {type = TRISTAR;}
> //        |        '*' {type = STAR;};
> //STAR options {greedy=false;}: '*' ;
> //TRISTAR : '***';
> //TRISTAR : ('***')=>'***';
> //TRISTAR options { greedy=true; } : '***';
> //TRISTAR options { greedy=true; } : ('***')=>'***';
> //TRISTAR options {greedy=false;} : '***';
> 
> Guntis
> 

-- 
GMX FreeMail: 1 GB Postfach, 5 E-Mail-Adressen, 10 Free SMS.
Alle Infos und kostenlose Anmeldung: http://www.gmx.net/de/go/freemail


More information about the antlr-interest mailing list