-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyntax.py
83 lines (71 loc) · 2.48 KB
/
syntax.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from node import Node
from utils import fail
class Syntax():
def __init__(self, config: dict):
self.config = config
self.root = None
self.atom_list = []
self.operations = []
def set_root(self, root: Node):
self.root = root
def recon(self, root: Node):
rez = ""
if(root.info in self.config["notations"]["binary"].values()):
r = "(" + self.recon(root.childs[0]) + \
root.info + self.recon(root.childs[1]) + ")"
rez += r
self.operations.append(r)
elif(root.info in self.config["notations"]["unary"].values()):
r = "(" + root.info + self.recon(root.childs[0]) + ")"
rez += r
self.operations.append(r)
else:
rez += root.info
return rez
def reconstruct(self):
return self.recon(self.root)
def check_childs(self, root: Node):
if root.info in self.config["notations"]["binary"].values():
if len(root.childs) != 2:
return False
return True
elif root.info in self.config["notations"]["unary"].values():
if len(root.childs) != 1:
return False
return True
elif len(root.childs) != 0:
return False
return True
def check(self, root: Node):
if(root.info == ""):
return False
if len(root.childs) > 2:
fail("syntax",(root,))
return False
for child in root.childs:
if(self.check_childs(child)):
self.check(child)
else:
fail("syntax", (child, ))
return False
return True
def check_syntax(self):
return self.check(self.root)
def validate(self, input_string):
import re
# construct regex
regex = "([A-Za-z"
regex += ''.join([i for i in self.config['notations']['brackets'].values()])
regex += ''.join([i for i in self.config['notations']['unary'].values()])
regex += ''.join([i for i in self.config['notations']['binary'].values()])
regex += "])"
reg = re.findall(regex, input_string)
if len(reg) != len(input_string):
fail("val", (reg, input_string))
# get all atoms
regex = "([A-Za-z])"
reg = re.findall(regex, input_string)
for i in reg:
if i not in self.atom_list:
self.atom_list.append(i)
return True