I’m using ANTLR4 and the official Kotlin G4 grammar (Grammar) to parse and analyze Kotlin source code.
Unfortunately the performance of this grammar can be fairly awful, even on simple inputs. For example, parsing a 20-arm if-else
statement takes around 500ms. I’m currently encountering a situation where parsing successful examples of a simple program we have students write in an introductory course can take several seconds.
I’m not an expert on ANTLR4, but my understanding is that the problem has to do with the structure of the grammar, which has a lot of pass-through rules that result in very deep nesting. I’ve done a bit of work previously to flatten portions to reduce memory usage and improve performance without affecting correctness. However, I’m wondering if anyone knows of or maintains an optimized Kotlin G4 grammar that is both correct and results in improved performance?