-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
180 lines (136 loc) · 2.18 KB
/
main.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
import ply.lex as lex
tokens = (
'NAME',
'NUMBER',
'LBRACE',
'RBRACE',
'SEMI',
'STRING',
'FNL',
'FNR',
'EQUAL',
'LOG',
'INT',
'CHAR',
'FLOAT',
'NUMBER_FLOAT',
'CONST',
'COMMENT',
'SHORTLOG',
'FN',
'RETURN',
'END',
'VOID',
'ASTERISK',
'PERCENT',
'SLASH',
'PLUS',
'MINUS',
'IF',
'DOUBLEEQUAL',
'COMMA',
'INPUT',
'QUESTION',
'FOR',
'DOUBLEEPLUS',
'LTS',
'LTR',
'BREAK',
'PR',
'PL',
'WHILE',
'NOTEQUAL'
)
# t_STRING = '"[a-zA-Z][a-zA-Z0-9]*"'
t_STRING = '"[^\t\n\r\f\v]*"'
t_ASTERISK = '\*'
t_PERCENT = '%'
t_SLASH = '/'
t_PLUS = '\+'
t_DOUBLEEPLUS = '\+\+'
t_MINUS = '-'
t_NUMBER = '[0-9]+'
t_NUMBER_FLOAT = "[0-9]+\.[0-9]+"
t_LBRACE = '{'
t_RBRACE = '}'
t_SEMI = ';'
t_FNL = '\)'
t_LTS = '<'
t_LTR = '>'
t_FNR = '\('
t_FN = ':'
t_PR = '\['
t_PL = '\]'
t_DOUBLEEQUAL = "=="
t_EQUAL = '='
t_ignore = ' \t'
t_NAME = '[a-zA-Z][a-zA-Z0-9]*'
t_COMMA = ','
t_QUESTION = '\?'
t_NOTEQUAL = '!='
def t_WHILE(t):
r"""while"""
return t
def t_INT(t):
r"""int"""
return t
def t_BREAK(t):
r"""break"""
return t
def t_INPUT(t):
r"""input"""
return t
def t_FOR(t):
r"""for"""
return t
def t_IF(t):
r"""if"""
return t
def t_VOID(t):
r"""void"""
return t
def t_END(t):
r"""end"""
return t
def t_RETURN(t):
r"""return"""
return t
def t_CONST(t):
r"""const"""
return t
def t_FLOAT(t):
r"""float"""
return t
def t_CHAR(t):
r"""char"""
return t
def t_LOG(t):
r"""log"""
return t
def t_SHORTLOG(t):
r"""l"""
return t
def t_COMMENT(t):
"""/\*[\s\S]*?\*/|//.*"""
t.lexer.lineno += t.value.count('\n')
return t
def t_newline(t):
r"""\n+"""
t.lexer.lineno += len(t.value)
pass
def t_error(t):
print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
lexer = lex.lex()
def lex_test():
f = open('data.aioc', 'r')
data = f.read()
f.close()
lexer.input(data)
while True:
tok = lexer.token()
if not tok:
break
print(tok)
if __name__ == '__main__':
lex_test()