-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner.py
232 lines (220 loc) · 8.66 KB
/
scanner.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
import string
class Scanner:
def __init__(self,text):
self.key_words = ["if", "while", "do", "else"]
self.operator = ["{", "}", ";", "+", "-", "/", "*", ">", "<", "!"]
self.digits = list(string.digits)
self.letters = list(string.ascii_letters)
self.withe_space = string.whitespace
self.state = 0
self.index = -1
self.line = 1
self.temp =""
self.code = text
def tokenizer(self):
while True:
match self.state:
case 0:
self.index += 1
if self.index > len(self.code)-1:
break
ch = self.code[self.index]
if ch in self.withe_space:
if ch == "\n":
self.line += 1
self.state = 0
elif ch == "{":
print("( { , { )")
self.state = 0
elif ch == "(":
print("( (, ( )")
self.state = 0
elif ch == ")":
print("( ) , ) )")
self.state = 0
elif ch == "}":
print("( } , } )")
self.state = 0
elif ch == "+":
print("(op , +)")
self.state = 0
elif ch == "-":
print("(op , -)")
self.state = 0
elif ch == "/":
print("(op , /)")
self.state = 0
elif ch == "*":
print("(op , *)")
self.state = 0
elif ch == "<":
self.state = 1
elif ch == ">":
self.state = 1
elif ch == "=":
self.state = 4
elif ch == ";":
print("( ; , ;)")
self.state = 0
elif ch == "!":
self.state = 2
elif ch == "$":
self.state = 3
elif ch in self.digits :
self.state = 5
elif ch in self.letters:
self.state = 6
else:
self.state = 9
case 1:
prev = ch
self.index += 1
ch = self.code[self.index]
if ch == "=":
print(f"(op, {prev}=)")
self.state = 0
continue
print(f"(op, {prev})")
self.index -= 1
self.state = 0
case 2:
prev = ch
self.index += 1
ch = self.code[self.index]
if ch == "=":
print(f"(op, {prev}=)")
self.state = 0
continue
print(f"(error, {prev} , line{self.line})")
self.index -= 1
self.state = 0
case 3:
if self.index == len(self.code)-1:
break
self.index += 1
if self.code[self.index] in self.withe_space:
print(f"(error, $ , line{self.line})")
self.state = 0
else:
self.index -= 1
self.state = 9
case 4:
prev = ch
self.index += 1
ch = self.code[self.index]
if ch == "=":
print("(op, ==)")
self.state = 0
continue
print("(assign_op , = )")
self.index -= 1
self.state = 0
case 5:
num = ch
self.index += 1
ch = self.code[self.index]
while ch in self.digits :
num += ch
self.index += 1
if self.index > len(self.code)-1:
break
ch = self.code[self.index]
if ch == ".":
self.state = 7
num += ch
elif ch == "E":
self.state = 8
num += ch
else:
print(f"(num , {num})")
num = ""
self.index -= 1
self.state = 0
case 6:
if not self.temp:
self.temp = ch
self.index += 1
ch = self.code[self.index]
while ch in self.digits or ch in self.letters:
self.temp += ch
self.index += 1
if self.index > len(self.code)-1:
break
ch = self.code[self.index]
if self.temp in self.key_words:
print(f"(keyword , {self.temp})")
self.temp = ""
self.index -= 1
self.state = 0
else:
print(f"(id , {self.temp})")
self.temp = ""
self.index -= 1
self.state = 0
case 7:
self.index += 1
ch = self.code[self.index]
if ch not in self.digits :
print(f"(error, {num} , line{self.line})")
num = ""
self.state = 0
self.index -= 1
continue
while ch in self.digits :
num += ch
self.index += 1
ch = self.code[self.index]
if ch == "E":
self.state = 8
num += ch
else:
print(f"(num , {num})")
num = ""
self.index -= 1
self.state = 0
case 8:
self.index += 1
ch = self.code[self.index]
if ch == "+" or ch == "-":
num += ch
self.index += 1
ch = self.code[self.index]
elif ch not in self.digits :
print(f"(error, {num} , line{self.line})")
num = ""
self.state = 0
self.index -= 1
continue
while ch in self.digits :
num += ch
self.index += 1
if self.index > len(self.code)-1:
break
ch = self.code[self.index]
print(f"(num , {num})")
num = ""
self.state = 0
self.index -= 1
case 9:
self.temp_error = ch
self.index += 1
if self.index > len(self.code)-1:
break
ch = self.code[self.index]
if ch in self.withe_space:
print(f"(error, {self.temp_error} , line{self.line})")
self.temp_error = ""
self.index -= 1
self.state = 0
continue
while ch not in self.digits and ch not in self.letters and ch not in self.operator and ch not in self.withe_space:
self.temp_error += ch
self.index += 1
if self.index > len(self.code)-1:
print(f"(error, {self.temp_error} , line{self.line})")
break
ch = self.code[self.index]
print(f"(error, {self.temp_error} , line{self.line})")
self.temp_error = ""
self.index -= 1
self.state = 0