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
class Assoc(*values)[source]

Bases: Enum

LEFT = 1
RIGHT = 2
infixl(name, op)[source]
Parameters:
Return type:

_InfixOperator | _PrefixOperator | _PostfixOperator | _AtomOperator

infixr(name, op)[source]
Parameters:
Return type:

_InfixOperator | _PrefixOperator | _PostfixOperator | _AtomOperator

prefix(name, op)[source]
Parameters:
Return type:

_InfixOperator | _PrefixOperator | _PostfixOperator | _AtomOperator

postfix(name, op)[source]
Parameters:
Return type:

_InfixOperator | _PrefixOperator | _PostfixOperator | _AtomOperator

atom(name, rule_str)[source]
Parameters:
Return type:

_InfixOperator | _PrefixOperator | _PostfixOperator | _AtomOperator

OperatorTable

An OperatorTable contains a list of precedence levels in ascending order.

alias of List[List[_InfixOperator | _PrefixOperator | _PostfixOperator | _AtomOperator]]

build_expr_parser(table, rule_name)[source]

Generate Lark grammar from the operator table.

Parameters:
  • table (List[List[_InfixOperator | _PrefixOperator | _PostfixOperator | _AtomOperator]])

  • rule_name (str)

Return type:

str