-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer.py
More file actions
79 lines (68 loc) · 2.75 KB
/
lexer.py
File metadata and controls
79 lines (68 loc) · 2.75 KB
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
# A lexical analysis is the process of converting a sequence of characters into a sequence of lexical
# tokens. A program that performs lexical analysis may be termed a lexer, tokenizer or scanner.
# A lexer is generally combined with a parser, which together analyze the syntax of programming languages,
# web pages, and so forth
from tokens import Integer, Float, Operation, Declaration, Variable, Boolean, Comparison, Reserved
class Lexer:
digits = "0123456789"
operations = "+-/*()="
stop_words = [" "]
letters = "abcdefghijklmnopqrstuvwxyz"
declarations = ["let"]
boolean = ["and", "or", "not"]
comparisons = [">", "<", ">=", "<=", "=="]
special_chars = "><="
reserved = ["if", "elif", "else", "do", "while"]
def __init__(self, text):
self.text = text
self.idx = 0
self.tokens = []
self.char = self.text[self.idx]
self.token = None
def tokenize(self):
while self.idx < len(self.text):
if self.char in Lexer.digits:
self.token = self.extract_number()
elif self.char in Lexer.operations:
self.token = Operation(self.char)
self.move()
elif self.char in Lexer.stop_words:
self.move()
continue
elif self.char in Lexer.letters:
word = self.extract_word()
if word in Lexer.declarations:
self.token = Declaration(word)
elif word in Lexer.boolean:
self.token = Boolean(word)
elif word in Lexer.reserved:
self.token = Reserved(word)
else:
self.token = Variable(word)
elif self.char in Lexer.special_chars:
comparison_operator = ""
while self.char in Lexer.special_chars and self.idx < len(self.text):
comparison_operator += self.char
self.move()
self.token = Comparison(comparison_operator)
self.tokens.append(self.token)
return self.tokens
def extract_number(self):
number = ""
isFloat = False
while (self.char in Lexer.digits or self.char == ".") and (self.idx < len(self.text)):
if self.char == ".":
isFloat = True
number += self.char
self.move()
return Integer(number) if not isFloat else Float(number)
def extract_word(self):
word = ""
while self.char in Lexer.letters and self.idx < len(self.text):
word += self.char
self.move()
return word
def move(self):
self.idx += 1
if self.idx < len(self.text):
self.char = self.text[self.idx]