-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenfa1.py
179 lines (142 loc) · 4.78 KB
/
enfa1.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
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
class Type:
SYMBOL = 1
CONCAT = 2
UNION = 3
KLEENE = 4
class ExpressionTree:
def __init__(self, _type, value=None):
self._type = _type
self.value = value
self.left = None
self.right = None
def constructTree(regexp):
stack = []
for c in regexp:
if c.isalpha():
stack.append(ExpressionTree(Type.SYMBOL, c))
else:
if c == "+":
z = ExpressionTree(Type.UNION)
z.right = stack.pop()
z.left = stack.pop()
elif c == ".":
z = ExpressionTree(Type.CONCAT)
z.right = stack.pop()
z.left = stack.pop()
elif c == "*":
z = ExpressionTree(Type.KLEENE)
z.left = stack.pop()
stack.append(z)
return stack[0]
def inorder(et):
if et._type == Type.SYMBOL:
print(et.value)
elif et._type == Type.CONCAT:
inorder(et.left)
print(".")
inorder(et.right)
elif et._type == Type.UNION:
inorder(et.left)
print("+")
inorder(et.right)
elif et._type == Type.KLEENE:
inorder(et.left)
print("*")
def higherPrecedence(a, b):
p = ["+", ".", "*"]
return p.index(a) > p.index(b)
def postfix(regexp):
# adding dot "." between consecutive symbols
temp = []
for i in range(len(regexp)):
if i != 0\
and (regexp[i-1].isalpha() or regexp[i-1] == ")" or regexp[i-1] == "*")\
and (regexp[i].isalpha() or regexp[i] == "("):
temp.append(".")
temp.append(regexp[i])
regexp = temp
stack = []
output = ""
for c in regexp:
if c.isalpha():
output = output + c
continue
if c == ")":
while len(stack) != 0 and stack[-1] != "(":
output = output + stack.pop()
stack.pop()
elif c == "(":
stack.append(c)
elif c == "*":
output = output + c
elif len(stack) == 0 or stack[-1] == "(" or higherPrecedence(c, stack[-1]):
stack.append(c)
else:
while len(stack) != 0 and stack[-1] != "(" and not higherPrecedence(c, stack[-1]):
output = output + stack.pop()
stack.append(c)
while len(stack) != 0:
output = output + stack.pop()
return output
class FiniteAutomataState:
def __init__(self):
self.next_state = {}
def evalRegex(et):
# returns equivalent E-NFA for given expression tree (representing a Regular
# Expression)
if et._type == Type.SYMBOL:
return evalRegexSymbol(et)
elif et._type == Type.CONCAT:
return evalRegexConcat(et)
elif et._type == Type.UNION:
return evalRegexUnion(et)
elif et._type == Type.KLEENE:
return evalRegexKleene(et)
def evalRegexSymbol(et):
start_state = FiniteAutomataState()
end_state = FiniteAutomataState()
start_state.next_state[et.value] = [end_state]
return start_state, end_state
def evalRegexConcat(et):
left_nfa = evalRegex(et.left)
right_nfa = evalRegex(et.right)
left_nfa[1].next_state['epsilon'] = [right_nfa[0]]
return left_nfa[0], right_nfa[1]
def evalRegexUnion(et):
start_state = FiniteAutomataState()
end_state = FiniteAutomataState()
up_nfa = evalRegex(et.left)
down_nfa = evalRegex(et.right)
start_state.next_state['epsilon'] = [up_nfa[0], down_nfa[0]]
up_nfa[1].next_state['epsilon'] = [end_state]
down_nfa[1].next_state['epsilon'] = [end_state]
return start_state, end_state
def evalRegexKleene(et):
start_state = FiniteAutomataState()
end_state = FiniteAutomataState()
sub_nfa = evalRegex(et.left)
start_state.next_state['epsilon'] = [sub_nfa[0], end_state]
sub_nfa[1].next_state['epsilon'] = [sub_nfa[0], end_state]
return start_state, end_state
def printStateTransitions(state, states_done, symbol_table):
if state in states_done:
return
states_done.append(state)
for symbol in list(state.next_state):
line_output = "q" + str(symbol_table[state]) + "\t\t" + symbol + "\t\t\t"
for ns in state.next_state[symbol]:
if ns not in symbol_table:
symbol_table[ns] = 1 + sorted(symbol_table.values())[-1]
line_output = line_output + "q" + str(symbol_table[ns]) + " "
print(line_output)
for ns in state.next_state[symbol]:
printStateTransitions(ns, states_done, symbol_table)
def printTransitionTable(finite_automata):
print("State\tSymbol\tNext state")
printStateTransitions(finite_automata[0], [], {finite_automata[0]:0})
r = input("Enter regex: ")
pr = postfix(r)
et = constructTree(pr)
#inorder(et)
fa = evalRegex(et)
printTransitionTable(fa)