-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodes.py
More file actions
61 lines (46 loc) · 1.59 KB
/
Copy pathnodes.py
File metadata and controls
61 lines (46 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# nodes.py
tokens = [] # List of Token objects (global like in C++)
dt_bu = [] # Derivation tree (strings)
ast_stack = [] # AST bottom-up stack (list used like a stack)
class Token:
def __init__(self, type_, value):
self.type = type_ # e.g., "identifier", "operator", etc.
self.value = value
def __repr__(self):
return f"<{self.type}:{self.value}>"
class Node:
def __init__(self, label, children=None):
self.label = label # String label for the AST node
self.children = children or [] # List of child Node objects
def __repr__(self, depth=0):
indent = "." * depth
s = f"{indent}{self.label}\n"
for child in self.children:
s += child.__repr__(depth + 1)
return s
def build_tree(label, num_args):
if len(ast_stack) < num_args:
raise RuntimeError("AST stack underflow: not enough children for node")
children = [ast_stack.pop() for _ in range(num_args)][::-1] # Reverse to keep original order
node = Node(label, children)
ast_stack.append(node)
def print_ast():
if not ast_stack:
print("AST stack is empty!")
return
print("Abstract Syntax Tree (AST):")
# print(ast_stack[-1].__repr__())
print(ast_stack[-1])
def print_tokens():
global tokens
print("\nTokens:")
for token in tokens:
print(f"{token.type} : {token.value}")
if token.type == "EOF":
break
print()
def print_tree():
print("\nDerivation Tree Top Down:")
for rule in dt_bu:
print(rule)
print()