[antlr-interest] NoViableAltException - Am I trying to do too much with the Lexer?

Gavin Lambert antlr at mirality.co.nz
Thu Aug 23 12:46:46 PDT 2007


At 06:58 24/08/2007, Kenny MacDermid wrote:
>Okay, I've got a solution that passes the tests, but I'm not a 
>fan of it.
>
>I've added to REALNUMBER:
>     | ('0' '..')=> '0' { $type = NUMBER; }
>
>and to NUMBER:
>      | ('0' '0'..'9')=> DIGIT+ { $type = REALNUMBER; }
>
>So now the rules are interdependent. Can anyone suggest a cleaner 
>solution?

The usual approach is to make one a fake token (declared in the 
tokens block) and put all the actual rule code into one rule 
(usually the simplest one), in a similar style to how you'd "parse 
it by eye".  In your case:

NUMBER
   :  DIGIT+
     ('.' DIGIT+ { $type = REALNUMBER; })?
   ;

(You'll need to expand on this to get the other cases you want to 
accept, but this should give you a starting point.) 



More information about the antlr-interest mailing list