-
Notifications
You must be signed in to change notification settings - Fork 2
/
Token.py
86 lines (79 loc) · 2 KB
/
Token.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
assignment_operators = ['=', '+=', '-=', '*=', '/=', '%=']
logical_operators = ['&&', '||', '!']
relational_operators = ['==', '!=', '>', '<', '>=', '<=']
arithmetic_operators = ['+', '-', '*', '/', '++', '--']
boolean = ['true', 'false']
def isVariable(token):
result = token[0].isalpha()
for i in token[1:]:
result = result and (i.isalpha() or i.isdigit())
return result
def isCharacter(token):
if len(token) == 3 and token[0] == '\'' and token[2] == '\'' and token[1].isalpha():
return True
def token_statement_num(token):
if token in relational_operators+logical_operators:
return 17
if token in boolean:
return 18
elif token in assignment_operators[1:]:
return 0
elif token in arithmetic_operators[:-2]:
return 1
elif token in arithmetic_operators[-2:]:
return 2
elif token == 'bool':
return 3
elif token == 'int':
return 4
elif token == 'char':
return 5
elif token == ';':
return 6
elif token == ',':
return 7
elif token == '=':
return 8
elif token == 'if':
return 9
elif token == 'while':
return 10
elif token == '(':
return 11
elif token == ')':
return 12
elif isVariable(token):
return 13
elif isCharacter(token):
return 14
else:
try:
int(token)
return 15
except:
return 16
def token_expression_num(token):
if token == '!':
return 0
elif token in logical_operators[:-1]:
return 1
elif token in relational_operators:
return 2
elif token in arithmetic_operators[:-2]:
return 3
elif token == '(':
return 4
elif token == ')':
return 5
elif token in boolean:
return 6
elif isCharacter(token):
return 7
elif isVariable(token):
return 8
else:
try:
int(token)
return 9
except:
return 10