clex.py
6.41 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# clex.py: modified by Franck Pommereau (2018)
# ---------------------------------------------------------------
# clex.py
#
# Atul Varma
# Python C Compiler - Lexical Analyzer
# $Id: clex.py,v 1.2 2004/06/02 21:05:45 varmaa Exp $
# ---------------------------------------------------------------
import ply.lex as lex
import re
# ---------------------------------------------------------------
# TOKEN LIST
# ---------------------------------------------------------------
tokens = (
# Reserved words
'AUTO',
'BREAK',
'CASE',
'CHAR',
'CONST',
'CONTINUE',
'DEFAULT',
'DO',
'DOUBLE',
'ELSE',
'ENUM',
'EXTERN',
'FLOAT',
'FOR',
'GOTO',
'IF',
'INT',
'LONG',
'REGISTER',
'RETURN',
'SHORT',
'SIGNED',
'SIZEOF',
'STATIC',
'STRUCT',
'SWITCH',
'TYPEDEF',
'UNION',
'UNSIGNED',
'VOID',
'VOLATILE',
'WHILE',
# Special characters
'COMMA',
'COLON',
'SEMICOLON',
'LPAREN',
'RPAREN',
'LBRACKET',
'RBRACKET',
'LBRACE',
'RBRACE',
'ASSIGN',
'GREATER',
'LESS',
'EQ',
'NOT_EQ',
'GREATER_EQ',
'LESS_EQ',
'DOUBLE_PLUS',
'DOUBLE_MINUS',
'PLUS',
'MINUS',
# 'TIMES',
'DIV',
'MODULO',
'DOUBLE_AMPERSAND',
'DOUBLE_PIPE',
'EXCLAMATION',
'AMPERSAND',
'PIPE',
'CARET',
'ASTERISK',
'QUESTION',
'TILDE',
'POUND',
'DOT',
'ELLIPSIS',
'ARROW',
'SHIFT_LEFT',
'SHIFT_RIGHT',
'EQ_PLUS',
'EQ_MINUS',
'EQ_TIMES',
'EQ_DIV',
'EQ_MODULO',
'EQ_PIPE',
'EQ_AMPERSAND',
'EQ_CARET',
'EQ_SHIFT_LEFT',
'EQ_SHIFT_RIGHT',
# Complex tokens
'ID',
'FNUMBER',
'INUMBER',
'STRING',
'CHARACTER',
)
# ---------------------------------------------------------------
# RESERVED WORDS
# ---------------------------------------------------------------
reserved_words = {
'auto' : 'AUTO',
'break' : 'BREAK',
'case' : 'CASE',
'char' : 'CHAR',
'const' : 'CONST',
'continue' : 'CONTINUE',
'default' : 'DEFAULT',
'do' : 'DO',
'double' : 'DOUBLE',
'else' : 'ELSE',
'enum' : 'ENUM',
'extern' : 'EXTERN',
'float' : 'FLOAT',
'for' : 'FOR',
'goto' : 'GOTO',
'if' : 'IF',
'int' : 'INT',
'long' : 'LONG',
'register' : 'REGISTER',
'return' : 'RETURN',
'short' : 'SHORT',
'signed' : 'SIGNED',
'sizeof' : 'SIZEOF',
'static' : 'STATIC',
'struct' : 'STRUCT',
'switch' : 'SWITCH',
'typedef' : 'TYPEDEF',
'union' : 'UNION',
'unsigned' : 'UNSIGNED',
'void' : 'VOID',
'volatile' : 'VOLATILE',
'while' : 'WHILE'
}
# ---------------------------------------------------------------
# SPECIAL CHARACTERS
# ---------------------------------------------------------------
t_COMMA = r','
t_COLON = r':'
t_SEMICOLON = r';'
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_LBRACKET = r'\['
t_RBRACKET = r'\]'
t_LBRACE = r'{'
t_RBRACE = r'}'
t_ASSIGN = r'='
t_GREATER = r'>'
t_LESS = r'<'
t_EQ = r'=='
t_NOT_EQ = r'!='
t_GREATER_EQ = r'>='
t_LESS_EQ = r'<='
t_DOUBLE_PLUS = r'\+\+'
t_DOUBLE_MINUS = r'--'
t_PLUS = r'\+'
t_MINUS = r'-'
# t_TIMES = r'\*'
t_DIV = r'/(?!\*)'
t_MODULO = r'%'
t_DOUBLE_AMPERSAND = r'&&'
t_DOUBLE_PIPE = r'\|\|'
t_EXCLAMATION = r'!'
t_AMPERSAND = r'&'
t_PIPE = r'\|'
t_CARET = r'\^'
t_ASTERISK = r'\*'
t_QUESTION = r'\?'
t_TILDE = r'~'
t_POUND = r'\#'
t_ELLIPSIS = r'\.\.\.'
t_DOT = r'\.'
t_ARROW = r'->'
t_SHIFT_LEFT = r'<<'
t_SHIFT_RIGHT = r'>>'
t_EQ_PLUS = r'\+='
t_EQ_MINUS = r'-='
t_EQ_TIMES = r'\*='
t_EQ_DIV = r'/='
t_EQ_MODULO = r'%='
t_EQ_PIPE = r'\|='
t_EQ_AMPERSAND = r'&='
t_EQ_CARET = r'\^='
t_EQ_SHIFT_LEFT = r'<<='
t_EQ_SHIFT_RIGHT = r'>>='
# ---------------------------------------------------------------
# COMPLEX TOKENS
# ---------------------------------------------------------------
def t_ID(t):
r'[A-Za-z_][\w]*'
if t.value in reserved_words:
t.type = reserved_words[t.value]
return t
def t_FNUMBER(t):
r'((0(?!\d))|([1-9]\d*))((\.\d+(e[+-]?\d+)?)|(e[+-]?\d+))'
return t
def t_malformed_fnumber(t):
r'(0\d+)((\.\d+(e[+-]?\d+)?)|(e[+-]?\d+))'
print("Line %d. Malformed floating point number '%s'" % (t.lineno, t.value))
def t_INUMBER(t):
r'0(?!\d)|([1-9]\d*)'
return t
def t_malformed_inumber(t):
r'0\d+'
print("Line %d. Malformed integer '%s'" % (t.lineno, t.value))
def t_CHARACTER(t):
r"'\w'"
return t
def t_STRING(t):
r'"[^\n]*?(?<!\\)"'
temp_str = t.value.replace(r'\\', '')
m = re.search(r'\\[^n"]', temp_str)
if m != None:
print("Line %d. Unsupported character escape %s in string literal."
% (t.lineno, m.group(0)))
return
return t
# ---------------------------------------------------------------
# IGNORED TOKENS
# ---------------------------------------------------------------
def t_WHITESPACE(t):
r'[ \t]+'
pass
def t_NEWLINE(t):
r'\n+'
t.lexer.lineno += len(t.value)
def t_COMMENT(t):
r'/\*[\w\W]*?\*/'
t.lexer.lineno += t.value.count('\n')
pass
# ---------------------------------------------------------------
# ERROR HANDLING
# ---------------------------------------------------------------
def t_error(t):
print("Line %d." % (t.lineno,) + "",)
if t.value[0] == '"':
print("Unterminated string literal.")
if t.value.count('\n') > 0:
t.skip(t.value.index('\n'))
elif t.value[0:2] == '/*':
print("Unterminated comment.")
else:
print("Illegal character '%s'" % t.value[0])
t.skip(1)
# ---------------------------------------------------------------
# MAIN LEXER FUNCTIONALITY
# ---------------------------------------------------------------
def run_lexer():
"""This is just a debugging function that prints out a list of
tokens, it's not actually called by the compiler or anything."""
import sys
file = open(sys.argv[1])
lines = file.readlines()
file.close()
strings = ""
for i in lines:
strings += i
lex.input(strings)
while 1:
token = lex.token() # Get a token
if not token: break # No more tokens
print("(%s,'%s',%d)" % (token.type, token.value, token.lineno))
lex.lex()
if __name__ == '__main__':
run_lexer()
# ---------------------------------------------------------------
# End of clex.py
# ---------------------------------------------------------------