Simple example of assignment in my language:
x = 3 ->
Here's the generated AST after parsing (In Python):
[('statement', ('assignment', 'x', ('assignment_operator', '='), ('expr', ('term', ('factor', '3')))), '->')]
How can I recursively access any possible depth in order to, in the most trivial case, print all of them? (Or convert the text into something else?). Is there a specific algorithm for doing this? If there is, do you recommend any specific material?
To walk a tree, just use a stack or a queue (depending on wether you want to go depth first or breath first). For each node encountered, push the children onto the stack or into the queue, then take the next item out of the data structure to process and repeat.
Application in compilers. Abstract syntax trees are data structures widely used in compilers to represent the structure of program code. An AST is usually the result of the syntax analysis phase of a compiler.
Abstract syntax trees are important data structures in a compiler. It contains the least unnecessary information. Abstract syntax trees are more compact than a parse tree and can be easily used by a compiler.
An Abstract Syntax Tree describes the parse tree logically. It does not need to contain all the syntactical constructs required to parse some source code (white spaces, braces, keywords, parenthesis etc). That's why Parse Tree is also called Concrete Syntax Tree while the AST is called Syntax Tree .
To walk a tree, just use a stack or a queue (depending on wether you want to go depth first or breath first).
For each node encountered, push the children onto the stack or into the queue, then take the next item out of the data structure to process and repeat.
For example, breath first could look like this:
from collections import deque
def walk(node):
queue = deque([node])
while queue:
node = queue.popleft()
if isinstance(node, tuple):
queue.extend(node[1:]) # add the children to the queue
yield node
which produces the following walking order for your tree:
>>> for node in walk(tree[0]):
... print(node[0] if isinstance(node, tuple) else node)
...
statement
assignment
->
x
assignment_operator
expr
=
term
factor
3
Your data structure is a little messy, mixing tuples of different length. You may want to look to using a nametuple
class to formalise the contents a little.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With