Browse Source

Make the JSON parser fast again

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.8.0
Erez Sh 4 years ago
parent
commit
f7a6366b6c
1 changed files with 11 additions and 1 deletions
  1. +11
    -1
      examples/json_parser.py

+ 11
- 1
examples/json_parser.py View File

@@ -49,11 +49,21 @@ class TreeToJson(Transformer):
false = lambda self, _: False


### Create the JSON parser with Lark, using the Earley algorithm
# json_parser = Lark(json_grammar, parser='earley', lexer='standard')
# def parse(x):
# return TreeToJson().transform(json_parser.parse(x))

json_parser = Lark(json_grammar, parser='lalr', lexer='standard', transformer=TreeToJson())
### 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())
parse = json_parser.parse




Loading…
Cancel
Save