Walking the tree top-down while passing arguments to child nodes recursively
See original GitHub issuehttps://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:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top 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 >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
@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 likechildren[0]
Does something speak against storing the ‘arguments’/environment as a class attribute of the Interpreter subclass?