-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLexicalAnalyzer.py
More file actions
41 lines (40 loc) · 1.69 KB
/
Copy pathLexicalAnalyzer.py
File metadata and controls
41 lines (40 loc) · 1.69 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
class LexicalAnalyzer:
def __init__(self, file, dfa):
self.file = file
self.dfa = dfa
def get_next_token(self):
with open(self.file, mode='r') as input_file:
state = self.dfa.init_state
byte = input_file.read(1)
lexeme = ""
error_detection = False
while True:
lexeme += byte
last_state = state
state = state.get_next_state(byte)
if state is None:
error_detection = False
if last_state is None or last_state.goal_type is None:
error_detection = True
else:
if self.dfa.init_state.get_next_state(byte) is not None:
if byte:
yield (lexeme[:-1], last_state.goal_type, False)
else:
yield (lexeme, last_state.goal_type, False)
else:
error_detection = True
if error_detection:
yield (lexeme, last_state.goal_type, True)
state = self.dfa.init_state
last_state = None
lexeme = ""
else:
state = self.dfa.init_state
last_state = None
lexeme = ""
continue
if not byte and not error_detection and not lexeme:
break
byte = input_file.read(1)
yield ('', self.dfa.init_state.get_next_state('').goal_type, False)