Make Parsers and DSLs with Jacob

2015-03-05 00:00:00 +0000 by Alex R. Young

Recently there have been several exciting projects that provide support for upcoming ES6 and ES7 features. One way of implementing parsers like this is with a context-free grammar. A popular way to do this is with Jison, but Gabriele Cannata sent in a new library called Jacob (GitHub: Canna71/Jacob, License: ISC, npm: jacob).

Jacob has a lexer and a parser. In that sense it's inspired by both Flex and Bison. If you want to learn about Flex and Bison, I found an article about writing a toy compiler with Flex, Bison, and LLVM. Jacob's lexer supports regular expressions and lexer actions. The parser supports simple LR parsing, Look-Ahead LR parsing, and canonical LR parsing. The grammar is specified with Extended Backus–Naur Form, which means you define a formal language based on how symbols can be combined into legal sequences.

This means you can define your language or DSL, validate code written with it, and produce abstract syntax trees that can be used to write compilers.

I've written very basic grammar files with Jison before, and I've found that it takes practice to get the hang of it. The best place to get started is with real examples, so have a look at the Jacob examples folder. Also, the JSON definition on the json.org site will help you to understand how something you're familiar with maps to a formal grammar.

If you've ever wondered how people write things like transpilers in a formal and structured way, then it's worth checking out Jison and Jacob.