Are parsers generated by ANTLR thread-safe? Can I parse multiple inputs concurrently?

Terence Parr

The simple answer is "yes", however, if you try to parse the same input using the same object with more than one thread the answer is "no". Fortunately, parsing is, for the most part, an inherently serial problem so you'd never want multiple threads playing with the same input stream object.

Typically this question is really asking, "can I make multiple instances of the same parser and parse multiple input streams at the same time?" Emphatically, "yes". There are not global variables or shared static variables that would force all parsers to operate at different times.

You can have multiple parsers and multiple lexers all operating on the same input stream (to handle complicated input streams like java/javadoc comments), but a single thread should weave its way through all of them.

None of the ANTLR-generated methods are synchronized, by the way.