Sign Up for Free

RunKit +

Try any Node.js package right in your browser

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.

maitreya v0.1.4

A generalized LR parser generator written in javascript

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free