Browse Source

Added custom_lexer to examples/README

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.6.1
Erez Shinan 6 years ago
parent
commit
2be35e3e38
2 changed files with 4 additions and 1 deletions
  1. +1
    -0
      examples/README.md
  2. +3
    -1
      examples/custom_lexer.py

+ 1
- 0
examples/README.md View File

@@ -15,4 +15,5 @@
- [python\_parser.py](python_parser.py) - A fully-working Python 2 & 3 parser (but not production ready yet!)
- [conf\_lalr.py](conf_lalr.py) - Demonstrates the power of LALR's contextual lexer on a toy configuration language
- [conf\_earley.py](conf_earley.py) - Demonstrates the power of Earley's dynamic lexer on a toy configuration language
- [custom\_lexer.py](custom_lexer.py) - Demonstrates using a custom lexer to parse a non-textual stream of data
- [reconstruct\_json.py](reconstruct_json.py) - Demonstrates the experimental text-reconstruction feature

+ 3
- 1
examples/custom_lexer.py View File

@@ -16,7 +16,6 @@ class TypeLexer(Lexer):
pass

def lex(self, data):
print(data)
for obj in data:
if isinstance(obj, int):
yield Token('INT', obj)
@@ -44,9 +43,12 @@ class ParseToDict(Transformer):
def test():
data = ['alice', 1, 27, 3, 'bob', 4, 'carrie', 'dan', 8, 6]

print(data)

tree = parser.parse(data)
res = ParseToDict().transform(tree)

print('-->')
print(res) # prints {'alice': [1, 27, 3], 'bob': [4], 'carrie': [], 'dan': [8, 6]}




Loading…
Cancel
Save