Skip to content

Instantly share code, notes, and snippets.

@DmitrySoshnikov
Created October 24, 2016 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DmitrySoshnikov/7372d30ee23c1ac58b260697da0e9e67 to your computer and use it in GitHub Desktop.
Save DmitrySoshnikov/7372d30ee23c1ac58b260697da0e9e67 to your computer and use it in GitHub Desktop.
calc-no-assoc.g.js
/**
* 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