-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDFA.py
213 lines (185 loc) · 8.77 KB
/
DFA.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
import json
class DFA:
def __init__(self):
# load the DFA
with open("./json/" + "NUM_DFA.json", "r") as f:
self.NUM_DFA = json.load(f)
with open("./json/" + "ID_DFA.json", "r") as f:
self.ID_DFA = json.load(f)
with open("./json/" + "SYMBOL_DFA.json", "r") as f:
self.SYMBOL_DFA = json.load(f)
with open("./json/" + "COMMENT_DFA.json", "r") as f:
self.COMMENT_DFA = json.load(f)
with open("./json/" + "WHITESPACE_DFA.json", "r") as f:
self.WHITESPACE_DFA = json.load(f)
# load alphabet
with open("./json/" + "alphabet.json", "r") as f:
self.alphabet = json.load(f)
# initialize DFA states
self.keywords = ["break", "else", "if", "int", "repeat", "return", "until", "void"]
self.reset()
def reset(self):
self.NUM_DFA_state = "start"
self.ID_DFA_state = "start"
self.SYMBOL_DFA_state = "start"
self.COMMENT_DFA_state = "start"
self.WHITESPACE_DFA_state = "start"
self.current_token = ""
self.current_tokens = []
def get_next_token(self, character):
self.current_token = character
token = ""
token_type = ""
repeat = False
# check if character is in alphabet
if not (self.COMMENT_DFA_state == "comment" or \
self.COMMENT_DFA_state == "* recognized"):
if ((character not in self.alphabet) and (character != "EOF")) \
or (character == "/" and self.COMMENT_DFA_state != "start"\
and self.COMMENT_DFA_state != "/ recognized" \
and self.COMMENT_DFA_state != "* in start"\
and self.SYMBOL_DFA_state != "containing symbol ="):
token_type = "error"
token = "(" + "".join(self.current_tokens) \
+ character + ", Invalid input)"
repeat = False
self.reset()
return token_type , token , repeat
# get next state for each DFA
# NUM_DFA
if self.NUM_DFA_state != "finished : is number" and self.NUM_DFA_state != "not_number" \
and self.NUM_DFA_state != "error invalid number":
current_NUM_DFA_state_dict = self.NUM_DFA[self.NUM_DFA_state]
if character in current_NUM_DFA_state_dict:
self.NUM_DFA_state = current_NUM_DFA_state_dict[character]
else:
if self.NUM_DFA_state == "start":
self.NUM_DFA_state = "not_number"
elif self.NUM_DFA_state == "containing number":
self.NUM_DFA_state = "finished : is number"
# ID_DFA
if self.ID_DFA_state != "finished : is identifier" and self.ID_DFA_state != "not_identifier":
current_ID_DFA_state_dict = self.ID_DFA[self.ID_DFA_state]
if character in current_ID_DFA_state_dict:
self.ID_DFA_state = current_ID_DFA_state_dict[character]
else:
if self.ID_DFA_state == "start":
self.ID_DFA_state = "not_identifier"
elif self.ID_DFA_state == "containing letter":
self.ID_DFA_state = "finished : is identifier"
# SYMBOL_DFA
if self.SYMBOL_DFA_state != "symbol recognized" and self.SYMBOL_DFA_state != "not_symbol":
current_SYMBOL_DFA_state_dict = self.SYMBOL_DFA[self.SYMBOL_DFA_state]
if character in current_SYMBOL_DFA_state_dict:
self.SYMBOL_DFA_state = current_SYMBOL_DFA_state_dict[character]
else:
if self.SYMBOL_DFA_state == "start":
self.SYMBOL_DFA_state = "not_symbol"
elif self.SYMBOL_DFA_state == "containing symbol =":
self.SYMBOL_DFA_state = "symbol recognized"
elif self.SYMBOL_DFA_state == "containing symbol *":
if character == "/" :
self.SYMBOL_DFA_state = "not_symbol"
else:
self.SYMBOL_DFA_state = "symbol recognized"
# COMMENT_DFA
if self.COMMENT_DFA_state != "comment finished" and self.COMMENT_DFA_state != "not_comment" \
and self.COMMENT_DFA_state != "error unclosed comment" and self.COMMENT_DFA_state != "error unmatched":
current_COMMENT_DFA_state_dict = self.COMMENT_DFA[self.COMMENT_DFA_state]
if character in current_COMMENT_DFA_state_dict:
self.COMMENT_DFA_state = current_COMMENT_DFA_state_dict[character]
else:
if self.COMMENT_DFA_state == "start":
self.COMMENT_DFA_state = "not_comment"
elif self.COMMENT_DFA_state == "comment":
if character == "EOF":
self.COMMENT_DFA_state = "error unclosed comment"
else:
self.COMMENT_DFA_state = "comment"
elif self.COMMENT_DFA_state == "* recognized":
if character == "*":
self.COMMENT_DFA_state = "* recognized"
else:
self.COMMENT_DFA_state = "comment"
elif self.COMMENT_DFA_state == "/ recognized":
token_type = "error"
token = "(" + "".join(self.current_tokens) \
+ ", Invalid input)"
repeat = True
self.reset()
return token_type , token , repeat
# WHITESPACE_DFA
if self.WHITESPACE_DFA_state != "whitespace recognized" and self.WHITESPACE_DFA_state != "not_whitespace":
current_WHITESPACE_DFA_state_dict = self.WHITESPACE_DFA[self.WHITESPACE_DFA_state]
if character in current_WHITESPACE_DFA_state_dict:
self.WHITESPACE_DFA_state = current_WHITESPACE_DFA_state_dict[character]
else:
if self.WHITESPACE_DFA_state == "start":
self.WHITESPACE_DFA_state = "not_whitespace"
# check if any DFA is in final state
if self.NUM_DFA_state == "finished : is number":
token_type = "NUM"
token = "".join(self.current_tokens)
repeat = True
self.reset()
return token_type , token , repeat
elif self.NUM_DFA_state == "error invalid number":
token_type = "error"
token = "(" + "".join(self.current_tokens) \
+ character + ", Invalid number)"
repeat = False
self.reset()
return token_type , token , repeat
elif self.ID_DFA_state == "finished : is identifier":
token_type = "ID"
token = "".join(self.current_tokens)
if token in self.keywords:
token_type = "KEYWORD"
repeat = True
self.reset()
return token_type , token , repeat
elif self.COMMENT_DFA_state == "comment finished":
token_type = "comment"
token = ""
repeat = False
self.reset()
return token_type , token , repeat
elif self.COMMENT_DFA_state == "error unclosed comment":
token_type = "error"
token = "(" + ("".join(self.current_tokens))[0:7] + "..." \
+ ", Unclosed comment)"
repeat = False
self.reset()
return token_type , token , repeat
elif self.COMMENT_DFA_state == "error unmatched":
token_type = "error"
token = "(" + ("".join(self.current_tokens)) + character \
+ ", Unmatched comment)"
repeat = False
self.reset()
return token_type , token , repeat
elif self.SYMBOL_DFA_state == "symbol recognized":
token_type = "SYMBOL"
if len(self.current_tokens) != 0:
if (self.current_tokens[-1] == "=" and character != "=") \
or (self.current_tokens[-1] == "*"):
token = "".join(self.current_tokens)
repeat = True
self.reset()
return token_type , token , repeat
token = "".join(self.current_tokens) + character
repeat = False
self.reset()
return token_type , token , repeat
elif self.WHITESPACE_DFA_state == "whitespace recognized":
token_type = "whitespace"
token = ""
repeat = False
self.reset()
return token_type , token , repeat
else:
self.current_tokens.append(character)
token_type = "not recognized"
token = ""
repeat = False
return token_type , token , repeat