[antlr-interest] Question on the protected keyword in lexer

Martin Probst mail at martin-probst.com
Tue Jul 5 02:14:45 PDT 2005


Hi,

A slightly faster way would probably be:

-- snip --
tokens {
  INT,
  FLOAT
}
-- snip --
NUMBER:
  (DIGIT)+ { $setType(INT); }  ( '.' (DIGIT)+ { $setType(FLOAT); } )?;

protected DIGIT:
  '0'..'9';

... in which case you avoid the backtracking you'd have to do with your
implementation (e.g. with your code each time an integer is parsed an
exception is thrown, caught, and the integer is re-parsed, which is
unnecessary).

You can easily extend the above to also allow doubles (a la 0.4132e-13)
and signed numbers.

Regards,
Martin



More information about the antlr-interest mailing list