Reusing the antlr4 parser and warming up

In my use case, I have to parse several thousand small and independent expressions in a tree view using Visitor in the generated parsing trees. Currently, new threads, lexers, and parsers are created for each parsing operation.

I suggest that this may not be optimal. What object instances could be reused in such a setting to use the ANTLR4 warm-up property? What about thread safety - which of these instances should be a local thread? Is reset required to reuse an instance of lexer or parser?

+8
antlr4
source share
1 answer

In the early days of ANTLR 4 (a few months before its initial release), an adaptive DFA cache was created based on each instance, so using Lexer.setInputStream or Parser.setInputStream was necessary to achieve good performance.

This is no longer the case. The background cache is now shared between all instances of the parser and is thread safe. Lexer and Parser class methods are not thread safe, so if you want to analyze multiple threads, you will need to create multiple instances of your lexer and parser.

+11
source share

All Articles