How do I make ANTLR build a lexer that uses my own token class?

Ric Klaren

You can set the factory used to generate Tokens with a call to setTokenObjectFactory. Check out the java/C++ file for CharScanner. Or, tell the lexer what class you want to use:

public static void main(String[] args) {
  InstrLexer lexer =
    new InstrLexer(new DataInputStream(;

Caveat: you probably have to do a lot of casting in your grammar actions.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact