question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inspect parse tree as it's being parsed, without transforming.

See original GitHub issue

I’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:closed
  • Created 4 years ago
  • Comments:12 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
erezshcommented, May 12, 2019

although it’s a bit unintuitive

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.

0reactions
elektitocommented, May 12, 2019

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! 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found