earley-cfg
v0.1.0
Published
An implementation of the a Context Free Grammar (CFG) using the Earley algorithm in Node.JS
Downloads
4
Maintainers
Readme
earley-cfg
An npm module containing an implementation of the a Context Free Grammar (CFG) using the Earley algorithm. This is my take on this NPM module, with changes to suit my needs.
Example
var earley = require('earley-cfg');
var grammar = new earley.Grammar('grammar.cfg');
var parser = new earley.Parser(grammar);
var sentence = new earley.Sentence.SentenceFromString('time/time<N> flies/fly<N>/fly<V> like/like<V>/like<P> an/a<D> arrow/arrow<N>');
var result = parser.parse(sentence);
if(result.valid === true){
console.log('Valid');
var trees = new earley.ParseTrees(result);
console.log(trees.toString());
}
else{
console.log('Invalid');
}
grammar.cfg
S -> NP VP | VP
NP -> D N | N | N NP
VP -> V | V NP | V PP | V NP PP
PP -> P NP
Download
Install using Node Package Manager (npm
):
npm install earley-cfg