Browse Source

Fixed examples. They didn't work with python3

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.5.1
Erez Shinan 9 years ago
parent
commit
7ba98c46f6
3 changed files with 11 additions and 9 deletions
  1. +3
    -3
      examples/calc.py
  2. +6
    -4
      examples/indented_tree.py
  3. +2
    -2
      examples/json_parser.py

+ 3
- 3
examples/calc.py View File

@@ -22,7 +22,7 @@ calc_grammar = """
""" """


class CalculateTree(InlineTransformer): class CalculateTree(InlineTransformer):
from operator import add, sub, mul, div, neg
from operator import add, sub, mul, truediv as div, neg
number = float number = float


def __init__(self): def __init__(self):
@@ -50,8 +50,8 @@ def main():


def test(): def test():
# print calc("a=(1+2)") # print calc("a=(1+2)")
print calc("a = 1+2")
print calc("1+a*-3")
print(calc("a = 1+2"))
print(calc("1+a*-3"))


if __name__ == '__main__': if __name__ == '__main__':
test() test()


+ 6
- 4
examples/indented_tree.py View File

@@ -7,15 +7,17 @@ It is crucial for the indenter that the NL_type matches the spaces (and tabs) af
from lark.lark import Lark from lark.lark import Lark
from lark.indenter import Indenter from lark.indenter import Indenter


tree_grammar = """
tree_grammar = r"""
?start: _NL* tree ?start: _NL* tree


tree: /\w+/ _NL [_INDENT tree+ _DEDENT]
tree: NAME _NL [_INDENT tree+ _DEDENT]


NAME: /\w+/ NAME: /\w+/


WS.ignore: /\s+/ WS.ignore: /\s+/
_NL.newline: /(\r?\n[\t ]*)+/
_NL: /(\r?\n[\t ]*)+/
_INDENT: "<INDENT>"
_DEDENT: "<DEDENT>"
""" """


class TreeIndenter(Indenter): class TreeIndenter(Indenter):
@@ -39,7 +41,7 @@ a
""" """


def test(): def test():
print parser.parse(test_tree).pretty()
print(parser.parse(test_tree).pretty())


if __name__ == '__main__': if __name__ == '__main__':
test() test()


+ 2
- 2
examples/json_parser.py View File

@@ -53,12 +53,12 @@ def test():
''' '''


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


if __name__ == '__main__': if __name__ == '__main__':
test() test()
with open(sys.argv[1]) as f: with open(sys.argv[1]) as f:
print parse(f.read())
print(parse(f.read()))



Loading…
Cancel
Save