Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

antlr4 parser re-use and warm up

Tags:

antlr4

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?

like image 626
FreeJack Avatar asked Mar 06 '13 00:03

FreeJack


1 Answers

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.

like image 171
Sam Harwell Avatar answered Sep 24 '22 21:09

Sam Harwell