View on GitHub

Tech, Games and Whisky

The Sebr's Blog

Language Parser

|

I wrote about Context Free Design Grammar a few days ago. This made me looked into implementation (both C++ and javascript) of CFDG. And this lead me to different parser generators. Granted writing a custom parser is fun and it is easy to write one for CFDG, I still think parser technology is interesting.

The venerable Bison

Bison is one of the first parser generators. It is an executable that is part of the GNU Operating System. You will need to write a Lexical analyzer and a Grammar specification. Running Bison will then produce a C++ parser. You will then need to link your program with the generated Bison parser.

Jison: Javascript Bison

Jison is really similar to Bison but it is all javascript based. It worked both for NodeJs and Browser. You will need to specify a Lexer and grammar file. It even uses the Bison Gramma files format (why break a winning recipe?). You will then end up with a javascript parser that can easily be used from your app. The parser will output an Abstract Syntax Tree.

Antlr

Antlr is yet ANother Tool for Language Recognition. It has the particularity of creating a “language walker” that will call your own custom code when language features are encountered (i.e. visitor pattern). The following mega tutorial is a wealth of information on how to use Antlr. Antlr is a java program and needs you to have a valid installation of Java 1.7 (why, oh why!). What is really great with Antlr is that it can output a parser in Python, JavaScript, Java and C#. That makes Antlr a really flexible tool. There is even a GUI that can be used to view the resulting AST.