Detailed example - msv2017/gebf GitHub Wiki
The following code defines grammar that produces expressions with the very same output for given test formula.
var lang = {
entry: "<expr>",
"<expr>": [
"<expr> <op> <expr>",
"<fn>(<expr>)",
"<var>",
"<const>"
],
"<op>": ["+", "-", "*", "/"],
"<fn>": ["sin", "cos",],
"<var>": ["x",],
"<const>": ["1", "2"],
bindings: {
sin: Math.sin,
cos: Math.cos,
}
};
// we are going to prove that given grammar can produce a formula sin(2*x)
var testFunc = x => 2 * Math.sin(x) * Math.cos(x);
var testPoints = [-1, -.9, -.8, -.76, -.72, -.68, -.64, -.4, -.2, 0, .2, .4, .63, .72, .81, .9, .93, .96, .99, 1];
var testResults = testPoints.map(x => testFunc(x));
var gebf = require('gebf');
var result = gebf(lang, testFunc, testPoints, testResults);
console.log(result);
After a while, it produces the following result:
[ 'sin(2 * x)', 'sin(x * 2)', 'sin(x + x)' ]