Created
October 24, 2016 01:19
-
-
Save DmitrySoshnikov/7372d30ee23c1ac58b260697da0e9e67 to your computer and use it in GitHub Desktop.
calc-no-assoc.g.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Explicit handling of the operator associativity. See also implicit handling | |
* https://gist.github.com/DmitrySoshnikov/ab6ed4d41505c579cee9af759e77ead9 | |
* | |
* Calculator grammar for a parser in Python. | |
* | |
* syntax-cli -g calc-no-assoc.g.js -m LALR1 -o calcparser.py | |
* | |
* >>> import calcparser | |
* >>> calcparser.parse('2 + 2 * 2') | |
* >>> 6 | |
*/ | |
{ | |
"lex": { | |
"rules": [ | |
["\\s+", "# skip whitespace"], | |
["\\d+", "return 'NUMBER'"], | |
["\\*", "return '*'"], | |
["\\+", "return '+'"], | |
["\\(", "return '('"], | |
["\\)", "return ')'"], | |
] | |
}, | |
"bnf": { | |
"E": [["E + T", "$$ = $1 + $3"], | |
["T", "$$ = $1"]], | |
"T": [["T * F", "$$ = $1 * $3"], | |
["F", "$$ = $1"]], | |
"F": [["NUMBER", "$$ = int($1)"], | |
["( E )", "$$ = $2"]], | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment