[antlr-interest] need some advice

Bart Kiers bkiers at gmail.com
Fri Apr 9 02:50:23 PDT 2010


On Fri, Apr 9, 2010 at 10:56 AM, Dietmar Schaefer <dietmar-s at online.de>wrote:

> ...
> DefSequence L { -- }   works
>
> DefSequence L { A1200}  gives me MismatchedTokenException: line 1:16
> mismatched input 'A1200' expecting '\u0006'
>

When stumbling upon the 'A' from 'A1200', the lexer tries to complete an
ID-rule. Try moving the VALUE-rule before the ID-rule:

grammar Test;

scenario
  :  statement
  ;


statement
  :  sequenceStatement
  ;

sequenceStatement
   :  'DefSequence' ID '{' VALUE '}' ';'?
   ;

VALUE
  :  ('A' | 'B' | 'C') DIGIT DIGIT DIGIT DIGIT
  |  ('-')*
  ;

ID
  :  LETTER (LETTER | DIGIT | '_')*
  ;

fragment
DIGIT
  :  '0'..'9'
  ;

fragment
LETTER
  :  'a'..'z' | 'A'..'Z'
  ;

WHITESPACE
  :  ('\t' | ' ' | '\n' | '\r')+ {$channel = HIDDEN;}
  ;

But note that your ID's can then not start with 'A', 'B' or 'C'! If you want
that, look into semantic predicates:
http://www.antlr.org/doc/glossary.html#Predicate,_semantic

Regards,

Bart.


More information about the antlr-interest mailing list