Python Lark 解析器:我安装的所有版本似乎都没有 .pretty() 打印方法

Python Lark parser: no versions I've installed seem to have the .pretty() print method

问题:

# From example at https://github.com/lark-parser/lark/blob/master/examples/json_parser.py
from lark import Lark, Transformer, v_args
parse = json_parser.parse
json_grammar = r""" ... """
### Create the JSON parser with Lark, using the LALR algorithm
json_parser = Lark(json_grammar, parser='lalr',
                   # Using the standard lexer isn't required, and isn't usually recommended.
                   # But, it's good enough for JSON, and it's slightly faster.
                   lexer='standard',
                   # Disabling propagate_positions and placeholders slightly improves speed
                   propagate_positions=False,
                   maybe_placeholders=False,
                   # Using an internal transformer is faster and more memory efficient
                   transformer=TreeToJson())

with open(sys.argv[1]) as f:
    tree = parse(f.read())
    print( tree )
    # Errors next 2 lines:
    # No: tree.pretty( indent_str="  " )
    # No: Lark.pretty( indent_str="  " )

具体错误:

设置:

Python版本=3.8.1

在 Mac Bug Sur

上的 Miniconda 3
conda install lark-parser

Installed 0.11.2-pyh44b312d_0

conda upgrade lark-parser

Installed 0.11.3-pyhd8ed1ab_0

编辑:注意我的目标:

这里的目标不仅仅是解析 JSON;我只是碰巧使用 JSON 示例来尝试学习。我想为我在工作中处理的一些数据编写自己的语法。

编辑:为什么我相信漂亮的印刷品应该存在:

这是一个使用 .pretty() 函数的示例,甚至包括输出。但我似乎找不到任何包含 .pretty(): http://github.com/lark-parser/lark/blob/master/docs/json_tutorial.md

的内容(至少通过 conda)

Lark 示例目录中的 JSON 解析器使用树转换器将解析后的树转换为普通的 JSON 对象。这使得通过将其与 Python 标准库中的 JSON 解析器进行比较来验证解析是否正确成为可能:

    j = parse(test_json)
    print(j)
    import json
    assert j == json.loads(test_json)

最后的assert只有在parse返回的值和json.loads返回的对象类型相同的情况下才能通过,这是一个普通的朴实无华的[=25] =] 内置类型,通常是 dictarray.

您可能会发现 pretty printer in the Python standard library useful for this particular application. Or you could use the builtin JSON.dumps 函数具有非零 indent 关键字参数。 (例如:print(json.dumps(json_value, indent=2))

我不确定我可以在这个答案中输入其他答案中没有的内容。我将尝试创建相应的示例:

json_parser = Lark(json_grammar, parser='lalr',
                   # Using the standard lexer isn't required, and isn't usually recommended.
                   # But, it's good enough for JSON, and it's slightly faster.
                   lexer='standard',
                   # Disabling propagate_positions and placeholders slightly improves speed
                   propagate_positions=False,
                   maybe_placeholders=False,
                   # Using an internal transformer is faster and more memory efficient
                   transformer=TreeToJson()
)

这里重要的一行是 transformer=TreeToJson()。它告诉 lark 在将 Tree 返回给您之前应用 Transformer class TreeToJson。如果删除该行:

json_parser = Lark(json_grammar, parser='lalr',
                   # Using the standard lexer isn't required, and isn't usually recommended.
                   # But, it's good enough for JSON, and it's slightly faster.
                   lexer='standard',
                   # Disabling propagate_positions and placeholders slightly improves speed
                   propagate_positions=False,
                   maybe_placeholders=False,
)

然后用.pretty方法得到Tree实例:

tree = json_parser.parse(test_json)
print(tree.pretty())

然后您可以手动应用 Transformer

res = TreeToJson().transform(tree)

现在这是一个 'normal' python 对象,就像您从 stdlib json 模块中得到的一样,所以可能是一个 dictonary.

Lark 构造的 transformer= 选项使得它可以在创建树之前完成,从而节省时间和内存。