lujiguo115 发表于 2015-4-27 06:23:30

编译原理----词法分析程序----python语言版

对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 in Kongbai:
      continue
    if test in Fenjiefu:
      print "分界符\t",test
      continue
    if test in Yunsuanfu:
      print "运算符\t",test
      continue
    if test in Number:
      continue
    if test in Number:
      print "数字\t",
      while test in Number:
            print test,
            i+=1
      print ''
    if test in Number or (test>='a' and test
页: [1]
查看完整版本: 编译原理----词法分析程序----python语言版