prodigy.pgcl.util.lark_expr_parser¶
Lark Expression Parser¶
Generate Lark grammars for expressions from operator tables. The algorithm is pretty simple, but easy to mess up when doing it by hand.
>>> table = [
... [infixl("plus", "+"), infixl("minus", "-")],
... [prefix("neg", "-"), atom("parens", '"(" test ")"'), atom("int", "/[0-9]+/")]
... ]
>>> print(build_expr_parser(table, "test"))
?test: test_0
?test_0: test_1
| test_0 "+" test_1 -> plus
| test_0 "-" test_1 -> minus
?test_1: "-" test_1 -> neg
| "(" test ")" -> parens
| /[0-9]+/ -> int