这里是我第一个python程序,是一个逆波兰式的表达式分析器,完全照抄另外一个C语言版本的,原作者看到了请见谅哈!
非常简陋,什么错误检测都没有,纯粹是练笔的东东:
1
#My first python
2
import sys
3
def parse(str):
4
"""Expression Parser"""
5
(s, exp, i, str) = ([], [], 0, "(" + str + ")#")
6
while str[i] != "#":
7
if (str[i] >= '0' and str[i] <= '9') or (str[i] >= 'a' and str[i] <= 'z'):
8
exp.append(str[i])
9
10
elif str[i] == '(':
11
s.append(str[i])
12
13
elif str[i] == ')':
14
while s[-1] != '(':
15
exp.append(s.pop())
16
s.pop()
17
18
elif str[i] in ['+', '-']:
19
while s[-1] != '(':
20
exp.append(s.pop())
21
s.append(str[i])
22
23
elif str[i] in ['*', '/']:
24
while s[-1] in ['*', '/']:
25
exp.append(s.pop())
26
s.append(str[i])
27
i += 1
28
print '=============='
29
print exp
30
31
if __name__ == "__main__":
32
parse("a-b*c/(3+6)")
33

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

要是python支持t-sql那样的between语法就好了。