-
Notifications
You must be signed in to change notification settings - Fork 2
/
Semantic.py
349 lines (328 loc) · 14.7 KB
/
Semantic.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
from Token import *
from StateMachine import *
operation_priority = {'!': 0, '&&': -1, '||': -2, '(': -3, '+': 2, '+=': 2, '-': 2, '-=': 2, '*': 3, '*=': 3, '/': 3, '/=': 3}
main_operations = ['+', '+=', '-', '-=', '*', '*=', '/', '/=']
rel_operations = ['<', '<=', '==', '>', '>=']
operation_stack = []
variable_stack = []
defined_var = dict()
def get_value(var):
try:
res = int(var)
return [[],res,False]
except:
return defined_var[var]+[True]
def is_var(var):
try:
res = int(var)
return False
except:
return True
def calculate():
tmp_operation = operation_stack.pop()
if tmp_operation in ['-', '-=']:
first_num = get_value(variable_stack.pop())
second_num = get_value(variable_stack.pop())
if first_num is None or second_num is None :
return ['Variable is Undefined',False]
if first_num[2] == True :
if first_num[0] == 'int':
if second_num[2] == True :
if second_num[0] == 'int':
return [second_num[1] - first_num[1], True]
return [second_num[:-1], False]
return [second_num[1] - first_num[1], True]
return [first_num[:-1],False]
if second_num[2] == True:
if second_num[0] == 'int':
return [second_num[1] - first_num[1], True]
return [second_num[:-1], False]
return [second_num[1] - first_num[1], True]
if tmp_operation in ['+', '+=']:
first_num = get_value(variable_stack.pop())
second_num = get_value(variable_stack.pop())
if first_num is None or second_num is None :
return ['Variable is Undefined',False]
if first_num[2] == True:
if first_num[0] == 'int':
if second_num[2] == True:
if second_num[0] == 'int':
return [second_num[1] + first_num[1], True]
return [second_num[:-1], False]
return [second_num[1] + first_num[1], True]
return [first_num[:-1], False]
if second_num[2] == True:
if second_num[0] == 'int':
return [second_num[1] + first_num[1], True]
return [second_num[:-1], False]
return [second_num[1] + first_num[1], True]
if tmp_operation in ['*', '*=']:
first_num = get_value(variable_stack.pop())
second_num = get_value(variable_stack.pop())
if first_num is None or second_num is None :
return ['Variable is Undefined',False]
if first_num[2] == True:
if first_num[0] == 'int':
if second_num[2] == True:
if second_num[0] == 'int':
return [second_num[1] * first_num[1], True]
return [second_num[:-1], False]
return [second_num[1] * first_num[1], True]
return [first_num[:-1], False]
if second_num[2] == True:
if second_num[0] == 'int':
return [second_num[1] * first_num[1], True]
return [second_num[:-1], False]
return [second_num[1] * first_num[1], True]
if tmp_operation in ['/', '/=']:
first_num = get_value(variable_stack.pop())
second_num = get_value(variable_stack.pop())
if first_num is None or second_num is None :
return ['Variable is Undefined',False]
if first_num[2] == True:
if first_num[0] == 'int':
if second_num[2] == True:
if second_num[0] == 'int':
if first_num[1] == 0:
return ['Division by Zero', False]
return [second_num[1] / first_num[1], True]
return [second_num[:-1], False]
if first_num[1] == 0:
return ['Division by Zero', False]
return [second_num[1] / first_num[1], True]
return [first_num[:-1], False]
if second_num[2] == True:
if second_num[0] == 'int':
if first_num[1] == 0:
return ['Division by Zero', False]
return [second_num[1] / first_num[1], True]
return [second_num[:-1], False]
if first_num[1] == 0:
return ['Division by Zero', False]
return [second_num[1] / first_num[1], True]
def find_end(tokens,start_token,char):
token_enum = start_token + 0
if char == ')':
parentheses_count = 0
finded = False
while not finded:
if tokens[token_enum] == '(':
parentheses_count += 1
elif tokens[token_enum] == ')':
parentheses_count -= 1
if parentheses_count == 0:
return token_enum
token_enum += 1
return -1
else:
if char == '}':
brace_count = 0
finded = False
while not finded:
if tokens[token_enum] == '{':
brace_count += 1
elif tokens[token_enum] == '}':
brace_count -= 1
if brace_count == 0:
return token_enum
token_enum += 1
return -1
elif char == ';':
finded = False
while not finded and token_enum<len(tokens):
if tokens[token_enum] == ';':
return token_enum + 1
token_enum += 1
return -1
def check_expression(tokens,start=0,end=0):
current_state = 0
token_enum = start + 0
while token_enum < end and current_state >= 0:
tmp_token = tokens[token_enum].strip()
tmp_state = current_state + 0
current_state = expression_automata[current_state][token_expression_num(tokens[token_enum].strip())]
if current_state == 3:
if tmp_token in defined_var:
variable_stack.append(tmp_token)
else:
return ['Variable is not Defined',False,token_enum]
if current_state == 4 or current_state == 7:
if len(operation_stack) > 0 and operation_priority[operation_stack[-1]] > operation_priority[tmp_token] and tmp_token is not '(':
while len(operation_stack) > 0 and operation_priority[operation_stack[-1]] > operation_priority[tmp_token] and tmp_token is not '(':
cal_res = calculate()
if cal_res[1]:
variable_stack.append(cal_res[0])
else:
return cal_res+[token_enum]
elif len(operation_stack) > 0 and tmp_token != '(' and operation_stack[-1] != '(' and operation_priority[
operation_stack[-1]] == operation_priority[tmp_token]:
cal_res = calculate()
if cal_res[1]:
variable_stack.append(cal_res[0])
else:
return cal_res+[token_enum]
operation_stack.append(tmp_token)
if current_state == 2 or current_state == 6:
if tmp_token == ')':
while operation_stack[-1] is not '(':
cal_res = calculate()
if cal_res[1]:
variable_stack.append(cal_res[0])
else:
return cal_res+[token_enum]
operation_stack.pop()
else:
variable_stack.append(tmp_token)
if (tmp_state == 2 and current_state == 5) or (tmp_state == 6 and current_state == 0) or token_enum == end-1:
while len(operation_stack) > 0:
cal_res = calculate()
if cal_res[1]:
variable_stack.append(cal_res[0])
else:
return cal_res+[token_enum]
token_enum += 1
return [current_state,True,token_enum]
def sem_check(tokens,start=0,end=0):
return check_statement(tokens=tokens,start=start,end=end)
def check_statement(tokens,start=0,end=0):
current_state = 0
token_enum = start
token_end = end
while token_enum < token_end:
tmp_token = tokens[token_enum].strip()
tmp_state = current_state + 0
current_state = statement_automata[current_state][token_statement_num(tmp_token)]
if current_state == 13:
if defined_var[last_variable][0] != 'int':
return ['Variable Type is not Int',False,token_enum]
if tmp_token=='++':
defined_var[last_variable][1] += 1
else:
defined_var[last_variable][1] -= 1
if current_state == 12:
if tmp_token in defined_var:
last_variable = tmp_token
else:
return ['Variable is not Defined',False,token_enum]
if current_state == 9:
if tmp_token not in defined_var:
defined_var[tmp_token] = ['int',None]
last_variable = tmp_token
else:
return ['Variable is Defined Before',False,token_enum]
elif current_state == 2 :
if tmp_token not in defined_var:
defined_var[tmp_token] = ['char', None]
last_variable = tmp_token
else:
return ['Variable is Defined Before',False, token_enum]
elif current_state == 6:
if tmp_token not in defined_var:
defined_var[tmp_token] = ['bool', None]
last_variable = tmp_token
else:
return ['Variable is Defined Before',False, token_enum]
if current_state == 10 and tmp_token is not '=':
if tmp_token in ['+=', '-=', '*=', '/=']:
variable_stack.append(last_variable)
if len(operation_stack) > 0 and operation_priority[operation_stack[-1]] > operation_priority[tmp_token] and tmp_token is not '(':
while len(operation_stack) > 0 and operation_priority[operation_stack[-1]] > operation_priority[tmp_token] and tmp_token is not '(':
cal_res = calculate()
if cal_res[1]:
variable_stack.append(cal_res[0])
else:
return cal_res + [token_enum]
elif len(operation_stack)>0 and tmp_token!='(' and operation_stack[-1]!='(' and operation_priority[operation_stack[-1]] == operation_priority[tmp_token]:
cal_res = calculate()
if cal_res[1]:
variable_stack.append(cal_res[0])
else:
return cal_res + [token_enum]
operation_stack.append(tmp_token)
if current_state == 11:
if tmp_token == ')':
while operation_stack[-1] is not '(':
cal_res = calculate()
if cal_res[1]:
variable_stack.append(cal_res[0])
else:
return cal_res + [token_enum]
operation_stack.pop()
else:
variable_stack.append(tmp_token)
if tmp_state == 11 and current_state == 0:
while len(operation_stack)> 0:
cal_res = calculate()
if cal_res[1]:
variable_stack.append(cal_res[0])
else:
return cal_res + [token_enum]
defined_var[last_variable][1] = int(variable_stack.pop())
print(defined_var[last_variable])
if current_state == 0 and tmp_token == 'if':
result = check_if(tokens,start=token_enum+1)
if result[1]:
token_enum = result[0] + 1
else:
return result
elif current_state == 0 and tmp_token == 'while':
result = check_while(tokens,start=token_enum+1)
if result[1]:
token_enum = result[0] + 1
else:
return result
token_enum += 1
return [current_state , True , token_enum]
def check_if(tokens,start):
token_enum = start + 0
if tokens[token_enum] == '(':
start_statement = find_end(tokens, token_enum, char=')')
result = check_expression(tokens, start=token_enum + 1, end=start_statement)
if not result[1]:
return result
else:
if tokens[start_statement + 1] == '{':
end_statement = find_end(tokens, start_statement + 1, char='}')
statement_bool = check_statement(tokens=tokens, start=start_statement + 2, end=end_statement)
if not statement_bool[1]:
return [statement_bool[0] , False , statement_bool[2]]
else:
end_statement = find_end(tokens, start_statement + 1, char=';')
statement_bool = check_statement(tokens=tokens, start=start_statement + 1, end=end_statement)
if not statement_bool[1]:
return [statement_bool[0] , False , statement_bool[2]]
if len(tokens) > end_statement+1 and tokens[end_statement+1] == 'else':
if tokens[end_statement + 2] == '{':
end_statement_tmp = find_end(tokens, end_statement + 2, char='}')
statement_bool = check_statement(tokens=tokens, start=end_statement + 3, end=end_statement_tmp)
if not statement_bool[1]:
return [statement_bool[0] , False , statement_bool[2]]
else:
end_statement_tmp = find_end(tokens, end_statement + 2, char=';')
statement_bool = check_statement(tokens=tokens, start=end_statement + 2, end=end_statement_tmp)
if not statement_bool[1]:
return [statement_bool[0] , False , statement_bool[2]]
return [end_statement_tmp , True]
else:
return [end_statement , True]
return -1
def check_while(tokens,start):
token_enum = start + 0
if tokens[token_enum] == '(':
start_statement = find_end(tokens, token_enum, char=')')
result = check_expression(tokens, start=token_enum + 1, end=start_statement)
if not result[1]:
return result
else:
if tokens[start_statement+1] == '{':
end_statement = find_end(tokens, start_statement + 1, char='}')
result = check_statement(tokens=tokens, start=start_statement + 2, end=end_statement)
if not result[1]:
return result
else:
end_statement = find_end(tokens, start_statement + 1, char=';')
result = check_statement(tokens=tokens, start=start_statement + 1, end=end_statement)
if not result[1]:
return result
return [end_statement , True]
return -1