This is a general-purpose recursive descent parser and evaluator.
Using this module, You can:
- Defines a syntax rule such as programming languages, expressions, or structured statements.
- Parses the code written based on the rules.
- Evaluates the result of the parsing.
NOTE: The class
BNF exported in v0.3.3 is deprecated and it can be used as
The parser is defined as an instance of
Language class by using objects of BNF-like
notation and its evaluator functions. Those can run in dynamically and immediately.
Language constructor takes one array of syntax rule.
To create the syntax rule, use
- A name of the syntax rule.
- An array of a rule which is an array of terms.
- An evaluator function (optional). It can be omitted when the second parameter contains only one rule which containing only one other name of syntax rule.
Demo: Evaluating arithmetic expression
With following files, those shows how define a syntax of language.
A simple calculator script to parse and evaluate an expression from CLI.
An implementation of the calculator syntax.
run on bash
$ node sample/eval-expr.js '(1 + 2) * ((3 + 4) / 2)' 10.5 $
This software is released under the MIT License, see LICENSE