首页 > 其他 > 详细

词法分析的设计与实现

时间:2019-10-11 16:12:45      阅读:78      评论:0      收藏:0      [点我收藏+]
 1 import re
 2 
 3 
 4 strs = "if sum >= 1000 then x : x - 1;#"+" "
 5 
 6 types = {‘begin‘:1, 
 7          ‘if‘:2,
 8          ‘then‘:3,
 9          ‘while‘:4,
10          ‘do‘:5,
11          ‘end‘:6,
12          ‘l(l|d)*‘:10,
13          ‘dd*‘:11,
14          ‘+‘:13,
15          ‘-‘:14,
16          ‘*‘:15,
17          ‘/‘:16,
18          ‘:‘:17,
19          ‘:=‘:18,
20          ‘<‘:20,
21          ‘<=‘:21,
22          ‘<>‘:22,
23          ‘>‘:23,
24          ‘>=‘:24,
25          ‘=‘:25,
26          ‘;‘:26,
27          ‘(‘:27,
28          ‘)‘:28,
29          ‘#‘:0
30         }
31 
32 if __name__ == ‘__main__‘:
33     # strs = input(‘请输入程序代码:‘)+" " #补位
34     
35     index = 0
36     while index < len(strs):
37         keyIndex = 0
38         for key in types.keys():
39             if index+len(key) < len(strs):
40                 if strs[index:index+len(key)] == key and not re.match(‘^[=a-zA-Z0-9_-]$‘, strs[index+len(key)]):
41                     if not(strs[index] == ‘=‘ and re.match(‘^[<>]$‘, strs[index-1])):
42                         ss = strs[index:index+len(key)]
43                         print((ss, types.get(ss)))
44                 elif re.match(‘^[a-zA-Z0-9_]+‘, strs[index:]):
45                     ss = re.match(‘^([a-zA-Z0-9_]+)‘, strs[index:]).group()
46                     if not types.get(ss):
47                         if re.match(‘[a-zA-Z]+‘, ss):
48                             print((ss, ‘标识符‘))
49                         elif re.match(‘\d+‘, ss):
50                             print((ss, ‘数字‘))
51                         else:
52                             print((ss, ‘其他‘))
53                     index += len(ss)
54             keyIndex+=1
55         index+=1

技术分享图片

 

 

以上代码知识参考软件1701卓志诚

 

词法分析的设计与实现

原文:https://www.cnblogs.com/veccchan/p/11653825.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!