/
lexical_analyser.py
236 lines (190 loc) · 8.06 KB
/
lexical_analyser.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
from token import Token
from function_definitions import function_definitions
from constants import constant_definitions
from placeholder import placeholder_definitions
numbers = "0123456789"
allowed_var_chars = "_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
alphabets = "_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
special_chars = "$"
operators = "+-*/%=^~√∛<>"
syntax = "();"
class Analyser:
def __init__(self, input_string, debug = False, log = False):
self.input_string = input_string
self.position = 0
self.line_stack = []
self.debug = debug
self.log = log
self.analyse()
def analyse(self):
if len(self.input_string) == 0:
logs = open("logs.txt", "a")
logs.write(self.__str__() + "\n")
logs.close()
if len(self.input_string) > 0 and self.input_string[0] == "#":
self.input_string = ""
self.checkForPlaceholders()
while self.position < len(self.input_string):
character = self.input_string[self.position]
if character in numbers:
self.insertNumber()
self.position -= 1
elif character == "'" or character == '"':
self.insertString(character)
elif character in alphabets + special_chars:
self.insertName()
self.position -= 1
elif character in operators:
self.insertOperator()
elif character in syntax:
self.insertSyntax()
elif character == "[":
self.insertList()
elif character == "{":
self.insertBlock()
elif character == "#":
self.input_string == ""
self.position += 1
if self.debug and len(self.input_string) > 0:
print(self)
if self.log and len(self.input_string) > 0:
logs = open("logs.txt", "a")
logs.write(self.__str__() + "\n")
logs.close()
def insertNumber(self):
number_string = ""
num_dots = 0
while self.position < len(self.input_string) and self.input_string[self.position] in numbers + ".":
if self.input_string[self.position] == ".":
num_dots += 1
if num_dots > 1:
break
number_string += self.input_string[self.position]
self.position += 1
if num_dots == 0:
value = int(number_string)
else:
value = float(number_string)
number_token = Token("NUMBER", value)
self.line_stack.append(number_token)
def insertString(self, beginner):
string = ""
self.position += 1
while self.position < len(self.input_string) and self.input_string[self.position] != beginner:
string += self.input_string[self.position]
self.position += 1
string_token = Token("STRING", string)
self.line_stack.append(string_token)
def insertName(self):
name_string = ""
while self.position < len(self.input_string) and self.input_string[self.position] in allowed_var_chars + special_chars:
name_string += self.input_string[self.position]
self.position += 1
if self.checkForFunctions(name_string):
pass
elif self.checkForConstants(name_string):
pass
else:
name_token = Token("NAME", name_string)
self.line_stack.append(name_token)
if name_token.value == "while" or name_token.value == "if" or name_token.value == "else_if" or name_token.value == "else":
self.insertCondition()
def insertOperator(self):
character = self.input_string[self.position]
for function in function_definitions:
if character in function.identifiers:
operator_token = Token(function.type, function.value)
self.line_stack.append(operator_token)
def insertSyntax(self):
character = self.input_string[self.position]
token_value = None
if character == "(":
token_value = "LPAREN"
elif character == ")":
token_value = "RPAREN"
elif character == ";":
token_value = "PERIOD"
symbol_token = Token("SYNTAX", token_value)
self.line_stack.append(symbol_token)
def insertList(self):
start_position = self.position
stop_position = self.getClosingSquareBracket(self.position)
child_analyser = Analyser(self.input_string[start_position + 1: stop_position], False, False)
items_stack = child_analyser.line_stack
new_list = Token("LIST", items = items_stack)
self.line_stack.append(new_list)
self.position = stop_position
def insertCondition(self):
start_position = self.position
stop_position = self.getOpeningBrace(start_position)
child_analyser = Analyser(self.input_string[start_position + 1: stop_position], False, False)
condition_items = child_analyser.line_stack
new_condition = Token("CONDITION", items = condition_items)
self.line_stack.append(new_condition)
self.position = stop_position
def insertBlock(self):
start_position = self.position
stop_position = self.getClosingBrace(self.position)
child_analyser = Analyser(self.input_string[start_position + 1: stop_position], False, False)
block_items = child_analyser.line_stack
new_block = Token("BLOCK", items = block_items)
self.line_stack.append(new_block)
self.position = stop_position
def checkForFunctions(self, name_string):
for function in function_definitions:
if name_string in function.identifiers:
function_token = Token(function.type, function.value)
self.line_stack.append(function_token)
return True
return False
def checkForConstants(self, name_string):
for constant in constant_definitions:
if name_string in constant.identifiers:
constant_token = Token("NUMBER", constant.value)
self.line_stack.append(constant_token)
return True
return False
def checkForPlaceholders(self):
for placeholder in placeholder_definitions:
for identifier in placeholder.identifiers:
self.input_string = self.input_string.replace(identifier, placeholder.replacement)
def getClosingSquareBracket(self, position):
num_opened = 0
current_position = position
while current_position < len(self.input_string):
if self.input_string[current_position] == "[":
num_opened += 1
elif self.input_string[current_position] == "]":
num_opened -= 1
if num_opened == 0:
return current_position
current_position += 1
return -1
def getOpeningBrace(self, position):
current_position = position
while current_position < len(self.input_string):
if self.input_string[current_position] == "{":
return current_position
current_position += 1
return -1
def getClosingBrace(self, position):
num_opened = 0
current_position = position
while current_position < len(self.input_string):
if self.input_string[current_position] == "{":
num_opened += 1
elif self.input_string[current_position] == "}":
num_opened -= 1
if num_opened == 0:
return current_position
current_position += 1
return -1
def __str__(self):
if len(self.line_stack) > 0:
output_string = "LEXICAL STACK: "
for token in self.line_stack:
output_string += token.getString() + " "
return output_string
else:
# without this, we get empty LEXICAL STACK for blank lines
return ""