-
Notifications
You must be signed in to change notification settings - Fork 0
/
nfa-ep.py
executable file
·38 lines (30 loc) · 1021 Bytes
/
nfa-ep.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
#!/usr/bin/python
import json
def loadFromJson(filename):
global alphabet, states, initial_state, final_states, transitions
with open(filename) as f:
data = json.load(f)
alphabet = data["alphabet"]
states = data["states"]
initial_state = data["initial_state"]
final_states = data["final_states"]
transitions = data["transitions"]
def process_string(input_string, starting_state):
global alphabet, states, initial_state, final_states, transitions
if len(input_string) > 0:
for state in transitions[starting_state][input_string[0]]:
if process_string(input_string[1:], state):
return True
elif starting_state in final_states:
return True # ended in an accepting state
# epsilon transforms
if("e" in transitions[starting_state].keys()):
for state in transitions[starting_state]["e"]:
if process_string(input_string, state):
return True
return False
loadFromJson("sample_nfa-ep.json")
if process_string("000011", initial_state):
print ("accepted")
else:
print("rejected")