-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.py
393 lines (332 loc) · 11.9 KB
/
parser.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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
from math import cos, sin
from lex_analyzer import LexAnalyzer
from token_type import TokenType
import turtle
wn = turtle.Screen()
wn.screensize(1200, 1000)
wn.setup(1200, 1000)
alex = turtle.Turtle()
alex.radians()
alex.pensize(6)
alex.speed(50)
print(alex.position())
alex.dot()
alex.penup()
class ExpressionNode:
"""表达式节点对象
Args:
token_type (TokenType): 节点对应的记号类型
Keyword Args:
const_value (float): 常量值
t (float): 参数 T
func (object): 内置函数
chile (ExpressionNode): 孩子节点,父节点对应的记号类型是 TokenType.func
left (ExpressionNode): 左孩子节点
right (ExpressionNode): 右孩子节点
"""
def __init__(self, token_type, **kwargs):
self.kind = token_type
if token_type == TokenType.const:
self.const = kwargs.get("const_value")
elif token_type == TokenType.t:
self.param = kwargs.get("t")
elif token_type == TokenType.func:
self.func = kwargs.get("func")
self.child = kwargs.get("child")
else:
self.left = kwargs.get("left")
self.right = kwargs.get("right")
def print_tree(self, indent: int):
"""递归打印语法树
Args:
indent (int): 语法树每一层对应的缩进
"""
for i in range(0, indent):
print("\t", end="")
print(">> ", end="")
tmp_type = self.kind
if tmp_type == TokenType.plus:
print("+")
elif tmp_type == TokenType.minus:
print("-")
elif tmp_type == TokenType.mul:
print("*")
elif tmp_type == TokenType.div:
print("/")
elif tmp_type == TokenType.power:
print("**")
elif tmp_type == TokenType.func:
print(self.func)
elif tmp_type == TokenType.const:
print(self.const)
elif tmp_type == TokenType.t:
print("T")
else:
print("错误的树节点")
if self.kind in [TokenType.const, TokenType.t]:
return
if self.kind == TokenType.func:
self.child.print_tree(indent + 1)
else:
self.left.print_tree(indent + 1)
self.right.print_tree(indent + 1)
def get_value(root):
"""计算表达式的值
Args:
root (ExpressionNode): 表达式的根节点
"""
if not root:
return 0.0
if root.kind == TokenType.plus:
return get_value(root.left) + get_value(root.right)
elif root.kind == TokenType.minus:
return get_value(root.left) - get_value(root.right)
elif root.kind == TokenType.mul:
return get_value(root.left) * get_value(root.right)
elif root.kind == TokenType.div:
return get_value(root.left) / get_value(root.right)
elif root.kind == TokenType.power:
return get_value(root.left) ** get_value(root.right)
elif root.kind == TokenType.func:
return root.func(get_value(root.child))
elif root.kind == TokenType.const:
return root.const
elif root.kind == TokenType.t:
return Property.parameter
else:
return 0.0
class Property:
"""语句属性
"""
parameter = 0.0
origin_x = 0.0
origin_y = 0.0
rot_ang = 0.0
scale_x = 1
scale_y = 1
def calc_coordinate(x, y):
"""计算点的坐标
Args:
x (ExpressionNode): x 坐标的表达式节点
y (ExpressionNode): y 坐标的表达式节点
"""
local_x = get_value(x)
local_y = get_value(y)
local_x *= Property.scale_x
local_y *= Property.scale_y
temp = local_x * cos(Property.rot_ang) + local_y * sin(Property.rot_ang)
local_y = local_y * cos(Property.rot_ang) - local_x * sin(Property.rot_ang)
local_x = temp
local_x += Property.origin_x
local_y += Property.origin_y
return local_x, local_y
def draw_pixel(x: float, y: float):
alex.goto(x, y)
alex.dot("black")
def draw_loop(start, end, step, x_expr, y_expr):
"""for-draw 语句中,根据起点、终点、步长、x 坐标、y 坐标画出图形
Args:
start (float): 起点
end (float): 终点
step (float): 步长
x_expr (ExpressionNode): x 坐标
y_expr (ExpressionNode): y 坐标
"""
Property.parameter = start
while Property.parameter <= end:
x_val, y_val = calc_coordinate(x_expr, y_expr)
draw_pixel(x_val, y_val)
Property.parameter += step
return
def set_origin(x, y):
Property.origin_x = x
Property.origin_y = y
def set_rot(angle):
Property.rot_ang = angle
def set_scale(x, y):
Property.scale_x = x
Property.scale_y = y
class Parser:
"""语法分析器
Attributes:
tokens (list): 记号流
index (int): 遍历记号流时当前记号的索引
tokens_length (int): 记号流长度
current (Token): 遍历记号流时记录当前记号
"""
def __init__(self, file):
"""初始化语法分析器,同时调用词法分析器获取记号流
Args:
file (file): 源程序文件
"""
lex_analyzer = LexAnalyzer(file)
self.tokens = lex_analyzer.get_token()
self.index = 0
self.tokens_length = len(self.tokens)
self.current = None
def fetch_token(self):
"""从记号流中获取记号,在 match_token() 中被调用"""
try:
self.current = self.tokens[self.index]
except IndexError:
print("语法分析结束")
if self.current.type == TokenType.err:
print("[No.{0}] 记号本身错误,请检查关键字是否拼写正确或者有非法的运算符".format(self.index))
# exit(1)
self.index += 1
def match_token(self, token_type):
"""根据预期的记号类型判断当前记号是否合法,不合法则输出错误信息,反之获取下一记号
Args:
token_type (TokenType): 预期的记号类型
"""
if self.current.type != token_type:
print("[No.{0}] 该记号种类为 ".format(self.index) + str(self.current.type))
print("期望的记号种类为 " + str(token_type))
if token_type == TokenType.semico:
print("缺少分号")
# exit(1)
print("[No.{0}] 记号类型不匹配".format(self.index))
# exit(1)
self.fetch_token()
def parse(self):
"""开始语法分析"""
self.fetch_token()
self.program()
def program(self):
"""遍历记号流"""
while self.index < self.tokens_length:
self.statement()
self.match_token(TokenType.semico)
def statement(self):
"""判断语句类型,调用对应语句的子程序"""
if self.current.type == TokenType.origin:
self.origin_statement()
elif self.current.type == TokenType.rot:
self.rot_statement()
elif self.current.type == TokenType.scale:
self.scale_statement()
elif self.current.type == TokenType.for_:
self.for_statement()
else:
print("[No.{0}] 不是合法的语句".format(self.index))
# exit(1)
def origin_statement(self):
"""origin 语句子程序,设置坐标原点"""
self.match_token(TokenType.origin)
self.match_token(TokenType.is_)
self.match_token(TokenType.l_bracket)
x_origin = self.expression()
x_origin.print_tree(0)
self.match_token(TokenType.comma)
y_origin = self.expression()
y_origin.print_tree(0)
self.match_token(TokenType.r_bracket)
set_origin(get_value(x_origin), get_value(y_origin))
alex.setposition(Property.origin_x, Property.origin_y)
return
def rot_statement(self):
"""rot 语句子程序,设置图形旋转角度"""
self.match_token(TokenType.rot)
self.match_token(TokenType.is_)
rot = self.expression()
rot.print_tree(0)
set_rot(get_value(rot))
alex.left(Property.rot_ang)
return
def scale_statement(self):
"""scale 语句子程序,设置坐标轴比例"""
self.match_token(TokenType.scale)
self.match_token(TokenType.is_)
self.match_token(TokenType.l_bracket)
x_scale = self.expression()
x_scale.print_tree(0)
self.match_token(TokenType.comma)
y_scale = self.expression()
y_scale.print_tree(0)
self.match_token(TokenType.r_bracket)
set_scale(get_value(x_scale), get_value(y_scale))
return
def for_statement(self):
"""for 语句子程序,并画出图形"""
self.match_token(TokenType.for_)
self.match_token(TokenType.t)
self.match_token(TokenType.from_)
start = self.expression()
start.print_tree(0)
self.match_token(TokenType.to)
end = self.expression()
end.print_tree(0)
self.match_token(TokenType.step)
step = self.expression()
step.print_tree(0)
self.match_token(TokenType.draw)
self.match_token(TokenType.l_bracket)
x = self.expression()
x.print_tree(0)
self.match_token(TokenType.comma)
y = self.expression()
y.print_tree(0)
self.match_token(TokenType.r_bracket)
draw_loop(get_value(start), get_value(end), get_value(step), x, y)
return
def expression(self):
"""构造表达式语法树"""
left = self.term()
while self.current.type in [TokenType.plus, TokenType.minus]:
token_tmp = self.current.type
self.match_token(token_tmp)
right = self.term()
left = ExpressionNode(token_tmp, left=left, right=right)
return left
def term(self):
left = self.factor()
while self.current.type in [TokenType.mul, TokenType.div]:
token_tmp = self.current.type
self.match_token(token_tmp)
right = self.factor()
left = ExpressionNode(token_tmp, left=left, right=right)
return left
def factor(self):
if self.current.type == TokenType.plus:
self.match_token(TokenType.plus)
right = self.factor()
elif self.current.type == TokenType.minus:
self.match_token(TokenType.minus)
right = self.factor()
left = ExpressionNode(TokenType.const, const_value=0.0)
right = ExpressionNode(TokenType.minus, left=left, right=right)
else:
right = self.component()
return right
def component(self):
left = self.atom()
if self.current.type == TokenType.power:
self.match_token(TokenType.power)
right = self.component()
left = ExpressionNode(TokenType.power, left=left, right=right)
return left
def atom(self):
tmp_token = self.current
if self.current.type == TokenType.const:
self.match_token(TokenType.const)
quark = ExpressionNode(tmp_token.type, const_value=tmp_token.value)
elif self.current.type == TokenType.t:
self.match_token(TokenType.t)
quark = ExpressionNode(tmp_token.type, t=Property.parameter)
elif self.current.type == TokenType.func:
self.match_token(TokenType.func)
self.match_token(TokenType.l_bracket)
temp = self.expression()
quark = ExpressionNode(tmp_token.type, func=tmp_token.func, child=temp)
self.match_token(TokenType.r_bracket)
elif self.current.type == TokenType.l_bracket:
self.match_token(TokenType.l_bracket)
quark = self.expression()
self.match_token(TokenType.r_bracket)
else:
raise ValueError("不是预期的记号")
return quark
if __name__ == '__main__':
parser = Parser("test.txt")
parser.parse()
turtle.done()