-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsers.py
141 lines (123 loc) · 3.79 KB
/
parsers.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
from ast_node import *
from errors import *
from tokens import *
class ParserResult(object):
"""
语法解析结果
"""
def __init__(self):
self.error = None
self.node = None
def success(self, node):
self.node = node
return self
def failure(self, error):
self.error = error
return self
def register(self, res):
if isinstance(res, ParserResult):
if res.error:
self.error = res.error
return res.node
return res
class Parser(object):
"""
词法分析器
"""
def __init__(self, tokens):
self.tokens = tokens
self.tok_idx = -1
self.current_tok: Token = None
self.advance()
def advance(self):
self.tok_idx += 1
if self.tok_idx < len(self.tokens):
self.current_tok = self.tokens[self.tok_idx]
return self.current_tok
def parse(self):
res = self.expr()
if not res.error and self.current_tok.ttype != TT_EOF:
return res.failure(InvalidSyntaxError(
self.current_tok.pos_start.copy(),
self.current_tok.pos_end.copy(),
"Expected '+', '-', '*', or '/'"
))
return res
def factor(self):
"""
factor -> INT | FLOAT
-> (PLUS | MINUS ) factor
-> LPAREN expr RPAREN
:return:
"""
res = ParserResult()
tok = self.current_tok
"""
1 + 1
INT -> 1
tok = +
并不需要error判断,他只是一个简单的token
-1 + 1
MINUS -> 0
tok = 1
tok = factor
factor 非终止符
因为是非终止符,会继续匹配,匹配过程中,可能发现error
因此需要error判断
"""
if tok.ttype in (TT_INT, TT_FLOAT): # factor -> INT | FLOAT
res.register(self.advance())
return res.success(NumberNode(tok)) # (PLUS | MINUS ) factor
elif tok.ttype in (TT_PLUS, TT_MINUS):
res.register(self.advance())
factor = res.register(self.factor())
if res.error:
return res
return res.success(UnaryOpNode(tok, factor))
elif tok.ttype == TT_LPAREN:
res.register(self.advance())
expr = res.register(self.expr())
if res.error:
return res
if self.current_tok.ttype == TT_RPAREN:
res.register(self.advance())
return res.success(expr)
else:
return res.failure(InvalidSyntaxError(
self.current_tok.pos_start.copy(),
self.current_tok.pos_end.copy(),
"Expected ')'"
))
return res.failure(InvalidSyntaxError(
self.current_tok.pos_start.copy(),
self.current_tok.pos_end.copy(),
"Expected int or float"
))
def term(self):
"""
term -> factor ( MUL | DIV ) factor)*
:return:
"""
return self.bin_op(self.factor, (TT_MUL, TT_DIV))
def expr(self):
"""
exp -> term (( PLUS | MINUS ) term)*
:return:
"""
return self.bin_op(self.term, (TT_PLUS, TT_MINUS))
def bin_op(self, func, ops):
"""
common function for term and expr
递归调用,构建AST
:param func: function
:param ops:
:return:
"""
res = ParserResult()
left = res.register(func())
while self.current_tok.ttype in ops:
op_tok = self.current_tok
res.register(self.advance())
right = res.register(func())
left = BinOpNode(left, op_tok, right)
return res.success(left)