When is the best time to use a parser like ANTLR instead of writing your own parsing code?

I need to parse a simple DSL that looks like this:

funcA Type1 a (funcB Type1 b) ReturnType c 

Since I have no experience with grammar analysis tools, I thought it was faster to write a basic parser (in Java).

Would it be better, even for a simple DSL, for me to use something like ANTLR and build the correct grammar definition?

+4
source share
3 answers

The simple answer is: when is it easier to write rules that describe your grammar than to write code that accepts the language described by your grammar.

If the only thing you need to parse looks the same as what you wrote above, then I would say that you could just write it by hand.

More generally, I would say that most common languages can be parsed faster manually (using a regular expression).

If you understand a context-free language with many rules and productions, ANTLRs (or other parser generators) can make life much easier.

Also, if you have a simple language that you expect to become more complex in the future, it will be easier to add rule descriptions to the ANTLR grammar than to create them in a hand-held encoder.

+3
source

Grammars tend to evolve (as do requirements). Home brew partners are hard to maintain and lead to the reinvention of the wheel example. If you think you can write a fast parser in java, you should know that it would be faster to use any of the lex / yacc / compiler-compiler solutions. It’s easier for lexers to write, then you need your own rule priority semantics, which are not easy to check or save. ANTLR also provides the idea of ​​ASTA visualization, you can defeat this assistant. An additional benefit is the ability to generate intermediate code using string patterns, which is completely different.

+4
source

It is better to use a ready-made parser (generator), such as ANTLR, when you want to develop and use your own language. It’s better to write your own parser when your task is to write a parser.

If you have a lot of experience writing parsers, you can get a working parser faster than using ANTLR. But I guess you asked the question that this get-out clause does not apply.

+2
source

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


All Articles