1

I try to describe grammar and operators.

I use next code

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()

But it returns

'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'

But how can I realize it and differ string and operators? If I use declaring without " to operators, I get an error

GrammarError: Unexpected input '< | >' at line 17 column 36 in <string>
Petr Petrov
  • 4,090
  • 10
  • 31
  • 68

0 Answers0