Inspect parse tree as it's being parsed, without transforming.
See original GitHub issueI’m trying to use the transformer
argument of the Lark
class to have a visitor inspect everything that has been parsed so far, so that later this can help with the lexer (something similar to “the lexer hack” some C compilers use). I tried using an InPlace trasnformer and not-transforming anything, but it seems that the InPlace transformer is not accepted, or rather it is accepted but used as a base transformer. This is a very simple example:
grammar = r"""
program: idx+
idx: "xyz"i
%ignore /[ \t\f]+/
"""
prog="xyz xyz xyz"
@v_args(tree=True)
class T(Transformer_InPlace):
def idx(self, t):
print('visited:', t)
parser = Lark(grammar, start='program', parser='lalr', transformer=T())
t = parser.parse(prog)
print(t.pretty())
This will output:
visited: []
visited: []
visited: []
program
[]
[]
[]
This means that neither v_args has worked as it should, nor has Transformer_InPlace behaved as expected.
Is this the expected behavior? Because if it is, I don’t think it’s documented anywhere. Also, is there any other way of achieving this?
Issue Analytics
- State:
- Created 4 years ago
- Comments:12 (12 by maintainers)
Top Results From Across the Web
Parse Tree - an overview | ScienceDirect Topics
The paradigm relies, inherently, on the availability of the parse tree. The evaluator might simulate the parse tree, but it must behave as...
Read more >parser — Access Python parse trees — Python 3.8.14 ...
The expr() function parses the parameter source as if it were an input to compile(source, 'file.py', 'eval') . If the parse succeeds, an...
Read more >A Guide To Parsing: Algorithms And Terminology
Fundamentally parsing is necessary because different entities need the data to be in different forms. Parsing allows to transform data in a way...
Read more >Documentation: 15: 52.3. The Parser Stage - PostgreSQL
The parser has to check the query string (which arrives as plain text) for valid syntax. If the syntax is correct a parse...
Read more >You are not logged in. Reading 18: Parsers
The parser typically produces a parse tree , which shows how grammar productions are expanded into a sentence that matches the character sequence....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I guess what you’re trying to do is a bit unusual, so it’s not that bad. If someone comes up with a better design I’ll be happy to consider it. But as for the current one, as they say, I had my reasons 😃
As for questions, no worries. I always have time for contributors, and I’ll be happy to help.
I guess you’re right, although it’s a bit unintuitive. Anyways, I already fixed my code with the reconstruction code you first suggested and moved on. I believe I will be back with other questions soon, so I apologize for that beforehand! 😃