Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is better to use a parser such as ANTLR vs. writing your own parsing code?

Tags:

parsing

antlr

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

funcA Type1 a (funcB Type1 b) ReturnType c

As I have no experience with grammar parsing tools, I thought it would be quicker to write a basic parser myself (in Java).

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

like image 216
Viral Shah Avatar asked Mar 16 '10 17:03

Viral Shah


People also ask

Why ANTLR is used?

ANTLR (ANother Tool for Language Recognition) is a tool for processing structured text. It does this by giving us access to language processing primitives like lexers, grammars, and parsers as well as the runtime to process text against them. It's often used to build tools and frameworks.

What type of parser is ANTLR?

ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files. Terence Parr is a tech lead at Google and until 2022 was a professor of data science / computer science at Univ.

Why do we use parser?

Parsers are used when there is a need to represent input data from source code abstractly as a data structure so that it can be checked for the correct syntax. Coding languages and other technologies use parsing of some type for this purpose.

Is ANTLR used in industry?

ANTLR is a powerful parser generator that you can use to read, process, execute, or translate structured text or binary files. It's widely used in academia and industry to build all sorts of languages, tools, and frameworks.


2 Answers

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

If the only thing you need to parse looks exactly like what you've written above, then I would say you could just write it by hand.

More generally speaking, I would say that most regular languages could be parsed more quickly by hand (using a regular expression).

If you are parsing a context-free language with lots of rules and productions, ANTLR (or other parser generators) can make life much easier.

Also, if you have a simple language that you expect to grow more complicated in the future, it will be easier to add rule descriptions to an ANTLR grammar than to build them into a hand-coded parser.

like image 157
danben Avatar answered Sep 28 '22 04:09

danben


Grammars tend to evolve, (as do requirements). Home brew parsers are difficult to maintain and lead to re-inventing the wheel example. If you think you can write a quick parser in java, you should know that it would be quicker to use any of the lex/yacc/compiler-compiler solutions. Lexers are easier to write, then you would want your own rule precedence semantics which are not easy to test or maintain. ANTLR also provides an ide for visualising AST, can you beat that mate. Added advantage is the ability to generate intermediate code using string templates, which is a different aspect altogether.

like image 37
questzen Avatar answered Sep 28 '22 05:09

questzen