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*|>>|<<|::|->|\.|\+=|\-=|\*=|/=|%=|>=|<=|==|!=|&&|\|\||\+|\-|\*|/|=|>|<|!|^|%|~|\?|:|,|;|\(|\)|\[|\]|\{|\}|\'|\")')
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)
12-02
2119
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
12-24
04-24
1140
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交