对python的应用还是不熟练,很多实用的方法没掌握,下面的程序本来是用C写的,为了练习一下python,又用python改写的,很粗糙,有bug,不过能运行出结果,嘿嘿,以后学好了python再来优化吧
# -*- coding: cp936 -*-
Keyword = ("begin","end","if","while","var","procedure","else","for","do","int","read","write")
Yunsuanfu = ('+','-','*','/','<','>','%','=')
Fenjiefu = (',',';','(',')','{','}',':')
Kongbai = (' ','\t','\n')
Number = ('0','1','2','3','4','5','6','7','8','9')
test="var a=10;\nvar b,c;\nprocedure p; \n\tbegin\n\t\tc=a+b\n\tend\n"
print test
length=len(test)
for i in range(length):
if test[i] in Kongbai:
continue
if test[i] in Fenjiefu:
print "分界符\t",test[i]
continue
if test[i] in Yunsuanfu:
print "运算符\t",test[i]
continue
if test[i-1] in Number:
continue
if test[i] in Number:
print "数字\t",
while test[i] in Number:
print test[i],
i+=1
print ''
if test[i-1] in Number or (test[i-1]>='a' and test[i-1]<='z'):
continue
j=0
temp = ""
while True:
if test[i] in Kongbai or test[i] in Yunsuanfu or test[i] in Fenjiefu:
break
else:
j+=1
i+=1
temp = test[i-j:i]
if temp in Keyword:
print "关键字\t",temp
else:
print "标识符\t",temp
Keyword = ("begin","end","if","while","var","procedure","else","for","do","int","read","write")
Yunsuanfu = ('+','-','*','/','<','>','%','=')
Fenjiefu = (',',';','(',')','{','}',':')
Kongbai = (' ','\t','\n')
Number = ('0','1','2','3','4','5','6','7','8','9')
test="var a=10;\nvar b,c;\nprocedure p; \n\tbegin\n\t\tc=a+b\n\tend\n"
print test
length=len(test)
for i in range(length):
if test[i] in Kongbai:
continue
if test[i] in Fenjiefu:
print "分界符\t",test[i]
continue
if test[i] in Yunsuanfu:
print "运算符\t",test[i]
continue
if test[i-1] in Number:
continue
if test[i] in Number:
print "数字\t",
while test[i] in Number:
print test[i],
i+=1
print ''
if test[i-1] in Number or (test[i-1]>='a' and test[i-1]<='z'):
continue
j=0
temp = ""
while True:
if test[i] in Kongbai or test[i] in Yunsuanfu or test[i] in Fenjiefu:
break
else:
j+=1
i+=1
temp = test[i-j:i]
if temp in Keyword:
print "关键字\t",temp
else:
print "标识符\t",temp
2012年3月21日 22:43:52改写的代码:
1 # -*- coding: cp936 -*-
2 Keyword = ("begin","end","if","while","var","procedure","else","for","do","int","read","write")
3 Yunsuanfu = ('+','-','*','/','<','>','%','=')
4 Fenjiefu = (',',';','(',')','{','}',':')
5 Kongbai = (' ','\t','\n')
6
7 test="var a=10;\nvar b,c;\nprocedure p; \n\tbegin\n\t\tc=a+b\n\tend\n"
8 print test
9 length=len(test)
10 i=-1
11 while i < length:
12 i+=1
13 if test[i] in Kongbai:
14 continue
15 if test[i] in Fenjiefu:
16 print "分界符\t",test[i]
17 continue
18 if test[i] in Yunsuanfu:
19 print "运算符\t",test[i]
20 continue
21 if test[i].isdigit():
22 print "数字\t",
23 while test[i].isdigit():
24 print test[i],
25 i+=1
26 print ''
27 i-=1
28 continue
29 j=0
30 temp = ""
31 while True:
32 if test[i] in Kongbai or test[i] in Yunsuanfu or test[i] in Fenjiefu:
33 break
34 else:
35 j+=1
36 i+=1
37 temp = test[i-j:i]
38 if temp in Keyword:
39 print "关键字\t",temp
40 else:
41 print "标识符\t",temp
2 Keyword = ("begin","end","if","while","var","procedure","else","for","do","int","read","write")
3 Yunsuanfu = ('+','-','*','/','<','>','%','=')
4 Fenjiefu = (',',';','(',')','{','}',':')
5 Kongbai = (' ','\t','\n')
6
7 test="var a=10;\nvar b,c;\nprocedure p; \n\tbegin\n\t\tc=a+b\n\tend\n"
8 print test
9 length=len(test)
10 i=-1
11 while i < length:
12 i+=1
13 if test[i] in Kongbai:
14 continue
15 if test[i] in Fenjiefu:
16 print "分界符\t",test[i]
17 continue
18 if test[i] in Yunsuanfu:
19 print "运算符\t",test[i]
20 continue
21 if test[i].isdigit():
22 print "数字\t",
23 while test[i].isdigit():
24 print test[i],
25 i+=1
26 print ''
27 i-=1
28 continue
29 j=0
30 temp = ""
31 while True:
32 if test[i] in Kongbai or test[i] in Yunsuanfu or test[i] in Fenjiefu:
33 break
34 else:
35 j+=1
36 i+=1
37 temp = test[i-j:i]
38 if temp in Keyword:
39 print "关键字\t",temp
40 else:
41 print "标识符\t",temp
42 i-=1
博主ma6174对本博客文章(除转载的)享有版权,未经许可不得用于商业用途。转载请注明出处http://www.cnblogs.com/ma6174/
对文章有啥看法或建议,可以评论或发电子邮件到ma6174@163.com
本文转自ma6174博客园博客,原文链接:http://www.cnblogs.com/ma6174/archive/2012/03/20/2407383.html,如需转载请自行联系原作者