-
Notifications
You must be signed in to change notification settings - Fork 0
/
delossantos4.py
executable file
·323 lines (308 loc) · 15 KB
/
delossantos4.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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
class DFA:
def __init__(self, line):
self.line = line
self.istype = [0,0,0,0,0]
self.ischar = 0
self.isarray = 0
self.iseqarray = 0
self.isequal = 0
self.ispointer = 0
self.existtype = 0
def process(self):
testcase = int(self.line[0]) * 2
i = 0
while i < testcase:
kind = int(self.line[i+1])
string = self.line[i+2]
self.execute(kind, string)
i += 2
def execute(self, kind, string):
xstr = self.extract(string)
self.check(kind, xstr)
def extract(self, string):
xstr = []
tmp = ''
for char in string:
if char == ' ':
xstr.append(tmp)
tmp = ''
elif char in ',;(){}[]=+-*/"\'':
xstr.append(tmp)
xstr.append(char)
tmp = ''
else:
tmp += char
xstr.append(tmp)
xstr = list(filter(None, xstr))
return xstr
def evaluate(self, st):
result = 0
types = ['int', 'float', 'char', 'double', 'void']
if st in types:
result = self.checktype(st)
return result
elif st.isdigit():
result = self.checknumber(st)
elif '.' in st:
if st.replace('.', '').isdigit():
if st.count('.') == 1:
result = 4
else:
result = 20
else:
result = 20
elif st.isalpha() and len(st) == 1:
result = self.checkname(st)
elif st == ',':
result = 5
elif st == ';':
result = 6
elif st == '=':
result = 7
self.isequal = 1
# elif st == '*':
# result = 18
# self.ispointer = 1
elif st == 'return':
result = 15
elif st == '"' or st == "'":
result = 8
self.ischar = 1
elif st == '{':
result = 9
elif st == '}':
result = 10
elif st == '[':
result = 11
self.isarray = 1
elif st == ']':
result = 12
elif st == '(':
result = 13
elif st == ')':
result = 14
elif st in '+-*/':
result = 17
elif len(st) > 1 and st.isalnum():
if st.isalnum():
result = self.checkname(st)
else:
result = 20
else:
result = 20
return result
def checktype(self, st):
if st == 'int':
self.istype = [1, 0, 0, 0, 0]
elif st == 'char':
self.istype = [0, 1, 0, 0, 0]
elif st == 'double':
self.istype = [0, 0, 1, 0, 0]
elif st == 'float':
self.istype = [0, 0, 0, 1, 0]
elif st == 'void':
self.istype = [0, 0, 0, 0, 1]
self.existtype = 1
return 0
def checkname(self, st):
keywords = [
'auto', 'double', 'int', 'struct',
'break', 'else', 'long', 'switch',
'case', 'enum', 'register', 'typedef',
'char', 'extern', 'return', 'union',
'const', 'float', 'short', 'unsigned',
'continue', 'for', 'signed', 'void',
'default', 'goto', 'sizeof', 'volatile',
'do', 'if', 'static', 'while'
]
if st[0].isdigit():
return 20
elif self.existtype == 1 or self.existtype == 0:
if st in keywords:
return 20
elif self.iseqarray == 1 and self.isarray == 0:
return 20
elif self.ischar == 1:
self.ischar = 0
if self.istype == [0, 1, 0, 0, 0] and len(st) == 1:
return 3
else:
return 20
else:
return 1
def checknumber(self, st):
if self.isequal == 1:
if self.istype == [1, 0, 0, 0, 0]:
if self.ischar == 1:
self.ischar = 0
return 20
elif self.iseqarray == 1 and self.isarray == 0:
return 20
else:
return 4
elif self.istype == [0, 1, 0, 0, 0]:
if self.ischar == 1:
self.ischar = 0
return 3
else:
return 4
elif self.isarray == 1:
return 16
else:
return 20
def check(self, kind, lst):
codes = []
i = 0
while i < len(lst):
codes.append(self.evaluate(lst[i]))
i += 1
i = 0
while i < len(codes):
if codes[i] == None:
codes[i] = 20
i += 1
if kind == 1:
table = [
[ 1, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 2, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 19, 20, 20],
[20, 20, 20, 20, 20, 15, 3, 4, 20, 20, 20, 16, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[ 1, 20, 20, 20, 20, 20, 3, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 5, 20, 20, 5, 20, 20, 20, 6, 8, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 15, 3, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 7, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 20, 20, 20, 5, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 13, 20, 20, 20, 9, 20, 5, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 10, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 20, 20, 20, 11, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 12, 20, 20, 20, 20, 5, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 20, 20, 20, 9, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 14, 20, 20, 20, 20, 5, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 13, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 2, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 17, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 18, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 15, 3, 4, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 2, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 2, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20]
]
state = 0
i = 0
while i < len(codes):
state = table[state][codes[i]]
i += 1
if state == 3:
self.result('YES')
else:
self.result('NO')
elif kind == 2:
table = [
[ 1, 2, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 2, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 3, 20, 20, 20, 20, 20, 20, 20],
[ 6, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 4, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 20, 5, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[ 1, 2, 20, 20, 20, 20, 5, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 8, 20, 20, 20, 7, 20, 20, 20, 20, 20, 10, 20, 20, 9, 20, 20, 20, 20, 20, 20],
[ 6, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 7, 20, 20, 20, 20, 20, 10, 20, 20, 9, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 20, 5, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 11, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 9, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20]
]
state = 0
i = 0
while i < len(codes):
state = table[state][codes[i]]
i += 1
if state == 5:
self.result('YES')
else:
self.result('NO')
elif kind == 3:
table = [
[ 1, 2, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 2, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 3, 20, 20, 20, 20, 20, 20, 20],
[ 7, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 4, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 20, 20, 20, 20, 5, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[13, 14, 20, 20, 20, 20, 20, 20, 20, 20, 6, 20, 20, 20, 20, 41, 20, 15, 20, 20, 20],
[ 1, 2, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 8, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 9, 20, 20, 20, 20, 20, 11, 20, 20, 10, 20, 20, 20, 20, 20, 20],
[ 7, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 20, 20, 20, 20, 5, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 12, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 9, 20, 20, 20, 20, 20, 11, 20, 20, 10, 20, 20, 20, 20, 20, 20],
[20, 14, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 15, 20, 20, 20],
[20, 20, 20, 20, 20, 22, 16, 21, 20, 20, 20, 17, 20, 20, 20, 20, 20, 39, 20, 20, 20],
[20, 14, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[13, 14, 20, 20, 20, 20, 16, 20, 20, 20, 6, 20, 20, 20, 20, 41, 20, 15, 20, 20, 20],
[20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 19, 20, 20, 20, 18, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 19, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 22, 16, 21, 20, 20, 20, 17, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 23, 20, 20, 23, 20, 20, 20, 24, 28, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[13, 14, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 15, 20, 20, 20],
[20, 20, 20, 20, 20, 36, 16, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 37, 20, 20, 20],
[20, 20, 20, 26, 20, 20, 20, 20, 25, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 36, 16, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 37, 20, 20, 20],
[20, 20, 20, 20, 20, 20, 20, 20, 25, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 30, 20, 20, 20, 32, 20, 29, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 36, 16, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 31, 20, 20, 20, 20, 29, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 30, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 34, 20, 20, 20, 20, 33, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 35, 20, 20, 20, 20, 29, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 20, 20, 20, 33, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 20, 20, 20, 32, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[13, 14, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 15, 20, 20, 20],
[20, 38, 20, 20, 38, 20, 20, 20, 24, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 20, 16, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 37, 20, 20, 20],
[20, 20, 20, 20, 20, 20, 20, 21, 20, 20, 20, 20, 20, 20, 20, 20, 20, 40, 20, 20, 20],
[20, 20, 20, 20, 20, 20, 16, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 42, 20, 42, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
[20, 20, 20, 20, 20, 20, 16, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 43, 20, 20, 20],
[20, 42, 20, 42, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20]
]
state = 0
i = 0
while i < len(codes):
state = table[state][codes[i]]
i += 1
if state == 6:
self.result('YES')
else:
self.result('NO')
def result(self, string):
with open('delossantos4.out', 'a') as file:
file.write(string + '\n')
file.close()
def main():
try:
fileinput = input('Enter input file: ')
with open(fileinput) as inp:
line = [ln.strip() for ln in inp]
if len(line) == 0:
raise ValueError('File is empty')
except FileNotFoundError:
print(fileinput, 'does not exist.')
main()
except ValueError as err:
print(err)
main()
open('delossantos4.out', 'w').close()
par = DFA(line)
par.process()
print('Done! Open delossantos4.out')
if __name__ == "__main__":
main()