is there a way to attach my lexer to the generated parser?

18 views
Skip to first unread message

konstantin tantin

unread,
Sep 14, 2017, 1:05:15 PM9/14/17
to antlr-discussion
I have a transition table driven lexer similar to that described in the "Dragon Book". I need to use it with ANTLR4 parser instead of a generated lexer. Are there any examples showing how to implement a custom lexer?

Thanks!
Konstantin

Mike Lischke

unread,
Sep 15, 2017, 4:20:59 AM9/15/17
to antlr-di...@googlegroups.com
I have a transition table driven lexer similar to that described in the "Dragon Book". I need to use it with ANTLR4 parser instead of a generated lexer. Are there any examples showing how to implement a custom lexer?

This should not be a big problem. In your grammar (you need a split grammar in this case) you can define a base class to be used for your generated lexer:

options {
superClass = MyLexer;
}

In that lexer you have to implement the same interface as the standard lexer class provides, so that the parser can work with it. Most important is the `nextToken()` method, but a number of others must be supported as well, to make everything working. You can implement them step by step as needed to compile everything, by looking at the original implementation to see how it's done there.

Mike

konstantin tantin

unread,
Sep 15, 2017, 2:05:11 PM9/15/17
to antlr-di...@googlegroups.com
Thank you. I am working with the Javascript target. Is `superClass` option used there as well? How does this work in Javascript?

Mike Lischke

unread,
Sep 15, 2017, 4:02:22 PM9/15/17
to antlr-di...@googlegroups.com
Thank you. I am working with the Javascript target. Is `superClass` option used there as well? How does this work in Javascript?

Forget about the superClass option. That was nonsense. If you would use a grammar to generate a lexer, you wouldn't use your own.

The correct answer is in the second part of my reply. Change your custom lexer so that it provides the same interface as the one from ANTLR. It doesn't matter which target language you use, the general class structure in the runtime is the same. You could also wrap your custom lexer with a class that provides the interface and uses your custom lexer as source. The details depend on how your custom class is structured. Maybe you can also use it as token source instead of a full lexer.

konstantin tantin

unread,
Sep 16, 2017, 1:14:46 PM9/16/17
to antlr-discussion


пятница, 15 сентября 2017 г., 16:02:22 UTC-4 пользователь Mike Lischke написал:

Thank you. I am working with the Javascript target. Is `superClass` option used there as well? How does this work in Javascript?

Forget about the superClass option. That was nonsense. If you would use a grammar to generate a lexer, you wouldn't use your own.

In the generated Javascript code, the name that I give for the superClass option is used in the import statement that imports the lexer. So it must not  be complete nonsense...

Reply all
Reply to author
Forward
0 new messages