编译原理-词法分析器-python

import re
import codecs,sys
keyword = {'main':1, 'int':2, 'char':3, 'if':4, 'else':5, 'for':6, 'while':7,'return':8,'void':9, 'zhangyuge':10}
Symbol1 = {'=': 21, '+': 22, '-': 23, '*': 24, '/': 25, '>': 35, '<': 36, '>=': 37, '<=': 38, '==': 39, '!=': 40}
Symbol2 = {'(': 26, ')': 27, '[': 28, ']': 29, '{': 30, '}': 31, ',': 32, ':': 33, ';': 34}

#总正则表达式
# all=re.compile('(\d+\.\d+[eE][-+]?\d+|\d+\.\d+|[1-9]\d*|0[0-7]+|0x[0-9a-fA-F]+|[a-zA-Z_]\w*|&gt;&gt;|&lt;&lt;|::|-&gt;|\.|\+=|\-=|\*=|/=|%=|&gt;=|&lt;=|==|!=|&amp;&amp;|\|\||\+|\-|\*|/|=|&gt;|&lt;|!|^|%|~|\?|:|,|;|\(|\)|\[|\]|\{|\}|\'|\")')
all=re.compile('([0-9]+[a-z|A-Z|_]+[0-9]*|\d+\.\d+[eE][-+]?\d+|\d+\.\d+|[1-9]\d*|0[0-7]+|0x[0-9a-fA-F]+|'
               '[a-z|A-Z|0-9|_]*\".*?\"[a-z|A-Z|0-9|_]*|[a-zA-Z_]\w*|\".*\"|>>|<<|'
               '::|->|\+=|\-=|\*=|/=|%=|>=|<=|==|!=|&&|\|\||\+|\-|\*|/|=|>|'
               '<|!|\^|%|\~|\?|:|,|;|\(|\)|\[|\]|\{|\}|\"|\')')

def isfloat(str):
    try:
        float(str)
        return True
    except ValueError:
        return False

def Judge(s):
    if s[0].isalpha() and s in keyword:         #判断关键字
        print(s,':','关键字')
    elif s[0].isalpha() and s not in keyword and s.isalnum():     #判断标识符
        if len(s)>7:
            s = s[0:7]
            print(s, ':', '标识符')
    elif s.isdigit() or isfloat(s):                           #判断数字
        print(s, ':', '数字')
    elif s in Symbol1:                           #判断运算符或边界符
        print(s, ':', '运算符')
    elif s in Symbol2:                           #判断运算符或边界符
        print(s, ':', '边界符')
    else:
        if len(s) >= 2 and s[0] == '"' and s[-1] == '"':    #判断字符串
            print(s, ':', '字符串')
        else:                                   #没定义或者错误串
            print(s , ':' , '非法字符')

if __name__ == '__main__':
    #读取文件
    f = open("test.txt", "r" ,encoding="utf-8")
    result = []
    tmp=f.read().split('\n')
    for j in range(len(tmp)):
        line=tmp[j]
        flag=False
        for i in line:
           if '\u4e00'<i<'\u9fff':
               flag=True
               break
        if flag:
            print(str(j+1)+'行有中文错误:'+line)
            continue
        if len(line) == 1:
            result.extend(line)
        else:
            result.extend(all.findall(line))
    # print(result)
    # 去掉列表中残留的空字符
    for i in result:
        if '' in result:
            result.remove('')
    #词法分析
    for i in result:
        Judge(i)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值