Bad advice when working with ANTLR

Your attention is invited to a set of harmful tips that will help you create the most productive parser. Do not forget to do everything according to the instructions, otherwise something will go wrong!

1. ATN - not a bug, but a feature


Terrence Par did a great feature for optimizing branch prediction in your grammar. Exclusively out of concern for the performance of your parser, the field is static and will accumulate information forever! Of course, you will not notice much difference on small files, the main thing is not to parse files of 2-3 megabytes in your tests, even if there is not enough memory for someone else!

2. Lookahead for those who are not used to looking back at explosions!


If your grammar checks all tokens from the stream on each rule, then you can guarantee the accuracy of processing! If there is not enough stack, then it is enough to increase its size tenfold, the main thing is to make more rules!

3. SLL for wimps


Why do optimization if ANTLR4 can do it for you? Grammar in ALL mode saves you valuable time reading such articles. Remember to add the same tokens to each alternative, especially if it looks beautiful.

4. AST - all the information you have


Do not forget to listen to Terrence and add visitor, listener support to your parser. Embedding code in a grammar is not only unnecessary difficulties, but also a potential problem with refactoring! Bypassing every node in the tree, you can not only try your luck, but also check the customer’s nerves!

5. More rules, god of rules!


But did you know that with the help of ANTLR4 you can build a tower of Babel? In the code sections with a large nesting can not be found? A function inside a function with a huge amount of expressions? Just do not show the customer the work on minified js files, anyway, no one understands them, even browsers.

6. The lexer is only needed to create tokens.


Under no circumstances try to do something with the tokens in the lexer, packing the {} block in the token will not allow you to enjoy viewing the customer’s machines that require more memory!

Memory must flow! May memleak be with you!

Source: https://habr.com/ru/post/undefined/


All Articles