-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
192 lines (156 loc) · 5.24 KB
/
main.py
File metadata and controls
192 lines (156 loc) · 5.24 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
from anytree import Node, RenderTree
import intopostfix
regex = "(a|b)*.a.b.b"
regex = regex + ".#" #augmentation
#"ab|*ac.*|#."
postfix = intopostfix.infix_to_postfix(regex)
print(postfix)
class Node:
def __init__(self, value, left=None, right=None, parent = None, num=None):
self.value = value
self.left = left
self.right = right
self.first_pos = set()
self.last_pos = set()
self.nullable = False
self.follow_pos = set()
self.parent = parent
self.num = num
def position(regex):
stack = []
output = []
leaf = []
count = 1
for i in regex:
if i.isalpha() or i == '#':
node1 = Node(value=i, num=count)
node1.first_pos.add(count)
node1.last_pos.add(count)
node1.nullable = False
leaf.append(node1)
stack.append(node1)
count += 1
elif i == '|' or i == '.':
newNode = Node(value=i)
a = stack.pop()
b = stack.pop()
if i == "|":
newNode.nullable = False
if a.nullable or b.nullable:
newNode.nullable = True
newNode.first_pos = a.first_pos|b.first_pos
newNode.last_pos = a.last_pos|b.last_pos
else:
if b.nullable and a.nullable:
newNode.nullable = True
if b.nullable == True:
newNode.first_pos = a.first_pos|b.first_pos
newNode.last_pos = a.last_pos
if b.nullable!= True:
newNode.first_pos = b.first_pos
newNode.last_pos = a.last_pos
for i in b.last_pos:
leaf[i - 1].follow_pos = leaf[i - 1].follow_pos | a.first_pos
newNode.right = a
newNode.left = b
stack.append(newNode)
output.append(newNode)
elif i == '*':
a = stack.pop()
newNode = Node(value = i, left=a)
newNode.nullable = True
newNode.first_pos = a.first_pos
newNode.last_pos = a.last_pos
for i in newNode.last_pos:
leaf[i-1].follow_pos = leaf[i-1].follow_pos|newNode.first_pos
stack.append(newNode)
output.append(newNode)
else:
output.append(Node(value = i))
return output, leaf
val = position(postfix)
alpha = []
def printfollowPos(flag = False):
if flag == True:
for i in val[1]:
print(i.value)
print(i.num)
print("FOllow POS IS : " , i.follow_pos)
def make_DFA():
visited = []
marked = []
dstate = [val[0][-1].first_pos]
count = 0
# make list of chars
for i in postfix:
if i.isalpha():
if i not in alpha:
alpha.append(i)
marked.append([i])
count+=1
while dstate!=[]:
a = dstate.pop()
visited.append(a)
count1= 0
for i in alpha:
folpos = set()
for j in a:
if i == val[1][j-1].value:
folpos = folpos | val[1][j-1].follow_pos
if folpos not in visited:
dstate.append(folpos)
marked[count1].append(a)
marked[count1].append(folpos)
count1+=1
return marked, visited
rawdata = make_DFA()
rawar = rawdata[0]
nodes = rawdata[1]
print(nodes)
def transition(rawarr, char, stat):
for i in range(0,len(rawarr)):
if rawarr[i][0] == char:
for j in range(1,len(rawarr[i])-1,2):
if rawarr[i][j] == stat:
if rawarr[i][j+1] == set():
return None
else:
return rawarr[i][j+1]
def regexChcker(strr):
nxtStat = val[0][-1].first_pos
for i in strr:
if i not in alpha:
return print("Rejected")
a = transition(rawar, i , nxtStat)
if a != None:
nxtStat = a
if len(val[1]) in nxtStat:
print("Accepted")
else:
print("Rejected")
testStr = "aaabababaa"
regexChcker(testStr)
def nodesTree(p = False):
if p == True:
for i in range(0,len(val)):
print(val[0][i])
print("value: ", val[0][i].value)
try:
print("Right: ", val[0][i].right.value)
print("Left: ",val[0][i].left.value)
print("Parent First Post: ", val[0][i].first_pos)
print("Parent last Post: ", val[0][i].last_pos)
print("NUM Left First Pos: ", val[0][i].left.first_pos )
print("NUM Left Last Pos: ", val[0][i].left.last_pos)
print("NUM Right First Pos: ", val[0][i].right.first_pos )
print("NUM Right Last Pos: ", val[0][i].right.last_pos)
print("NUM Right: ", val[0][i].right.num)
print("NUM Left Follow Pos: ", val[0][i].left.follow_pos)
print("NUM Right Follow Pos: ", val[0][i].right.follow_pos )
except:
print("Right: ", val[0][i].right)
print("Left: ", val[0][i].left)
else:
return
nodesTree(p = False)
printfollowPos(False)