-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmka.py
More file actions
executable file
·270 lines (252 loc) · 7.95 KB
/
mka.py
File metadata and controls
executable file
·270 lines (252 loc) · 7.95 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/bin/python3.4
# coding=utf-8
#MKA:xkondu00
import os
import sys
import argparse
import stateMachine as SM
from configparser import ConfigParser
from stateMachine import FiniteStateMachine as FSM
CLASSIC_FSM = "classic.conf.py"
RULES_ONLY_FSM = "rules_only.conf.py"
WHITE_CHAR_FSM = "white_char.conf.py"
class WrongArgument(BaseException):
pass
def parse_args(parser):
"""
Handles CLI options using argparse
"""
if "--help" in sys.argv and len(sys.argv) != 2:
sys.stderr.write("Incorrect combination of parameters\n")
return 1
group = parser.add_mutually_exclusive_group()
parser.add_argument(
"--input",
help="Read from this file instead of stdin",
action="append",
type=str)
parser.add_argument(
"--output",
help="Write to this file instead of stdout",
action="append",
type=str)
group.add_argument(
"-f", "--find-non-finishing",
help="Prints out non finishing state, '0' if no state is found",
action="append_const",
const=True)
group.add_argument(
"-m", "--minimize",
help="Minimize finite state machine",
action="append_const",
const=True)
parser.add_argument(
"-i", "--case-insensitive",
help="Symbols and state names are case insensitive",
action="append_const",
const=True)
parser.add_argument(
"-w", "--white-char",
help="Commas can be replaced by white characters",
action="append_const",
const=True)
parser.add_argument(
"-r", "--rules-only",
help="Expects rules only. First state given is starting",
action="append_const",
const=True)
group.add_argument(
"--analyze-string",
help="Check if given string can be read by state machine",
type=str,
action='append',)
parser.add_argument(
"--wsfa",
help="Simple deterministic finite state machine could be given",
action="append_const",
const=True)
try:
args = parser.parse_args()
except SystemExit as e:
# changing return value from 2 to 1
# return 0 if --help
sys.exit(1 if e.code else 0)
# checking duplicity of arguments
for key in args.__dict__:
args.__dict__[key] = argument_param(args.__dict__[key], None)
# setting input stream
if args.input is None or args.input == "-":
args.input = sys.stdin
else:
try:
args.input = os.path.expanduser(args.input)
args.input = open(
os.path.realpath(args.input),
"r", encoding="utf-8"
)
except IOError or OSError:
sys.stderr.write("Could not open file\n")
return 2
# setting output stream
if args.output is None or args.output == "-":
args.output = sys.stdout
else:
try:
args.output = os.path.expanduser(args.output)
args.output = open(
os.path.realpath(args.output),
"w", encoding="utf-8"
)
except IOError or OSError:
sys.stderr.write("Could not open file\n")
return 3
return args
def argument_param(argv, default):
if type(argv) == list:
if len(argv) != 1:
sys.stderr.write("Duplicity of argument\n")
sys.exit(1)
else:
return argv[0]
elif type(argv) == bool:
return argv
return default
def input_parsing_fsm(conf_path, starting_state, fd):
"""
Builds FSM from given config file
Reads input from file or stdin
Returns: dictionery of FA's components created by handler
"""
state_fsm = FSM(rules_only=True)
conf = ConfigParser()
conf.read(conf_path)
state_fsm.build_from_config(conf)
state_fsm.set_starting(starting_state)
while True:
char = fd.read(1)
if char == "":
break
state_fsm.step(char)
if not state_fsm.is_finishing():
raise SM.NotFinishing("Lexial or syntax error", "\n")
state_fsm.step(" ")
return state_fsm.get_output()
def createFSM(configuration, case_insensitive=False, rules_only=False):
"""
Creates FSM object and fills it with components.
parametrs:
configuration: FA's components in dictionary
"""
list_of_states = configuration.get("state", [])
list_of_symbols = configuration.get("symbol", [])
list_of_rules = zip(
configuration.get("current", []),
configuration.get("target", []),
configuration.get("input", [])
)
if not rules_only:
start = configuration.get("start").pop()
list_of_finishing = configuration.get("finish", [])
else:
start = configuration.get("current", [])[0]
list_of_finishing = configuration.get("finish", [])
list_of_finishing = list(set([
x[0] for x in zip(
configuration.get("target", []),
list_of_finishing
)
if x[1] == "."
]))
fsm = FSM(rules_only=rules_only, line_comment=None)
# add all states
for item in list_of_states:
if case_insensitive:
item = item.lower()
fsm.add_state(item)
# add all symbols
if not list_of_symbols and not rules_only:
raise SM.Invalid("Set of symbols is empty", "\n")
for item in list_of_symbols:
if case_insensitive:
item = item.lower()
fsm.add_symbol(item)
# add rules
for item in list_of_rules:
if item[2] == "'":
raise SM.Nondeterminism("Epsilon transitions", "\n")
if case_insensitive:
fsm.add_rule(item[0].lower(), item[1].lower(), item[2].lower())
else:
fsm.add_rule(item[0], item[1], item[2])
# set starting
fsm.set_starting(start)
# set finishing
for item in list_of_finishing:
if case_insensitive:
item = item.lower()
fsm.set_finishing(item)
return fsm
def main(args):
"""
1) set correct config file for lexical and syntax analysis
2) read input (return 60 if syntax or lexical error)
3) create FSM object (return 61 if semantic error)
4) check if FSM is well specified (return 62 otherwise)
5) based of CLI options evaluate FSM
a) write to output
b) minimize
c) find non finihsing state
d) analyze string
"""
if args.rules_only:
fsm_conf = RULES_ONLY_FSM
elif args.white_char:
fsm_conf = WHITE_CHAR_FSM
else:
fsm_conf = CLASSIC_FSM
# Prepare input readinf FSM
try:
config = input_parsing_fsm(fsm_conf, "start_1", args.input)
except (SM.NotFinishing, SM.MissingRule) as e:
sys.stderr.write(" ".join(e.args))
sys.exit(60)
# Fill FSM with states, symbols and rules
try:
fsm = createFSM(config, args.case_insensitive, args.rules_only)
except (SM.Invalid) as e:
sys.stderr.write(" ".join(e.args))
sys.exit(61)
except (SM.Nondeterminism) as e:
sys.stderr.write(" ".join(e.args))
sys.exit(62)
if args.wsfa:
# TODO
pass
# Evaluate FSM
if not fsm.is_WSFA():
sys.stderr.write("Not well specified finit automata\n")
sys.exit(62)
if args.minimize:
fsm = fsm.minimize()
if args.analyze_string:
if args.case_insensitive:
args.analyze_string = args.analyze_string.lower()
try:
correct = fsm.read_string(args.analyze_string)
except SM.MissingRule:
return 1
args.output.write("1" if correct else "0")
elif not args.find_non_finishing:
args.output.write(repr(fsm))
else:
args.output.write(fsm.find_non_terminating())
args.input.close()
args.output.close()
return 0
if __name__ == "__main__":
parser = argparse.ArgumentParser()
args = parse_args(parser)
if type(args) == int:
sys.exit(args)
retval = main(args)
sys.exit(retval)