• 大小: 16KB
    文件类型: .zip
    金币: 2
    下载: 1 次
    发布日期: 2021-07-03
  • 语言: Python
  • 标签: 编译原理  python  

资源简介

编译原理的表达式计算器作业,上机实验题,用的是python写的

资源截图

代码片段和文件信息


import re
str=open(“2.txt“).readlines()
r=re.compile(r‘[0-9|+|\-|*|/|.|(|)]‘) #正则匹配合法字符


j=0
def judge(ab):    #判断是无符号整数还是浮点数
    if(little==0):
        print(“(5{0}{1}int)“.format(wordword))
    else:
        print(“(8{0}{1}double)“.format(wordword))

for s in str:
    s=s.lstrip()  #清除字符串前面的空格
    
    j+=1
    if j!=len(str):
       s=s[:-1]        #读文件操作
    print(“\n“) 
    print(“待分析的字符为:“)
    print(s)
    print(“分析的结果为:“)
    little=0             # 是否为小数状态 0表示整数 1表示浮点数
    word=‘‘              # 用字符串存数字
    index=0             # 计算当前遍历字符串的下标
    error=0           # 记录错误数
    Error=[]                # 存放错误信息
    for i in s:
        index+=1
        if(r.match(i)):
            d=re.compile(r‘[0-9]|\.‘)       # 匹配是否为数字或小数点
            if(d.match(i)):                  #
                word+=i
                if(i==‘.‘):                  # 若是小数点则把little改为1
                    little=1
                    
            if(i==‘+‘):
                if(word!=‘‘):                 # 如果word有存储数字则输出数字
                    judge(wordlittle)       #
                    word=‘‘                  # 输出数字后把dig和little改为初始化
                    little=0
                print(‘(1+NULLNULL)‘)
                
            if(i==‘-‘):
                if(word!=‘‘):
                    judge(wordlittle)
                    word=‘‘
                    little=0
                print(‘(2-NULLNULL)‘)
                
            if(i==‘*‘):
                if(word!=‘‘):
                    judge(wordlittle)
                    word=‘‘
                    little=0
                print(‘(3*NULLNULL)‘)
                
            if(i==‘/‘):
                if(word!=‘‘):
                    judge(wordlittle)
                    word=‘‘
                    little=0
                print(‘(4/NULLNULL)‘)
                
            if(i==‘(‘):
                if(word!=‘‘):
                    judge(wordlittle)
                    word=‘‘
                    little=0
                print(‘(6(NULLNULL)‘)
                
            if(i==‘)‘):
                if(word!=‘‘):
                    judge(wordlittle)
                    word=‘‘
                    little=0
                print(‘(7)NULLNULL)‘)
            if(i==‘ ‘):                       # 若遍历到空格则跳过   
                continue
            if(index==len(s) and word!=‘‘):    # 判断若数字结尾则输出数字
                judge(wordlittle)
                
        else:
            Error.append(“Error:第{0}个字符存在非法字符‘{1}‘“.format(indexi))    # 发现非法字符则把错误信息存入
            error+=1                                                    # error错误数增加
    if error!=0:                                                        # 按error的数进行遍历输出t中存储的错误信息
        print(‘ERROR‘)
        for i in range(error):
            print(Error[i])
        print(‘‘)
    else:
        print(‘‘)

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2019-01-12 22:04  计算器表达式\
     文件        3372  2019-01-12 22:02  计算器表达式\bianyi.py
     文件       16471  2019-01-12 22:04  计算器表达式\编译原理.docx

评论

共有 条评论