How to define a grammar - msv2017/gebf GitHub Wiki
Grammar definition
A text wrapped with <
and >
is considered as a token and terminal otherwise.
Tokens are expandable grammar expressions and terminals are not.
Terminals can be binded to actual javascript functions with bindings
key.
var lang = {
entry: '<entry>', // always must be defined
'<entry>': ['<sub1>', ... ,'<subN>'], // token and its sub-tokens
'<sub1>' : [...],
...
'<subN>' : [...],
bindings: {
sin: Math.sin,
cos: Math.cos,
}
};
Example
var lang = {
entry: '<expr>',
'<expr>: [
'<expr> <op> <expr>',
'x',
'1'
],
'<op>': ['+', '-'],
};
Can produce infinite expressions as follows:
1 + x
x - 1
x - 1 + x
x + x + 1 + 1
and so on