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
| # Token 类用于表示词法分析器生成的令牌
class Token:
def __init__(self, type, value=None):
self.type = type
self.value = value
# Lexer 类负责将输入文本解析成令牌流
class Lexer:
def __init__(self, text):
self.text = text # 要解析的文本
self.pos = 0 # 当前解析位置
# 获取下一个令牌
def get_next_token(self):
if self.pos >= len(self.text):
return Token("EOF") # 如果已经到达文本末尾,返回一个表示结束的令牌
current_char = self.text[self.pos]
# 如果当前字符是字母,则解析标识符
if current_char.isalpha():
identifier = ""
while self.pos < len(self.text) and self.text[self.pos].isalnum():
identifier += self.text[self.pos]
self.pos += 1
return Token("IDENTIFIER", identifier)
# 如果当前字符是数字,则解析数字
if current_char.isdigit():
self.pos += 1
return Token("NUMBER", int(current_char))
# 如果当前字符是运算符,则解析运算符
if current_char in "+-*/":
self.pos += 1
return Token("OPERATOR", current_char)
# 如果当前字符是等号,则解析为赋值符号
if current_char == "=":
self.pos += 1
return Token("ASSIGN", "=")
# 如果当前字符是分号,则解析为分号
if current_char == ";":
self.pos += 1
return Token("SEMICOLON", ";")
# 如果当前字符是左括号,则解析为左括号
if current_char == "(":
self.pos += 1
return Token("LPAREN", "(")
# 如果当前字符是右括号,则解析为右括号
if current_char == ")":
self.pos += 1
return Token("RPAREN", ")")
# 如果当前字符是空格或制表符,则忽略并获取下一个令牌
if current_char in " \t":
self.pos += 1
return self.get_next_token()
raise ValueError("Invalid character") # 如果遇到无法识别的字符,引发异常
# Parser 类负责解析令牌流并计算结果
class Parser:
def __init__(self, lexer):
self.lexer = lexer # 词法分析器
self.current_token = self.lexer.get_next_token() # 当前令牌
self.variables = {} # 存储变量名和值的字典
# 解析整个表达式
def parse(self):
results = []
while self.current_token.type != "EOF":
result = self.parse_statement()
results.append(result)
if self.current_token.type == "SEMICOLON":
self.eat("SEMICOLON")
return results
# 解析语句(赋值语句或表达式语句)
def parse_statement(self):
if self.current_token.type == "IDENTIFIER":
variable_name = self.current_token.value
self.eat("IDENTIFIER")
self.eat("ASSIGN")
expression_value = self.parse_expression()
self.variables[variable_name] = expression_value
return expression_value
elif self.current_token.type == "SEMICOLON":
self.eat("SEMICOLON")
return None # 处理分号
else:
return self.parse_expression()
# 解析表达式
def parse_expression(self, min_precedence=0):
left = self.parse_atom()
while self.current_token.type == "OPERATOR" and self.precedence(self.current_token.value) >= min_precedence:
operator = self.current_token.value
self.eat("OPERATOR")
right = self.parse_expression(self.precedence(operator) + 1)
left = self.apply_operator(left, operator, right)
return left
# 解析原子表达式(数字、变量、括号)
def parse_atom(self):
if self.current_token.type == "NUMBER":
value = self.current_token.value
self.eat("NUMBER")
return value
elif self.current_token.type == "IDENTIFIER":
variable_name = self.current_token.value
self.eat("IDENTIFIER")
if variable_name in self.variables:
return self.variables[variable_name]
else:
raise ValueError(f"Undefined variable: {variable_name}")
elif self.current_token.type == "LPAREN":
self.eat("LPAREN")
expression = self.parse_expression()
self.eat("RPAREN")
return expression
else:
raise ValueError("Invalid syntax")
# 吃掉一个令牌并获取下一个令牌
def eat(self, token_type):
if self.current_token.type == token_type:
self.current_token = self.lexer.get_next_token()
else:
raise ValueError("Unexpected token")
# 运算符优先级
def precedence(self, operator):
precedence = {"+": 1, "-": 1, "*": 2, "/": 2}
return precedence.get(operator, 0)
# 应用运算符
def apply_operator(self, left, operator, right):
if operator == "+":
return left + right
elif operator == "-":
return left - right
elif operator == "*":
return left * right
elif operator == "/":
return left / right
# 计算函数,接受一个表达式并返回计算结果
def calculate(expression):
lexer = Lexer(expression)
parser = Parser(lexer)
results = parser.parse()
return results
# 测试代码
expression = "x = 3 * (4-1); y = 2*x + 2;"
# expression = "7"
results = calculate(expression)
print(results) # 输出结果为 [9, 20]
|