-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstatic_dynamic_functions.py
321 lines (277 loc) · 8.92 KB
/
static_dynamic_functions.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
import program_graph as pg
import program_token as pt
comparision_operators = ['>' , '<' , '>=' , '<=' , '==']
binary_operators = ['+', '-', '*', '/', '==', '>=', '<=', '>', '<']
variable_memory_mapping = { 'int': 4, 'float': 4, 'double': 8, 'char': 1}
def memory_usage(graph, initial_statment, variable_list, variable_count, lexer):
total_memory = 0
for key,value in variable_count.items():
total_memory = total_memory + variable_memory_mapping[key]*value
variable_access_status = data_flow(graph, initial_statment, variable_list)
while 1:
tok = lexer.token()
if not tok: break
if tok.type in ['INT' , 'FLOAT' , 'DOUBLE' , 'CHAR']:
variable_type = str(tok.type).lower()
tok = lexer.token()
if str(tok.value) in variable_access_status:
variable_count[variable_type] -= 1
tok = lexer.token()
while tok.type != 'SEMICOLON':
tok = lexer.token()
if tok.type == 'COMMA':
tok = lexer.token()
if tok.value in variable_access_status:
variable_count[variable_type] -= 1
memory_with_unused_variable = 0
for key,value in variable_count.items():
memory_with_unused_variable += variable_memory_mapping[key]*value
return [total_memory, memory_with_unused_variable]
def category_wise_variable_count(lexer):
variable_count = {}
variable_count['int'] = 0
variable_count['float'] = 0
variable_count['double'] = 0
variable_count['char'] = 0
while 1:
tok = lexer.token()
if not tok:
break
if tok.type in ['INT', 'FLOAT', 'DOUBLE', 'CHAR']:
variable_count[str(tok.type).lower()] = variable_count[str(tok.type).lower()] + 1
variable_type = tok.type
tok = lexer.token()
while tok.type != 'SEMICOLON':
tok = lexer.token()
if tok.type == 'COMMA':
variable_count[str(variable_type).lower()] = variable_count[str(variable_type).lower()] + 1
return variable_count
def list_of_varibales(lexer):
variable_list = {}
flg = 0
while 1:
tok = lexer.token()
if not tok: break
if tok.type in ['INT' , 'FLOAT' , 'DOUBLE' , 'CHAR']:
tok = lexer.token()
variable_list[tok.value] = 0
last_variable = tok.value
while tok.type != 'SEMICOLON':
tok = lexer.token()
if tok.type == 'COMMA':
tok = lexer.token()
variable_list[tok.value] = 0
last_variable = tok.value
elif tok.type == 'ASSIGN':
tok = lexer.token()
variable_list[last_variable] = tok.value;
return variable_list
def get_error_line_number(current_edge , data):
statment_list = pg.data_pre_processing(data)
for i in range(0 , len(statment_list)):
if statment_list[i] == current_edge:
print('Divide By Zero on Line Number: ',i+1)
break
def divide_by_zero(graph , initial_statment , variable_list , data):
divide_by_zero_list = []
statment_stack = []
statment_stack.append(initial_statment)
error_flg = 0
while 1:
if len(statment_stack) == 0:
break
current_edge = statment_stack[0]
del statment_stack[0]
next_edge = list(graph.edges(current_edge))
if len(next_edge) == 0:
break
i = 0
while i < len(next_edge):
statment_stack.append(next_edge[i][1])
i += 1
lexer = pt.get_lexer(current_edge)
while 1:
tok = lexer.token()
if not tok:
break
if tok.type == 'DIVIDE':
tok = lexer.token()
if tok.type == 'ID' and variable_list[tok.value] == 0 or tok.type == 'NUMBER' and tok.value == 0:
flg = 0
for i in range(0 , len(divide_by_zero_list)):
if divide_by_zero_list[i] == current_edge:
flg = 1
break
if flg != 1:
error_flg = 1
get_error_line_number(current_edge , data)
divide_by_zero_list.append(current_edge)
if error_flg == 0:
print('\nNone Divide by zero')
def data_flow(graph, initial_statment, variable_list):
variable_access_status = []
statment_stack = []
statment_stack.append(initial_statment)
used_variable_type = {}
used_variable_type['int'] = 0
used_variable_type['float'] = 0
used_variable_type['double'] = 0
used_variable_type['char'] = 0
if_flg = 0
ans_flg = 0
while 1:
if len(statment_stack) == 0:
break
current_edge = statment_stack[0]
del statment_stack[0]
lexer = pt.get_lexer(current_edge)
current_token = lexer.token()
previous_token = current_token
if 'if' in current_edge:
temp_edge = current_edge
temp_edge = temp_edge.replace('if(' , '')
temp_edge = temp_edge.replace(')' , '')
lexer = pt.get_lexer(temp_edge)
current_token = lexer.token()
lhs_variable
ans_flg = 0
if_flg = 1
operator_flg = 0
operator_type = 'NOTHING'
while 1:
if not current_token:
break
if operator_flg != 0:
if operator_type == '>':
if current_token.type == 'NUMBER':
if variable_list[lhs_variable] > current_token.value:
ans_flg = 1
else:
if variable_list[lhs_variable] > variable_list[current_token.value]:
ans_flg = 1
variable_access_status.append(lhs_variable)
else:
if current_token.value in comparision_operators:
operator_flg = 1
operator_type = current_token.value
else:
lhs_variable = current_token.value
current_token = lexer.token()
else:
assign_flg = 0
rhs_value = 0
operator_flg = 0
operator_type = "NOTHING"
while 1:
if not current_token:
break
if assign_flg == 1:
if current_token.type != 'ID' and current_token.type != 'NUMBER':
operator_flg = 1
operator_type = current_token.type
else:
if operator_flg == 1:
if operator_type == 'DIVIDE':
if current_token.type == 'NUMBER':
rhs_value = rhs_value / current_token.value
else:
rhs_value = rhs_value / variable_list[current_token.value]
variable_list[lhs_variable] = rhs_value
if operator_type == 'MULT':
if current_token.type == 'NUMBER':
rhs_value = rhs_value * current_token.value
else:
rhs_value = rhs_value * variable_list[current_token.value]
variable_list[lhs_variable] = rhs_value
if operator_type == 'PLUS':
if current_token.type == 'NUMBER':
rhs_value = rhs_value + current_token.value
else:
rhs_value = rhs_value + variable_list[current_token.value]
variable_list[lhs_variable] = rhs_value
if operator_type == 'MINUS':
if current_token.type == 'NUMBER':
rhs_value = rhs_value - current_token.value
else:
rhs_value = rhs_value - variable_list[current_token.value]
variable_list[lhs_variable] = rhs_value
if current_token.type != 'NUMBER' and current_token.value not in variable_access_status:
variable_access_status.append(current_token.value)
else:
if current_token.type == 'NUMBER':
rhs_value = current_token.value
else:
rhs_value = variable_list[current_token.value]
if current_token.value not in variable_access_status:
variable_access_status.append(current_token.value)
if current_token.type == 'ASSIGN':
lhs_variable = previous_token.value
assign_flg = 1
previous_token = current_token
current_token = lexer.token()
next_edge = list(graph.edges(current_edge))
if len(next_edge) == 0:
break
i = 0
while i < len(next_edge):
statment_stack.append(next_edge[i][1])
i += 1
if if_flg == 1:
if ans_flg == 1:
del statment_stack[1]
else:
del statment_stack[0]
if_flg = 0
return variable_access_status
def unused_variable_detection(graph, initial_statment, variable_list):
variable_access_status = data_flow(graph, initial_statment, variable_list)
error_flg = 0
for variable in variable_list.keys():
if variable not in variable_access_status:
error_flg = 1
print('Variable "',variable, '" is not used in program')
if error_flg == 0:
print('\nNo unused variable detected')
def balanced_parenthesis(symbolString):
s = []
balanced = True
index = 0
while index < len(symbolString) and balanced:
symbol = symbolString[index]
if symbol == "(":
s.append(symbol)
else:
if len(s) == 0 and symbol in ['(', ')']:
balanced = False
elif symbol == ')':
s.pop()
index = index + 1
if balanced and len(s) == 0:
return True
else:
return False
def parenthesis_checker(data):
statment_list = pg.data_pre_processing(data)
error_flg = 0
print('')
for i in range(0 , len(statment_list)):
if balanced_parenthesis(statment_list[i]) == False:
error_flg = 1
print('Unbalanced Parenthesis on Line Number: ',i+1)
if error_flg == 0:
print('\nBalanced Parenthesis')
def valid_expression(data):
statment_list = pg.data_pre_processing(data)
for i in range(0, len(statment_list)):
lexer = pt.get_lexer(statment_list[i])
current_token = ''
previous_token = ''
while 1:
if not current_token:
break
current_token = lexer.token()
if current_token.value in binary_operators:
if previous_token.type == 'ID':
current_token = lexer.token()
if current_token.type != 'ID':
print('Invalid Equation at Line Number: ', i+1)