-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlexer.py
75 lines (56 loc) · 1.33 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import ply.lex as lex
import sys
def remove_slash(t):
return t.replace("\\\\", "\\").replace("\(", "(").replace("\)", ")").replace("\{", "{").replace("\}", "}").replace('\"', '"').replace('\"', '"')
tokens = [
'STATE',
'STATES',
'FINITE',
'INITIAL',
'BINDING',
'ALPHABET',
'TRANSITION',
'ASSIGNMENT',
'SYMBOL',
'SET',
]
t_ASSIGNMENT = r'='
t_TRANSITION = r'->'
t_STATES = r'STATES'
t_FINITE = r'FINITE'
t_INITIAL = r'INITIAL'
t_ALPHABET = r'ALPHABET'
t_BINDING = r'\+\+'
t_ignore = ' \t'
def t_newline(t):
r'\n+'
t.lexer.lineno += len(t.value)
def t_error(t):
print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
def t_SYMBOL(t):
r'"(([^\\]*?)|\\.)*?\"'
t.value = remove_slash(t.value[1:-1]).replace("\,", ",")
return t
def t_STATE(t):
r'\((([^\\]*?)|\\.)*?\)'
t.value = remove_slash(t.value[1:-1]).replace("\,", ",")
return t
def t_SET(t):
r'{(([^\\]*?)|\\.)*?}'
t.value = remove_slash(t.value[1:-1])
return t
lexer = lex.lex()
def main():
lexer = lex.lex()
rfile = open(sys.argv[1])
input = rfile.read()
lexer.input(input)
wfile = open(sys.argv[1] + ".out", 'w')
while True:
tok = lexer.token()
if not tok:
break
wfile.write(str(tok) + '\n')
if __name__ == "__main__":
main()