Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a framework for defining parsers in JavaScript?

Is there a JavaScript framework that allows to define a parsing grammar using JavaScript syntax, similar to the way Irony does it for C#?

like image 588
Andrey Shchekin Avatar asked Jun 23 '09 23:06

Andrey Shchekin


2 Answers

I have built a JavaScript Parsing DSL called Chevrotain.

Source: https://github.com/SAP/chevrotain

Online Playground: http://sap.github.io/chevrotain/playground/

It is not a Parser combinator like Irony, but it is very similar as it allows you to "define a parsing grammar using JavaScript syntax" without any code generation phase.

Using it is similar to "hand building" a recursive decent parser, only without most of the headache such as:

  • Lookahead function creation (deciding which alternative to take)
  • Automatic Error Recovery.
  • Left recursion detection
  • Ambiguity Detection.
  • Position information.
  • ...

as Chevrotain handles that automatically.

like image 139
bd82 Avatar answered Oct 11 '22 02:10

bd82


I don't know much about how Irony works, but Chris Double has a library that lets you define grammars in JavaScript here: http://www.bluishcoder.co.nz/2007/10/javascript-parser-combinators.html. The code is available on GitHub.

It's a "parser combinator" library which means you combine parsers for each production in your grammar into a larger parser that parses the whole thing. Each "sub-grammar" is a just a function that you create by calling the library functions.

like image 27
Matthew Crumley Avatar answered Oct 11 '22 02:10

Matthew Crumley