Tutorial :Is there a framework for defining parsers in JavaScript?



Question:

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


Solution:1

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.


Solution:2

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.


Solution:3

This is not exactly what you need but this article goes through making a recursive descent parser in javascript for javascript itself.


Solution:4

PEG.js could be one solution.

PEG.js is a simple parser generator for JavaScript that produces fast parsers with excellent error reporting. You can use it to process complex data or computer languages and build transformers, interpreters, compilers and other tools easily.

Features

Simple and expressive grammar syntax Integrates both lexical and syntactical analysis Parsers have excellent error reporting out of the box Based on parsing expression grammar formalism â€" more powerful than traditional LL(k) and LR(k) parsers Usable from your browser, from the command line, or via JavaScript API


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »