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.

Walking the tree top-down while passing arguments to child nodes recursively

See original GitHub issue

https://github.com/lark-parser/lark/issues/252 shows that the lark.Interpreter can be used to traverse lark.Tree top-down.

The language whose AST I want to work on requires me to collect some information before I can transform/reduce the source code to the final format:

domain A:
   def a():
       pass

   def b():
       pass

I need an to traverse this in a way that gives me a dictionary with the function names:

>>>domains
{"A":["a", "b"]}

Something like this:


domains = {}

class Unknown(lark.???):
    def domain(self, node):
        # Where node.value is then "A", for example
        domains[node.value] = []
        self.visit_children(node, arg=node.value)

    def function(self, node, arg):
        domains[arg].append(node.value)

The bottom-up lark.Visitor and lark.Transformer do not allow accessing parent node information from the child, so I cannot use them.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
erezshcommented, Apr 11, 2020

@void4 Is this basically for a namespace lookup?

Also, you can shape the tree in the grammar, like with ?name: .. to collapse nodes with a single child, so you won’t have to do things like children[0]

1reaction
MegaIngcommented, Apr 10, 2020

Does something speak against storing the ‘arguments’/environment as a class attribute of the Interpreter subclass?

class TypeGetter(Interpreter):
    def start(self, node):
        self.visit_children(node)
        if DEBUG:
            print("Collected type definitions and function signatures.")

    def domaindef(self, node):
        self.domainname = node.children[0].children[0].value
        self.visit_children(node)

    def funcdef(self, node):
        domains[self.domainname][getChildByName(node, "funname").children[0].value] = {...}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Walking a tree, parent first - algorithm - Stack Overflow
I think you can do it non-recursively with a queue:. In pseudocode: Create an empty queue, then push the root node. while nonempty(q)...
Read more >
Top-down and Bottom-up - RUOCHI.AI
As we know, a tree can be defined recursively as a node(the root node), which includes a value and a list of references...
Read more >
Transformers & Visitors - Lark documentation - Read the Docs
Interpreter walks the tree starting at the root. Visits the tree, starting with the root and finally the leaves (top-down). For each tree...
Read more >
Three ways to handle recursion - DEV Community ‍ ‍
When calling flatten(tree) , it starts processing at the root node and recursively walks down the tree walking over the children, to return ......
Read more >
Perl | Recursive Subroutines - GeeksforGeeks
A simple example showing the use of recursive subroutine in Perl would be ... Although in Perl, traversing a directory tree or walking...
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