-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathres.py
More file actions
118 lines (94 loc) · 2.46 KB
/
res.py
File metadata and controls
118 lines (94 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import examples
import err
import utils.utils as tools
import utils.errorHelper as error
# 存放资源
# 关键字表
keyWords = [
'begin',
'end',
'if',
'then',
'while',
'do',
'const',
'var',
'call',
'procedure',
'odd'
]
# 算符与界符表
operators = [
'+',
'-',
'*',
'/',
'=',
'#',
'<',
'>',
':=',
'(',
')',
',',
'.',
';'
]
# 标识符表
identifiers = []
# 读入代码
code = ""
# 字符流,由code转化,方便处理
buffer = ""
# 词法分析器解析出来的单词二元组(种别,属性值)
words = []
class Word:
def __init__(self, category='none', attribute='none'):
self.category = category
self.attribute = attribute
def set(self, s: str):
success = False
# 关键字匹配
for keyword in sorted(keyWords, key=lambda t: len(t), reverse=True):
pos = s.find(keyword)
if pos != -1:
success = True
self.category = keyword
return success, len(keyword)
# 算符匹配
for operator in sorted(operators, key=lambda t: len(t), reverse=True):
pos = s.find(operator)
if pos == 0:
success = True
self.category = operator
return success, len(operator)
# 常数匹配
if s[0].isdigit():
pointer = 0
while pointer < len(s) and s[pointer].isdigit():
pointer += 1
success = True
self.category = 'number'
self.attribute = int(s[:pointer])
return success, pointer
# 标识符匹配
if s[0].isalpha():
pointer = 0
while pointer < len(s) and s[pointer].isalnum():
pointer += 1
if pointer > 10:
error.showError(err.LENGTH, s[:pointer])
return success, -1
success = True
self.category = 'ident'
self.attribute = tools.findPosition(identifiers, s[:pointer])
return success, pointer
error.showError(err.WORD, s)
return success, -1
if __name__ == '__main__':
word = Word()
# success, l = word.set(examples.exaDict['常数'].s)
# success, l = word.set(examples.exaDict['长度>10'].s)
success, l = word.set(examples.exaDict['非法字符'].s)
print(success, l)
print(word.category, word.attribute)