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

  • 相关阅读:
    「小程序JAVA实战」小程序视频封面处理(48)
    「小程序JAVA实战」小程序视频处理工具ffmpeg(47)
    「小程序JAVA实战」小程序上传短视频(46)
    微信小程序跳转navigateTo与redirectTo
    微信小程序中this关键字使用技巧
    ***电商数据库设计参考:ecshop数据库+订单表结构等
    MYSQL中'TYPE=MyISAM'错误的解决方案
    PHP实现执行定时任务的几种思路详解
    PHP定时器实现每隔几秒运行一次
    ***LINUX添加PHP环境变量:CentOS下将php和mysql命令加入到环境变量中
  • 原文地址:https://www.cnblogs.com/ma6174/p/2407383.html
Copyright © 2020-2023  润新知