Browse Source

Added templates example

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.10.0
Erez Sh 4 years ago
parent
commit
d559e495ee
1 changed files with 26 additions and 0 deletions
  1. +26
    -0
      examples/templates.py

+ 26
- 0
examples/templates.py View File

@@ -0,0 +1,26 @@
#
# This example shows how to use Lark's templates to achieve cleaner grammars
#

from lark import Lark

grammar = r"""
start: list | dict

list: "[" _seperated{atom, ","} "]"
dict: "{" _seperated{key_value, ","} "}"
key_value: atom ":" atom

_seperated{x, sep}: x (sep x)* // Define a sequence of 'x sep x sep x ...'

atom: NUMBER | ESCAPED_STRING

%import common (NUMBER, ESCAPED_STRING, WS)
%ignore WS
"""


parser = Lark(grammar)

print(parser.parse('[1, "a", 2]'))
print(parser.parse('{"a": 2, "b": 6}'))

Loading…
Cancel
Save