Codementor Events

Introducing Chevrotain - The best tool for writing parsers in Javascript.

Published May 16, 2019

I would to start my first post on Codementor by introducing all of you to a technology that has saved my time numerous times.

Maybe some of you who went through a university class about compilers know about the ammount of effort it goes into properly parsing an entire language syntax into an abstract syntax tree, from which you can generate the desired output or analyse the contents of the given source code.

This effort becomes even higher if parsing very large files is a requirement along with performance. If you ever tried creating a custom parser or even with the help of certain libraries in C/C++, you know its not a pleasant experience.

Thus, when I first had to write a proper parser in my job, I tried to investigate what technologies were there available in 2018 and if I could possibly do it with node.js, my favourite scripting language.

The first thing I tried was : PEG.js.

While writing the parser itself with the PEG.js syntax was a piece of cake, the performance results left much to be desired when it came to performance. I was implementing an IDE like tool to provide the user with snippets, suggestions, syntax errors, etc. The performance of PEG.js for large files simply did not have the needed power.

I was on the verge of considering implementing a hand-build parser when I found out about Chevrotain.

You see, Chevrotain is not a parser generator like PEG.js, it is instead a parser API. While building the parser is slightly harder and much more verboose, the performance is blazing fast, as can be proven by their online benchmark.

image.png

Thus, we can arrive at my conclusion. If you need a powerful parser and you want to develop it in Javascript, your best bet is Chevrotain.

Discover and read more posts from Ricardo Ferreira da Silva
get started