From 2be35e3e38c2af9084ff764958d7ee3a30de059c Mon Sep 17 00:00:00 2001 From: Erez Shinan Date: Tue, 10 Jul 2018 15:01:31 +0300 Subject: [PATCH] Added custom_lexer to examples/README --- examples/README.md | 1 + examples/custom_lexer.py | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/examples/README.md b/examples/README.md index 3ca623a..8220d55 100644 --- a/examples/README.md +++ b/examples/README.md @@ -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 diff --git a/examples/custom_lexer.py b/examples/custom_lexer.py index 3e42a21..965490f 100644 --- a/examples/custom_lexer.py +++ b/examples/custom_lexer.py @@ -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]}