-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfa.py
executable file
·41 lines (31 loc) · 928 Bytes
/
dfa.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
#!/usr/bin/python
import json
import sys
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:
return starting_state in final_states
if process_string(input_string[1:], transitions[starting_state][input_string[0]]):
return True
return False
if len(sys.argv) > 1:
loadFromJson(sys.argv[1])
else:
print("Must specify dfa file")
if len(sys.argv) > 2:
input_string = sys.argv[2]
else:
input_string = "11001011001101101"
if process_string(input_string, initial_state):
print("accepted")
else:
print("rejected")