-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathscanner.py
112 lines (106 loc) · 4.3 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
import io
class Scanner(object):
def __init__(self, tiny_code=""):
tiny_code.encode(encoding="utf-8")
tiny_code = tiny_code.translate(str.maketrans({"-": r"\-",
"]": r"\]",
"\\": r"\\",
"^": r"\^",
"$": r"\$",
"*": r"\*",
".": r"\.",
":": r"\:"}))
self.tiny_code = tiny_code
self.tokens_list = []
self.code_list = []
def setTinyCode(self, tiny_code):
tiny_code.encode(encoding="utf-8")
self.tiny_code = tiny_code
def scan(self):
tokens_list = []
for tiny_in in io.StringIO(self.tiny_code):
token_str = ""
special_chars = ['(', ')', '+', '-', '*', '/',
'=', ';', '<', '>', '<=', '>=']
reversed_words = ["if", "then", "else",
"end", "repeat", "until", "read", "write"]
state = "start"
i = 0
while i < len(tiny_in):
if tiny_in[i] in special_chars and state != "inassign" and state != "incomment":
if (token_str != ""):
tokens_list.append(token_str)
token_str = ""
tokens_list.append(tiny_in[i])
state = "start"
elif state == "start":
if tiny_in[i] == " ":
state = "start"
elif tiny_in[i].isalpha():
token_str += tiny_in[i]
state = "inid"
elif tiny_in[i].isdigit():
token_str += tiny_in[i]
state = "innum"
elif tiny_in[i] == ':':
token_str += tiny_in[i]
state = "inassign"
elif tiny_in[i] == '{':
token_str += tiny_in[i]
state = "incomment"
else:
state = "done"
elif state == "inid":
if tiny_in[i].isalpha():
token_str += tiny_in[i]
state = "inid"
else:
state = "done"
elif state == "innum":
if tiny_in[i].isdigit():
token_str += tiny_in[i]
state = "innum"
else:
state = "done"
elif state == "inassign":
if tiny_in[i] == "=":
token_str += tiny_in[i]
state = "done"
else:
state = "done"
elif state == "incomment":
if tiny_in[i] == "}":
token_str += tiny_in[i]
state = "start"
else:
token_str += tiny_in[i]
elif state == "done":
tokens_list.append(token_str)
token_str = ""
state = "start"
i -= 1
i += 1
if (token_str != ""):
tokens_list.append(token_str)
token_str = ""
tokens_output = []
for t in tokens_list:
if t in reversed_words:
tokens_output.append(t)
elif t in special_chars:
tokens_output.append(t)
elif t == ":=":
tokens_output.append(t)
elif t.isdigit():
tokens_output.append("number")
elif t.isalpha():
tokens_output.append("identifier")
else:
pass
# tokens_output.append("comment")
self.code_list = tokens_list
self.tokens_list = tokens_output
def createOutputFile(self, filename):
output_code = self.scan()
with open(filename, 'w+') as out:
out.write(output_code)