-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patholdBuildPTrecFunc.txt
More file actions
48 lines (44 loc) · 2.04 KB
/
Copy patholdBuildPTrecFunc.txt
File metadata and controls
48 lines (44 loc) · 2.04 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
def build_parse_tree_rec(tokens: List[str], node: Optional[Node] = None) -> Node:
"""
An inner recursive inner function to build a parse tree
:param tokens: A list of token strings
:param node: A Node object
:return: a node with children whose tokens are variables, parenthesis, slashes, or the inner part of an expression
"""
firstOpenBracket = 0
while tokens:
# If there is no root node
if not node:
return build_parse_tree_rec(tokens,Node(tokens))
# If the token is a variable name or a lambda sign
elif len(tokens[0]) == 1 and tokens[0] in avc:
node.add_child_node(Node(tokens[0]))
return build_parse_tree_rec(tokens[1:],node)
# If the token is a variable with a length > then 1
elif len(tokens[0]) > 1:
node.add_child_node(Node(tokens[0]))
return build_parse_tree_rec(tokens[1:],node)
# If the token is a opening bracket
elif tokens[0] == "(":
closingBracketIndex = findMostOuterBracket(tokens)
# Test case(Leave out of final code**)
if closingBracketIndex == -1:
print("Issue with list input when recurrsivlty making parse tree")
print(tokens)
break
else:
subnode = Node(tokens[1:closingBracketIndex - 1])
node.add_child_node(Node(tokens[0]))
node.add_child_node(subnode)
node.add_child_node(build_parse_tree_rec(tokens[1: closingBracketIndex - 1], subnode))
node.add_child_node(closingBracketIndex)
return build_parse_tree_rec(tokens[closingBracketIndex:], node)
elif tokens[0] == ")":
break
else:
print(tokens)
# elif tokens[0] in ["\\", "("]: ## INCREASE LEVEL
# node.add_child_node(Node(tokens))
# return build_parse_tree_rec(tokens[1:], node)
#TODO
return node