-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.py
126 lines (106 loc) · 3.81 KB
/
utils.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
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
import abc
# this class characterizes an automaton
class FSA:
def __init__ (self, numStates = 0, startStates=None, finalStates=None, alphabetTransitions=None) :
self.numStates = numStates
self.startStates = startStates
self.finalStates = finalStates
self.alphabetTransitions = alphabetTransitions
class NFA(FSA):
def simulate(self, ipStr):
S = set(self.startStates)
newS = set()
for i in range(len(ipStr)):
symbol = ipStr[i]
tm = self.alphabetTransitions[symbol]
for state in S:
trs = tm[state]
for tr in range(len(trs)):
if trs[tr] == 1:
newS.add(tr)
S = set(newS)
newS = set()
if len(self.finalStates) > 0 and not S.isdisjoint(self.finalStates):
print("String Accepted")
return True
else:
print("String Rejected")
return False
def getNFA(self):
return self
class ETree:
root = None
nfa = None
class ETNode:
def __init__(self, val=" ", left=None, right=None):
self.val = val
self.left = left
self.right = right
def compute(self, operands, operators):
operator = operators.pop()
if operator == "*":
left = operands.pop()
operands.append(self.ETNode(val=operator, left=left))
elif operator == "+":
right, left = operands.pop(), operands.pop()
operands.append(self.ETNode(val=operator, left=left, right=right))
elif operator == ".":
right, left = operands.pop(), operands.pop()
operands.append(self.ETNode(val=operator, left=left, right=right))
def parseRegex(self, regex):
operands, operators = [], []
for i in range(len(regex)):
if regex[i].isalpha():
operands.append(self.ETNode(val=regex[i]))
elif regex[i] == '(':
operators.append(regex[i])
elif regex[i] == ')':
while operators[-1] != '(':
self.compute(operands, operators)
operators.pop()
else :
operators.append(regex[i])
while operators:
self.compute(operands, operators)
if len(operators) == 0:
self.root = operands[-1]
else :
print("Parsing Regex failed.")
def getTree(self):
return self.root
###################################################################
# IMPLEMENTATION STARTS AFTER THE COMMENT
# Implement the following functions
# In the below functions to be implemented delete the pass statement
# and implement the functions. You may define more functions according
# to your need.
###################################################################
# .
def operatorDot(self, fsaX, fsaY):
pass
# +
def operatorPlus(self, fsaX, fsaY):
pass
# *
def operatorStar(self, fsaX):
pass
# a, b, c and e for epsilon
def alphabet(self, symbol):
pass
# Traverse the regular expression tree(ETree)
# calling functions on each node and hence
# building the automaton for the regular
# expression at the root.
def buildNFA(self, root):
if root == None:
print("Tree not available")
exit(0)
numStates = 0
initialState = set()
finalStates = set()
transitions = {}
# write code to populate the above datastructures for a regex tree
self.nfa = NFA(numStates, initialState, finalStates, transitions)
# print NFA
return self.nfa
######################################################################