-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathword.py
363 lines (338 loc) · 12.3 KB
/
word.py
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import sys
import string
class word:
def __init__(self,name):
self.keywards = {}
self.lines=[]
self.value=[]
# 关键字部分
self.keywards['auto'] = 101
self.keywards['break'] = 102
self.keywards['case'] = 103
self.keywards['char'] = 104
self.keywards['const'] = 105
self.keywards['continue'] = 106
self.keywards['default'] = 107
self.keywards['do'] = 108
self.keywards['double'] = 109
self.keywards['else'] = 110
self.keywards['enum'] = 111
self.keywards['extern'] = 112
self.keywards['float'] = 113
self.keywards['for'] = 114
self.keywards['goto'] = 115
self.keywards['if'] = 116
self.keywards['int'] = 117
self.keywards['long'] = 118
self.keywards['register'] = 119
self.keywards['return'] = 120
self.keywards['short'] = 121
self.keywards['signed'] = 122
self.keywards['sizeof'] = 123
self.keywards['static'] = 124
self.keywards['struct'] = 125
self.keywards['switch'] = 126
self.keywards['typedef'] = 127
self.keywards['union'] = 128
self.keywards['unsigned'] = 129
self.keywards['void'] = 130
self.keywards['volatile'] = 131
self.keywards['while'] = 132
self.keywards['#include'] = 133
self.keywards['main'] = 134
# 符号
self.keywards['+'] = 201
self.keywards['-'] = 202
self.keywards['*'] = 203
self.keywards['/'] = 204
self.keywards['='] = 205
self.keywards[':'] = 206
self.keywards['<'] = 207
self.keywards['>'] = 208
self.keywards['%'] = 209
self.keywards['&'] = 210
self.keywards['!'] = 211
self.keywards['('] = 212
self.keywards[')'] = 213
self.keywards['['] = 214
self.keywards[']'] = 215
self.keywards['{'] = 216
self.keywards['}'] = 217
self.keywards['#'] = 218
self.keywards['|'] = 219
self.keywards[','] = 220
self.keywards[';'] = 221
self.keywards['\''] = 222
self.keywards['\"'] = 223
self.keywards['!='] = 224
self.keywards['<>'] = 224
self.keywards['<='] = 225
self.keywards['=<'] = 225
self.keywards['=>'] = 226
self.keywards['>='] = 226
self.keywards['=='] = 227
# 变量
# self.keywards['var'] = 301
# 常量
# self.keywards['const'] = 401
# Error
# self.keywards['const'] = 501
self.signlist = {}
self.file=name
def pretreatment(self,file_name):
try:
fp_read = open(file_name, 'r')
fp_write = open('file.tmp', 'w')
sign = 0
while True:
read = fp_read.readline()
if not read:
break
length = len(read)
i = -1
while i < length - 1:
i += 1
if sign == 0:
if read[i] == ' ':
continue
#if read[i] == '\\':
# break #!!!!!未解决!!!!解决注释问题,本来是这样,但是c语言中的注释是两个字符以上,因此作出修改,在后面识别注释符
if read[i] == ' ':
if sign == 1:
continue
else:
sign = 1
fp_write.write(' ')
elif read[i] == '\t':
if sign == 1:
continue
else:
sign = 1
fp_write.write(' ')
elif read[i] == '\n':
if sign == 1:
continue
else:
sign = 1
fp_write.write(' ')
elif read[i] == '"':
fp_write.write(read[i])
i += 1
while i < length and read[i] != '"':
fp_write.write(read[i])
i += 1
if i >= length:
break
fp_write.write(read[i])
elif read[i] == "'":
fp_write.write(read[i])
i += 1
while i < length and read[i] != "'":
fp_write.write(read[i])
i += 1
if i >= length:
break
fp_write.write(read[i])
else:
sign = 3
fp_write.write(read[i])
except Exception:
print(file_name, ': This FileName Not Found!')
def save(self,string):
if string in self.keywards.keys():
self.lines.append(str(self.keywards[string]))
self.value.append(string)
if string not in self.signlist.keys():
self.signlist[string] = self.keywards[string]
else:
try:
float(string)
self.save_const(string)
except ValueError:
self.save_var(string)
def save_var(self,string):
if len(string.strip()) < 1:
pass
else:
self.lines.append('301')
self.value.append(string)
if string not in self.signlist.keys():
if len(string.strip()) < 1:
pass
else:
if self.is_signal(string) == 1:
self.signlist[string] = 301
else:
self.signlist[string] = 501
def save_const(self,string):
self.lines.append('401')
self.value.append(string)
if string not in self.signlist.keys():
self.signlist[string] = 401
def save_error(self,string):
if string not in self.signlist.keys():
self.signlist[string] = 501
def is_signal(self,s):
if s[0] == '_' or s[0] in string.ascii_letters:
for i in s:
if i in string.ascii_letters or i == '_' or i in string.digits:
pass
else:
return 0
return 1
else:
return 0
def recognition(self,filename):
try:
fp_read = open(filename, 'r')
string = ""
sign = 0 #sign=1处理单引号',sign=2处理双引号",sign=3处理双符号的,比如!=,<>,<=,=<,>=,=>,==
while True:
read = fp_read.read(1)
if not read:
break
if sign == 3:
if read == '=' or read == '<' or read == '>':
string += read
self.save(string)
string = ""
sign = 0
continue
else:
self.save(string)
string = ""
sign = 0
if read == ' ':
if len(string.strip()) < 1:
sign = 0
pass
else:
if sign == 1 or sign == 2:
string += read
else:
self.save(string)
string = ""
sign = 0
elif read == '(':
if sign == 1 or sign == 2:
string += read
else:
self.save(string)
string = ""
self.save('(')
elif read == ')':
if sign == 1 or sign == 2:
string += read
else:
self.save(string)
string = ""
self.save(')')
elif read == '[':
if sign == 1 or sign == 2:
string += read
else:
self.save(string)
string = ""
self.save('[')
elif read == ']':
if sign == 1 or sign == 2:
string += read
else:
self.save(string)
string = ""
self.save(']')
elif read == '{':
if sign == 1 or sign == 2:
string += read
else:
self.save(string)
string = ""
self.save('{')
elif read == '}':
if sign == 1 or sign == 2:
string += read
else:
self.save(string)
string = ""
self.save('}')
elif read == '<':
self.save(string)
string = "<"
sign = 3
#self.save('<')
elif read == '>':
self.save(string)
string = ">"
sign = 3
#self.save('>')
elif read == ',':
self.save(string)
string = ""
self.save(',')
elif read == "'":
string += read
if sign == 1:
sign = 0
self.save_const(string)
string = ""
else:
if sign != 2:
sign = 1
elif read == '"':
string += read
if sign == 2:
sign = 0
self.save_const(string)
string = ""
else:
if sign != 1:
sign = 2
elif read == ':':
if sign == 1 or sign == 2:
string += read
else:
self.save(string)
string = ""
self.save(':')
elif read == ';':
self.save(string)
string = ""
self.save(';')
elif read == '+':
self.save(string)
string = ""
self.save('+')
elif read == '=':
self.save(string)
string = "="
sign = 3
#self.save('=')
elif read == '!':
self.save(string)
string = "!"
sign = 3
#self.save('!')
else:
string += read
except Exception as e:
print(e)
def get_label(self):
if self.file == None:
print("Please Input FileName")
else:
self.pretreatment(self.file)
self.recognition('file.tmp')
for i in self.signlist.keys():
print("(", self.signlist[i], ",", i, ")")
self.dic_cont={}
for _ in self.value:
try:
float(_)
if (_.count('.')==1):
lei='float'
self.dic_cont[_]=[lei,float(_)]
else:
lei='int'
self.dic_cont[_]=[lei,int(_)]
except ValueError:
pass