Context-Free Grammars for ReasonML/Bucklescript
This repository contains in-progress Reason/Bucklescript bindings for Cfgrammar-tool.
Example
This is an example from Cfgrammar-tool written in Reason using bs-cfgrammar-tool:
open Cfgrammar;
let t = Types.t;
let nt = Types.nt;
let rule = Types.rule;
let exprGrammar = grammar([|
rule("E", [| nt("E"), t("+"), nt("T")|]),
rule("E", [| nt("T")|]),
rule("T", [| nt("T"), t("*"), nt("F") |]),
rule("T", [| nt("F") |]),
rule("F", [| t("("), nt("E"), t(")") |]),
rule("F", [| t("n") |])
|],"E" );
Parse.parse(exprGrammar, "n*(n+n)")
|> Parse.length > 0; //true
Parse.parse(exprGrammar, "n(n+n)")
|> Parse.length > 0; //false
let generator = generator(exprGrammar,~determinism=false);
let produced: string = generate(generator,21); // something like 'n*((n+(n)*n+n+n*n))*n'
Installation
- Install the bindings using
npm install --save bs-cfgrammar-tool
- Add the bindings to
bsconfig.json
:
{
"bs-dependencies": [
"bs-cfgrammar-tool"
]
}
Build
npm run build
Build and Watch
npm run watch
Current State & Todo
Unopinionated porting, functionality for both instantiating grammars, generating strings from the grammars, and verifying if a string reduces to the grammar. Code is still not completey cleaned or more flavored to Ocaml yet, still in progress and would gladly take contributions.