-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlexer.py
154 lines (142 loc) · 4.7 KB
/
lexer.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
import os, collections
from collections import namedtuple
class Lexer:
DICT = {
'k': collections.OrderedDict(), # 关键字
'p': collections.OrderedDict(), # 界符
'con': collections.OrderedDict(), # 常数
'c': collections.OrderedDict(), # 字符
's': collections.OrderedDict(), # 字符串
'i': collections.OrderedDict(), # 标识符
}
INPUT = []
TokenList = []
CUR_ROW = -1
CUR_LINE = 0
id = -1
Token = namedtuple('Token', 'type index val cur_line id') # 具名元组表示
def __incId(self):
self.id += 1
return self.id
def __init__(self):
path1 = os.path.abspath('lexer_static/keyword_list')
path2 = os.path.abspath('lexer_static/p_list')
self.k_list = []
self.p_list = []
with open(path1, 'r', encoding='utf-8') as f:
for i, item in enumerate(f.readlines()):
item = item.strip()
self.k_list.append(item)
with open(path2, 'r', encoding='utf-8') as f:
for i, item in enumerate(f.readlines()):
item = item.strip()
self.p_list.append(item)
def getInput(self, input_list):
'''
:param input_list:['...','...'] the string split by '\n'
:return:
'''
self.INPUT = input_list
def getNextChar(self):
self.CUR_ROW += 1
if self.CUR_LINE == len(self.INPUT):
return "END"
while self.CUR_ROW == len(self.INPUT[self.CUR_LINE]):
'''the end of each line or the line is empty'''
self.CUR_ROW = 0
self.CUR_LINE += 1
if self.CUR_LINE == len(self.INPUT):
return "END"
return self.INPUT[self.CUR_LINE][self.CUR_ROW]
def backOneStep(self):
self.CUR_ROW -= 1
def __getId(self, demo, typ):
return self.DICT[typ].setdefault(demo, len(self.DICT[typ]))
def scanner(self):
item = self.getNextChar().strip()
id = None
demo = None
typ = None
if item == '':
return None
elif item == "END":
return "END"
elif item.isalpha() or item == '_':
demo = ""
while item.isalpha() or item.isdigit() or item in ['_', '.', '[', ']']:
demo += item
if self.CUR_ROW == len(self.INPUT[self.CUR_LINE]) - 1:
self.CUR_LINE += 1
self.CUR_ROW = 0
break
else:
item = self.getNextChar()
self.backOneStep()
if demo in self.k_list:
id = self.__getId(demo, 'k')
typ = 'k'
else:
id = self.__getId(demo, 'i')
typ = 'i'
elif item.isdigit():
demo = ""
while item.isdigit() or item == ".":
demo += item
if self.CUR_ROW == len(self.INPUT[self.CUR_LINE]) - 1:
self.CUR_LINE += 1
self.CUR_ROW = 0
break
else:
item = self.getNextChar()
self.backOneStep()
id = self.__getId(demo, 'con')
typ = 'con'
elif item == '"':
demo = '"'
item = self.getNextChar()
demo += item
while item != '"':
item = self.getNextChar()
demo += item
id = self.__getId(demo, 's')
typ = 's'
elif item == "'":
demo = "'"
for i in range(2):
item = self.getNextChar()
demo += item
id = self.__getId(demo, 'c')
typ = 'c'
else:
item_next = self.getNextChar()
if item + item_next in self.p_list:
demo = item + item_next
id = self.__getId(demo, 'p')
elif item in self.p_list:
demo = item
id = self.__getId(demo, 'p')
self.backOneStep()
typ = 'p'
return self.Token(typ, id, demo, self.CUR_LINE, self.__incId())
def analyse(self):
TOKEN_LIST = []
while True:
tmp = self.scanner()
if tmp == "END":
break
if tmp:
TOKEN_LIST.append(tmp)
self.TokenList = TOKEN_LIST
return TOKEN_LIST
'''
if __name__ == "__main__":
lex = Lexer()
path = os.path.abspath('c_input')
with open(path, 'r', encoding='utf-8') as f:
INPUT = f.readlines()
# INPUT = ['int a=0;\n', 'a=a+4;\n', 'c="ss"\n']
lex.getInput(INPUT)
res = lex.analyse()
for tmp in res:
print(tmp)
'''