-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlexer.py
47 lines (40 loc) · 1.44 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
# CONSTANTS
LETTER = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
DIGIT = "0123456789"
INPUTS = ["w", LETTER, DIGIT, "<", ">", "^", "=", "!", "h", "i", "l", "e", "$"]
# DFSM function
def dfsm(input_string):
table = {0: [1, 6, 7, 10, 13, 16, 18, 20, 6, 6, 6, 6, 6],
1: [6, 6, 6, 22, 22, 22, 22, 22, 2, 6, 6, 6, 6],
2: [6, 6, 6, 22, 22, 22, 22, 22, 6, 3, 6, 6, 6],
3: [6, 6, 6, 22, 22, 22, 22, 22, 6, 6, 4, 6, 6],
4: [6, 6, 6, 22, 22, 22, 22, 22, 6, 6, 6, 5, 6],
5: [22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22],
6: []}
state = 0
tokens = []
print(state)
for character in input_string:
col = char_to_col(character)
if state == 22:
print("ERROR, invalid input")
return False
else:
state = table[state][col]
print("state = " + str(state) + "\tcharacter = " + character + "\tcol = " + str(col))
if state == 5:
tokens.append(("keyword", "while"))
return tokens
# Gets column index from character
def char_to_col(character):
return INPUTS.index(character)
# MAIN
if __name__ == "__main__":
tokens = dfsm(input("Enter an input: "))
if len(tokens) != 0:
print("Terminated Successfully")
print("Type\tToken")
for token in tokens:
print(token[0] + "\t" + token[1])
else:
print("Terminated in error: no tokens found")