-
Notifications
You must be signed in to change notification settings - Fork 0
/
action_parser.py
482 lines (429 loc) · 14.3 KB
/
action_parser.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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# -*- coding: utf-8 -*-
# This file is part of sf2dve.
#
# sf2dve is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 2.1 of the License, or
# (at your option) any later version.
#
# sf2dve is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with sf2dve. If not, see <http://www.gnu.org/licenses/>.
"""
Created on Sun Nov 9 09:35:20 2014
@author: pavla
"""
from ply import lex, yacc
import os
from collections import OrderedDict
newVars = None
prefix = None
typeConversions = {
"int":["int", "int16", "int32", "uint8", "uint16", "uint32"],
"byte":["bool", "char", "int8"],
"modifiers":["long", "short", "signed", "unsigned"]
}
keywords = {
"const" : "CONST",
"bool" : "BOOL",
"char" : "CHAR",
"int" : "INT",
"long" : "LONG",
"short" : "SHORT",
"signed" : "SIGNED",
"unsigned" : "UNSIGNED",
"int8" : "INT8",
"int16" : "INT16",
"int32" : "INT32",
"uint8" : "UINT8",
"uint16" : "UINT16",
"uint32" : "UINT32"
}
tokens = (["COLON_ASSIGN", "RIGHT_ASSIGN", "LEFT_ASSIGN", "ADD_ASSIGN",
"SUB_ASSIGN", "MUL_ASSIGN", "DIV_ASSIGN", "MOD_ASSIGN",
"AND_ASSIGN", "XOR_ASSIGN", "OR_ASSIGN", "RIGHT_OP", "LEFT_OP",
"INC_OP", "DEC_OP", "AND_OP", "OR_OP", "LE_OP", "GE_OP", "EQ_OP",
"NE_OP", "LBRACE", "RBRACE", "LBRACKET", "RBRACKET", "NUMBER",
"IDENTIFIER", "NEWLINE"] + list(keywords.values()))
literals = ";,:=()&!~-+*/%<>^|?@"
t_ignore = " \t\r\f\v"
t_ignore_COMMENT = r"(//.*)|(/\*(.|\n)*?\*/)"
t_COLON_ASSIGN = r":="
t_RIGHT_ASSIGN = r">>="
t_LEFT_ASSIGN = r"<<="
t_ADD_ASSIGN = r"\+="
t_SUB_ASSIGN = r"-="
t_MUL_ASSIGN = r"\*="
t_DIV_ASSIGN = r"/="
t_MOD_ASSIGN = r"%="
t_AND_ASSIGN = r"&="
t_XOR_ASSIGN = r"^="
t_OR_ASSIGN = r"\|="
t_RIGHT_OP = r">>"
t_LEFT_OP = r"<<"
t_INC_OP = r"\+\+"
t_DEC_OP = r"--"
t_AND_OP = r"&&"
t_OR_OP = r"\|\|"
t_LE_OP = r"<="
t_GE_OP = r">="
t_EQ_OP = r"=="
t_NE_OP = r"!="
t_LBRACE = r"({|<%)"
t_RBRACE = r"(}|%>)"
t_LBRACKET = r"(\[|<:)"
t_RBRACKET = r"(\]|:>)"
t_NUMBER = r"[0-9]+"
def t_IDENTIFIER(t):
r"[a-zA-Z_][a-zA-Z_0-9]*"
t.type = keywords.get(t.value, "IDENTIFIER")
return t
def t_NEWLINE(t):
r"\n"
t.lexer.lineno += 1
return t
def t_error(t):
raise TypeError("Unknown text '%s'" % t.value)
def p_start(p):
"""start : empty
| block_items"""
if p[1] is None:
p[0] = []
else:
p[0] = p[1]
def p_empty(p):
"empty :"
pass
def p_compound_statement(p):
"""compound_statement : LBRACE RBRACE
| LBRACE block_items RBRACE"""
if len(p) == 4:
p[0] = p[2]
def p_block_items(p):
"""block_items : declaration block_items
| statement block_items
| declaration
| statement"""
if isinstance(p[1], str):
p[1] = [p[1]]
if len(p) == 3:
if p[1] is None:
p[0] = p[2]
else:
p[0] = p[1] + p[2]
else:
if p[1] is None:
p[0] = []
else:
p[0] = p[1]
def p_declaration(p):
"""declaration : type_specifiers ';'
| type_specifiers init_declarator_list ';'
| type_specifiers NEWLINE
| type_specifiers init_declarator_list NEWLINE"""
if len(p) == 4:
const = False
tempType = None
for varType in p[1]:
if varType == "const":
const = True
elif tempType == None:
if varType in typeConversions["byte"]:
tempType = varType
else:
tempType = "int"
elif (tempType == "int" and
varType not in typeConversions["modifiers"]):
raise ValueError("Error in declaration specifiers")
elif tempType == "bool":
raise ValueError("Error in declaration specifiers")
elif tempType == "byte":
if varType == "signed":
pass
elif varType == "unsigned":
tempType = "int"
else:
raise ValueError("Error in declaration specifiers")
if tempType == "int":
finalType = "int"
elif tempType in typeConversions["byte"]:
finalType = "byte"
for varInit in p[2]:
newVars[prefix + varInit[0]] = {"type":finalType, "const":const,
"init":varInit[1], "scope":"label"}
def p_type_specifiers(p):
"""type_specifiers : type_specifier
| type_specifier type_specifiers """
if len(p) == 2:
p[0] = [p[1]]
else:
p[0] = p[2] + [p[1]]
def p_type_specifier(p):
"""type_specifier : CONST
| BOOL
| CHAR
| SHORT
| INT
| LONG
| SIGNED
| UNSIGNED
| INT8
| INT16
| INT32
| UINT8
| UINT16
| UINT32"""
p[0] = p[1]
def p_init_declarator_list(p):
"""init_declarator_list : init_declarator
| init_declarator ',' init_declarator_list"""
if len(p) == 2:
p[0] = [p[1]]
else:
p[0] = [p[1]] + p[3]
def p_init_declarator(p):
"""init_declarator : declarator '=' initializer
| declarator"""
if len(p) == 2:
p[0] = (p[1], None)
else:
p[0] = (p[1], p[3])
def p_declarator(p):
"""declarator : IDENTIFIER
| '(' declarator ')'
| declarator LBRACKET assignment_expression RBRACKET
| declarator LBRACKET RBRACKET"""
if len(p) == 2:
if prefix + p[1] in newVars:
p[1] = prefix + p[1]
p[0] = p[1]
elif len(p) == 4:
p[0] = p[1] + p[2] + p[3]
else:
p[0] = p[1] + p[2] + p[3] + p[4]
def p_initializer(p):
"""initializer : LBRACE initializer_list RBRACE
| assignment_expression"""
if len(p) == 2:
p[0] = p[1]
else:
p[0] = p[1] + p[2] + p[3]
def p_initializer_list(p):
"""initializer_list : initializer
| initializer ','
| initializer ',' initializer_list"""
if len(p) == 2:
p[0] = p[1]
else:
p[0] = p[1] + p[2] + ' ' + p[3]
def p_statement(p):
"""statement : compound_statement
| expression_statement"""
p[0] = p[1]
def p_expression_statement(p):
"""expression_statement : ';'
| expression ';'
| NEWLINE
| expression NEWLINE"""
if len(p) == 3:
p[0] = p[1]
def p_expression(p):
"""expression : assignment_expression
| expression ',' assignment_expression"""
if len(p) == 2:
p[0] = p[1]
else:
p[0] = p[1] + ', ' + p[3]
def p_assignment_expression(p):
"""assignment_expression : inc_dec_assignment
| logical_or_expression
| unary_expression assignment_operator assignment_expression"""
if len(p) == 2:
p[0] = p[1]
elif p[2] == '=':
p[0] = p[1] + ' = ' + p[3]
elif p[2] == ':=':
p[0] = p[1] + ' = ' + p[3]
elif p[2] == "+=":
p[0] = p[1] + ' = (' + p[1] + ') + (' + p[3] + ')'
elif p[2] == "-=":
p[0] = p[1] + ' = (' + p[1] + ') - (' + p[3] + ')'
elif p[2] == "*=":
p[0] = p[1] + ' = (' + p[1] + ') * (' + p[3] + ')'
elif p[2] == "/=":
p[0] = p[1] + ' = (' + p[1] + ') / (' + p[3] + ')'
elif p[2] == "%=":
p[0] = p[1] + ' = (' + p[1] + ') % (' + p[3] + ')'
elif p[2] == "&=":
p[0] = p[1] + ' = (' + p[1] + ') & (' + p[3] + ')'
elif p[2] == "^=":
p[0] = p[1] + ' = (' + p[1] + ') ^ (' + p[3] + ')'
elif p[2] == "|=":
p[0] = p[1] + ' = (' + p[1] + ') | (' + p[3] + ')'
elif p[2] == ">>=":
p[0] = p[1] + ' = (' + p[1] + ') >> (' + p[3] + ')'
elif p[2] == "<<=":
p[0] = p[1] + ' = (' + p[1] + ') << (' + p[3] + ')'
else:
p[0] = p[1] + ' ' + p[2] + ' ' + p[3]
def p_inc_dec_assignment(p):
"""inc_dec_assignment : INC_OP unary_expression
| unary_expression INC_OP
| DEC_OP unary_expression
| unary_expression DEC_OP"""
if p[1] == "++":
p[0] = p[2] + " = " + p[2] + " + 1"
elif p[2] == "++":
p[0] = p[1] + " = " + p[1] + " + 1"
elif p[1] == "--":
p[0] = p[2] + " = " + p[2] + " - 1"
elif p[2] == "--":
p[0] = p[1] + " = " + p[1] + " - 1"
def p_assignment_operator(p):
"""assignment_operator : '='
| COLON_ASSIGN
| MUL_ASSIGN
| DIV_ASSIGN
| MOD_ASSIGN
| ADD_ASSIGN
| SUB_ASSIGN
| LEFT_ASSIGN
| RIGHT_ASSIGN
| AND_ASSIGN
| XOR_ASSIGN
| OR_ASSIGN"""
p[0] = p[1]
def p_logical_or_expression(p):
"""logical_or_expression : logical_and_expression
| logical_or_expression OR_OP logical_and_expression"""
if len(p) == 2:
p[0] = p[1]
else:
p[0] = p[1] + ' or ' + p[3]
def p_logical_and_expression(p):
"""logical_and_expression : inclusive_or_expression
| logical_and_expression AND_OP inclusive_or_expression"""
if len(p) == 2:
p[0] = p[1]
else:
p[0] = p[1] + ' and ' + p[3]
def p_inclusive_or_expression(p):
"""inclusive_or_expression : exclusive_or_expression
| inclusive_or_expression '|' exclusive_or_expression"""
if len(p) == 2:
p[0] = p[1]
else:
p[0] = p[1] + ' ' + p[2] + ' ' + p[3]
def p_exclusive_or_expression(p):
"""exclusive_or_expression : and_expression
| exclusive_or_expression '^' and_expression"""
if len(p) == 2:
p[0] = p[1]
else:
p[0] = p[1] + ' ' + p[2] + ' ' + p[3]
def p_and_expression(p):
"""and_expression : equality_expression
| and_expression '&' equality_expression"""
if len(p) == 2:
p[0] = p[1]
else:
p[0] = p[1] + ' ' + p[2] + ' ' + p[3]
def p_equality_expression(p):
"""equality_expression : relational_expression
| equality_expression EQ_OP relational_expression
| equality_expression NE_OP relational_expression"""
if len(p) == 2:
p[0] = p[1]
else:
p[0] = p[1] + ' ' + p[2] + ' ' + p[3]
def p_relational_expression(p):
"""relational_expression : shift_expression
| relational_expression '<' shift_expression
| relational_expression '>' shift_expression
| relational_expression LE_OP shift_expression
| relational_expression GE_OP shift_expression"""
if len(p) == 2:
p[0] = p[1]
else:
p[0] = p[1] + ' ' + p[2] + ' ' + p[3]
def p_shift_expression(p):
"""shift_expression : additive_expression
| shift_expression LEFT_OP additive_expression
| shift_expression RIGHT_OP additive_expression"""
if len(p) == 2:
p[0] = p[1]
else:
p[0] = p[1] + ' ' + p[2] + ' ' + p[3]
def p_additive_expression(p):
"""additive_expression : multiplicative_expression
| additive_expression '+' multiplicative_expression
| additive_expression '-' multiplicative_expression"""
if len(p) == 2:
p[0] = p[1]
else:
p[0] = p[1] + ' ' + p[2] + ' ' + p[3]
def p_multiplicative_expression(p):
"""multiplicative_expression : unary_expression
| multiplicative_expression '*' unary_expression
| multiplicative_expression '/' unary_expression
| multiplicative_expression '%' unary_expression"""
if len(p) == 2:
p[0] = p[1]
else:
p[0] = p[1] + ' ' + p[2] + ' ' + p[3]
def p_unary_expression(p):
"""unary_expression : primary_expression
| unary_operator unary_expression
| primary_expression LBRACKET expression RBRACKET"""
if len(p) == 2:
p[0] = p[1]
elif len(p) == 3 and p[1] == '+':
p[0] = p[2]
elif len(p) == 3:
p[0] = p[1] + p[2]
else:
p[0] = p[1] + p[2] + p[3] + p[4]
def p_unary_operator(p):
"""unary_operator : '+'
| '-'
| '~'
| '!'"""
if p[1] == '-' or p[1] == '~':
p[0] = p[1]
if p[1] == '!':
p[0] = ' not '
def p_primary_expression(p):
"""primary_expression : IDENTIFIER
| NUMBER
| '(' expression ')'"""
if len(p) == 2:
if prefix + p[1] in newVars:
p[1] = prefix + p[1]
p[0] = p[1]
else:
p[0] = p[1] + p[2] + p[3]
def p_error(p):
if p is None:
raise ValueError("Unknown error")
raise ValueError("Syntax error, line %s: %s" % (p.lineno, p.type))
directory = os.path.join(os.path.dirname(__file__),
"parser_tables",
os.path.basename(__file__).rsplit('.', 1)[0])
try:
os.makedirs(directory, exist_ok=True)
except OSError:
pass
lexer = lex.lex(debug=False, optimize=True, outputdir=directory)
parser = yacc.yacc(debug=False, optimize=True, outputdir=directory)
def parse(text, tempPrefix="", variables=None, lexer=lexer):
global prefix
global newVars
prefix = tempPrefix
if variables is None:
variables = OrderedDict()
newVars = variables
return (parser.parse(text, lexer), newVars)