1
votes

I am attempting to use Antlr to tokenize and classify the tokens of an input stream. Does anyone know of a way to generate only a Lexer from Antlr using a grammar with only Lexer rules?

2

2 Answers

1
votes

You can specify the type of grammar you want using the grammar title line.

grammar MyGrammar;

for combined grammars.

lexer grammar MyLexer;

for a lexer grammar (etc.). Of course in a pure lexer grammar you may only use lexer rules.

0
votes

You can basically generate a parser and extend the listener class then inside every exitMethod() push the tokens into a stack.

You can't generate only a lexer. If you are not familiar with ANTLR 4 grammar or the steps required to generate a parser, I advise you to spend 10 mins in reading this book "The definitive of ANTLR 4".