-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.py
executable file
·54 lines (52 loc) · 1.89 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
48
49
50
51
52
53
54
#!/usr/bin/python3
import sys
import lexer.table
import lexer.tokens
class Lexer:
def __init__(self, debug=False):
self.debug = debug
self.state = 0
self.tokenval = []
self.table = lexer.table.table
self.tokens = lexer.tokens.tokens
def prtoken(self):
print('<%s, %s>' % (self.tokens[self.state], ''.join(self.tokenval[:-1])))
def lex(self, infile):
linenum = 0
for line in infile:
linenum += 1
if self.debug:
print('line', linenum)
for char in line:
self.tokenval.append(char)
while True:
if self.debug:
print('-' * 10)
print('cur state', self.state)
print('new char', char)
# if able to proceed
if char in self.table[self.state]:
self.state = self.table[self.state][char]
if self.debug:
print('next state', self.state)
# if state is final state and unable to proceed
elif self.state in self.tokens:
self.prtoken()
self.state = 0
self.tokenval = self.tokenval[-1:]
continue
# if state is not final state and unable to proceed
else:
print('failed to lex at char [%s] at line %d[%s]' % (char, linenum, line))
return False
break
if self.state in self.tokens:
self.prtoken()
return True
else:
print('failed to lex')
return False
with open(sys.argv[1], 'r') as infile:
sys.stdout = open(sys.argv[1] + '.out', 'w')
lexer = Lexer()
lexer.lex(infile)