In my use case I have to parse several thousand small and independent expressions into a tree representation using a Visitor on the generated parse trees. Currently new streams, lexer and parser instances are created for each parse operation.
I assume this might not be optimal. Which object instances could be re-used in such a setup to make use of the warm-up property of ANTLR4? How about thread safety - which of these instances should be thread local? Is a reset of some kind required to re-use a lexer or parser instance?
In the early days of ANTLR 4 (many months before its initial release), the adaptive DFA cache was created on a per-instance basis, so the use of Lexer.setInputStream
or Parser.setInputStream
was essential for achieving good performance.
This is no longer the case. The background cache is now shared among all parser instances and is thread safe. The methods of the Lexer
and Parser
classes are not thread safe, so if you want to parse on multiple threads, you will need to create multiple instances of your lexer and parser.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With