Python:创建语法并实现运算符

时间:2018-03-27 10:29:56

标签: python grammar bnf lark-parser

我尝试描述语法和运算符。

我使用下一个代码

from lark import Lark
json_parser = Lark(r""" ?start: value
                    ?value: dict
                          | array
                          | operator
                          | string
                          | SIGNED_NUMBER      -> number
                          | "true"             -> true
                          | "false"            -> false
                          | "null"             -> null

                    statement : "если" condition "то" result
                    condition : string operator string
                    result : string
                    array  : "[" [value ("," value)*] "]"
                    dict : "{" [pair ("," pair)*] "}"
                    pair   : string ":" value
                    !operator : "<" | ">" | "=" | ">=" | "<=" | "!="
                    string : ESCAPED_STRING


                    %import common.ESCAPED_STRING
                    %import common.SIGNED_NUMBER
                    %import common.WS
                    %ignore WS """, start='value')
text = '{"key": ["item0", "item1", 3.14, "!="]}'
json_parser.parse(text).pretty()

但它返回

'dict\n  pair\n    string\t"key"\n    array\n      string\t"item0"\n      string\t"item1"\n      number\t3.14\n      string\t"!="\n'

但是我如何实现它并且不同的字符串和运算符? 如果我在没有"的情况下对运算符使用声明,则会出现错误

GrammarError: Unexpected input '< | >' at line 17 column 36 in <string>

0 个答案:

没有答案