This is a playground
to test code. It runs a full Node.js
environment and already has all of npm
’s 1,000,000+ packages pre-installed, including maitreya
with all npm
packages installed. Try it out:
let {defineGrammar, def, ref, many1, regex, choice} = require('maitreya/lib/grammar');
let {GLRParser} = require('maitreya/lib/interpret');
let grammar = defineGrammar(() => {
def('exp', [ref('num')], ([num]) => num);
def('exp', [ref('exp'), ref('op'), ref('exp')], ([lhs, op, rhs]) => op(lhs, rhs));
def('num', [many1(regex(/^[0-9]/))], ([digits]) => Number(digits.join('')));
def('op', [choice('+', '-')], ([op]) => {
return {
['+'](lhs, rhs) { return lhs + rhs; },
['-'](lhs, rhs) { return lhs - rhs; }
}[op];
});
});
let parser = new GLRParser(grammar);
parser.feed('3+2-5');
console.log(parser.results);
This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.