• 编译原理词法分析程序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[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

    42     i-=1 

  • 相关阅读:
    不要控制!
    【转】iframe页面跳转时,导致父页面滚动!该怎么解决?
    【转】XML 特殊字符处理
    【转】使用Log4Net进行日志记录
    【转】JS获取浏览器可视区域的尺寸
    【转】Winform程序未捕获异常解决方法 EventType clr20r3 P1
    【转】VMware Tools installation cannot be started manually while Easy Install is in progress.
    如何解决安装VMware后郑广电宽带客户端不能登录的问题?
    MVC中的M是ViewModel不是EntityModel!
    纸上原型--纸上草稿设计--简单高效的沟通方式!
  • 原文地址:https://www.cnblogs.com/ma6174/p/2407383.html
Copyright © 2020-2023  润新知