-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparserMatlab.py
More file actions
380 lines (333 loc) · 11 KB
/
Copy pathparserMatlab.py
File metadata and controls
380 lines (333 loc) · 11 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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
from lexerMatlab import lexer
from semanticMatlab import variables, functions, func_var
import sys
def parser(tokens):
ast = []
i = 0
while i < len(tokens):
token = tokens[i]
if token[0] == 'ID' and i + 1 < len(tokens) and tokens[i + 1][0] == 'ASSIGN':
variable = token[1]
if (not variables.__contains__(variable)):
variables.add(variable)
expression = parse_assign_expression(tokens[i + 2:])
ast.append(('ASSIGN', variable, expression[0]))
i += expression[1]+1
elif token[0] == 'FUNCTION':
expression = parse_function_expression(tokens[i+1:])
ast.append(('FUNCTION', expression[0]))
i += expression[1]+1
elif token[0] == 'ID':
ast.append(('ID', token[1]))
variable = token[1]
if variable not in variables:
variables.add(variable)
elif token[0] == 'NUMBER':
ast.append(('NUMBER', token[1]))
elif token[0] == 'STRING':
ast.append(('STRING', token[1]))
elif token[0] == 'PLUS':
ast.append(('PLUS', None))
elif token[0] == 'MINUS':
ast.append(('MINUS', None))
elif token[0] == 'MULTIPLY':
ast.append(('MULTIPLY', None))
elif token[0] == 'DIVIDE':
ast.append(('DIVIDE', None))
elif token[0] == 'WHILE':
expression = parse_while_expression(tokens[i+2:])
ast.append(('WHILE', token[1], expression[0]))
i+= expression[1]+1
elif token[0] == 'FOR':
expression = parse_for_expression(tokens[i+1:])
ast.append(('FOR', token[1], expression[0]))
i+= expression[1]+1
elif token[0] == 'LBRACKET':
ast.append(('LBRACKET', None))
elif token[0] == 'RBRACKET':
ast.append(('RBRACKET', None))
elif token[0] =='END':
ast.append(('END', token[1]))
elif token[0] =='PRINT':
expression = parse_for_print(tokens[i+1:])
ast.append(('PRINT',token[1], expression[0]))
elif token[0] =='LOGIC':
expression = parse_logic_expression(tokens[i+1:])
ast.append(('LOGIC',token[1], expression[0]))
i+= expression[1]+1
i += 1
return ast
def error(cause, guilty):
match cause:
case "variable":
print(f"Ошибка: переменная '{guilty}' не была объявлена")
case "function exists":
print(f"Ошибка: функция '{guilty}' уже объявлена")
case "function not exists":
print(f"Ошибка: функция '{guilty}' не объявлена")
case "wrong type":
print(f"Ошибка: '{guilty}' — неверный тип данных")
case "missing symbol":
print(f"Ошибка: '{guilty}' — отсутствует символ")
case _:
print(f"Общая ошибка")
sys.exit(1)
def parse_logic_expression(tokens):
ast = []
i = 0
while i < len(tokens):
token = tokens[i]
if token[0] == 'NUMBER':
ast.append(('NUMBER', token[1]))
elif token[0] == 'STRING':
ast.append(('STRING', token[1]))
elif token[0] == 'PLUS':
ast.append(('PLUS', None))
elif token[0] == 'MINUS':
ast.append(('MINUS', None))
elif token[0] == 'MULTIPLY':
ast.append(('MULTIPLY', None))
elif token[0] == 'DIVIDE':
ast.append(('DIVIDE', None))
elif token[0] == 'COMPARE':
ast.append(('COMPARE', token[1]))
elif token[0] == 'ID':
variable = token[1]
if variable not in variables:
error("variable", variable)
else:
ast.append(('ID', variable))
else:
break
i += 1
return ast, i
def parse_for_print(tokens):
ast = []
i=0
while i < len(tokens):
token = tokens[i]
if token[0] == 'LPAREN':
pass
elif token[0] == 'NUMBER':
ast.append(('NUMBER', token[1]))
elif token[0] == 'STRING':
ast.append(('STRING', token[1]))
elif token[0] == 'PLUS':
ast.append(('PLUS', None))
elif token[0] == 'MINUS':
ast.append(('MINUS', None))
elif token[0] == 'MULTIPLY':
ast.append(('MULTIPLY', None))
elif token[0] == 'DIVIDE':
ast.append(('DIVIDE', None))
elif token[0] == 'LPAREN':
pass
elif token[0] == 'ID':
variable = token[1]
if variable not in variables and variable not in functions:
error("variable", variable)
else:
ast.append(('ID', variable))
else:
break
i += 1
return ast, i
def parse_for_expression(tokens):
ast = []
i=0
isNameOfCounter = True
while i < len(tokens):
token = tokens[i]
if token[0] == 'NUMBER':
ast.append(('NUMBER', token[1]))
elif token[0] == 'STRING':
error("wrong type", 'string')
elif token[0] == 'COLON':
ast.append(('COLON', None))
elif token[0] == 'ASSIGN':
ast.append(('ASSIGN', None))
elif token[0] == 'PLUS':
ast.append(('PLUS', None))
elif token[0] == 'MINUS':
ast.append(('MINUS', None))
elif token[0] == 'MULTIPLY':
ast.append(('MULTIPLY', None))
elif token[0] == 'DIVIDE':
ast.append(('DIVIDE', None))
elif token[0] == 'RPAREN':
ast.append(('RPAREN', None))
elif token[0] == 'LPAREN':
ast.append(('LPAREN', None))
elif token[0] == 'ID':
variable = token[1]
if isNameOfCounter:
ast.append(('ID', variable))
isNameOfCounter = False
elif variable not in variables and variable not in func_var:
error("variable", variable)
else:
ast.append(('ID', variable))
else:
break
i += 1
return ast, i
def parse_assign_expression(tokens):
ast = []
i = 0
isAssigned = False
while i < len(tokens):
token = tokens[i]
if token[0] == 'NUMBER':
ast.append(('NUMBER', token[1]))
isAssigned = True
elif token[0] == 'STRING':
ast.append(('STRING', token[1]))
isAssigned = True
elif token[0] == 'PLUS':
ast.append(('PLUS', None))
elif token[0] == 'MINUS':
ast.append(('MINUS', None))
elif token[0] == 'MULTIPLY':
ast.append(('MULTIPLY', None))
elif token[0] == 'DIVIDE':
ast.append(('DIVIDE', None))
elif token[0] == 'RPAREN':
ast.append(('RPAREN', None))
elif token[0] == 'LPAREN':
ast.append(('LPAREN', None))
elif token[0] == 'ID':
variable = token[1]
if variable not in variables and variable not in func_var and variable not in functions:
error("variable", variable)
else:
ast.append(('ID', variable))
isAssigned = True
else:
break
i += 1
if not isAssigned :
error('missing symbol', 'Обьявление')
return ast, i
def parse_function_expression(tokens):
ast = []
i = 0
args = False
name = False
answer = False
while i < len(tokens):
token = tokens[i]
if token[0] == 'NUMBER':
ast.append(('NUMBER', token[1]))
elif token[0] == 'STRING':
ast.append(('STRING', token[1]))
elif token[0] == 'PLUS':
ast.append(('PLUS', None))
elif token[0] == 'ASSIGN':
ast.append(('ASSIGN', None))
name = True
elif token[0] == 'MINUS':
ast.append(('MINUS', None))
elif token[0] == 'MULTIPLY':
ast.append(('MULTIPLY', None))
elif token[0] == 'DIVIDE':
ast.append(('DIVIDE', None))
elif token[0] == 'COMPARE':
ast.append(('COMPARE', token[1]))
elif token[0] == 'RBRACKET':
ast.append(('RBRACKET', None))
answer = False
elif token[0] == 'LBRACKET':
ast.append(('LBRACKET', None))
answer= True
elif token[0] == 'RBCRACKET':
ast.append(('RBCRACKET', None))
break
elif token[0] == 'LBCRACKET':
ast.append(('LBCRACKET', None))
elif token[0] == 'RPAREN':
ast.append(('RPAREN', None))
args = False
elif token[0] == 'LPAREN':
ast.append(('LPAREN', None))
args = True
elif token[0] == 'ID':
if args ==True:
if token[1] not in functions:
func_var.add(token[1])
ast.append(('FARGS', token[1]))
if answer ==True:
if token[1] not in functions:
func_var.add(token[1])
ast.append(('FANSWER', token[1]))
if name ==True:
if token[1] not in functions:
functions.add(token[1])
else:
error("function exists", token[1])
ast.append(('FNAME', token[1]))
name = False
else:
break
i += 1
return ast, i
def parse_while_expression(tokens):
ast = []
i = 0
while i < len(tokens):
token = tokens[i]
if token[0] == 'NUMBER':
ast.append(('NUMBER', token[1]))
elif token[0] == 'STRING':
ast.append(('STRING', token[1]))
elif token[0] == 'PLUS':
ast.append(('PLUS', None))
elif token[0] == 'MINUS':
ast.append(('MINUS', None))
elif token[0] == 'MULTIPLY':
ast.append(('MULTIPLY', None))
elif token[0] == 'DIVIDE':
ast.append(('DIVIDE', None))
elif token[0] == 'COMPARE':
ast.append(('COMPARE', token[1]))
elif token[0] == 'ID':
variable = token[1]
if variable not in variables:
error("variable", variable)
else:
ast.append(('ID', variable))
else:
break
i += 1
return ast, i
# Пример использования парсера
data = '''
x = 5 + 3;
z = 5;
y = z * 2;
text = 'abra';
l;
l;
l;
function [c] = myfun(b)
x = b;
c = x+b;
end
while (x>3)
a= a+1;
while (l<5)
l=l+1;
end
end
for c = 1:x
end
y = y + 1;
disp(y);
v = myfun(x);
'''
#tokens = lexer(data)
#ast = parser(tokens)
#for node in ast:
# print(node)
#variables = variables.clear
#func_var = func_var.clear
#functions = functions.clear