-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformulaIO.py
More file actions
56 lines (48 loc) · 1.63 KB
/
formulaIO.py
File metadata and controls
56 lines (48 loc) · 1.63 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
"""
class definition for formula input and parse
"""
from converter import parser
from node import Node
class Formula:
def __init__(self, formula_parser=parser) -> None:
self.parser = formula_parser
# self.formula = input("Input formula : ")
self.formula = input()
self.parse()
self.show()
def parse(self) -> Node:
"""
parse formula using (input) parser
"""
self.parser_out = self.parser.parse(self.formula)
self.ast = Node(self.parser_out)
return self.ast
def show(self):
"""
Output the resultant formula in EG, EU and EX representation
output number of nodes in parse tree
In the bottom-up order, for each node of parse tree, output node-id and
subformula corr. to that node.
"""
print(f"resultant formula : {self.parser_out}")
print(f"No of Nodes : {self.ast.node_count-1}")
self.bottom_up_traversal(self.parser_out)
def bottom_up_traversal(self, t: tuple):
"""
bottom up traversal of parse tree
uses parser_out
"""
if len(t) == 2:
if isinstance(t[1], tuple):
self.bottom_up_traversal(t[1])
print(f"Node Id : {t[0]}\tSubformula : {t}")
elif len(t) == 3:
if isinstance(t[1], tuple):
self.bottom_up_traversal(t[1])
if isinstance(t[2], tuple):
self.bottom_up_traversal(t[2])
print(f"Node Id : {t[0]}\tSubformula : {t}")
elif len(t) == 1:
print(f"Node Id : {t[0]}")
# f = Formula().parse()
# f.inorder_traversal()