-
Notifications
You must be signed in to change notification settings - Fork 1
/
grammar.py
executable file
·369 lines (289 loc) · 9.61 KB
/
grammar.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
#!/usr/bin/python
import sys
sys.path.append("../..")
import ply.yacc as yacc
import errors
import declarations
from declarations import *
from mylexer import tokens
import mylexer
if "cmm" not in sys.argv[0]:
print ("usage : cmm inputfile")
raise SystemExit
GLOBAL="GLOBAL"
TMP="TMP"
############################################################################
var_global = Escopo(GLOBAL)
############################################################################
# Parsing rules
precedence = (
('left','LPAREN','RPAREN'),
('left','AND','OR'),
('left','MAIOR','MENOR', 'MAIOREQUALS', 'MENOREQUALS', 'EQUALS', 'DIFF'),
('left','PLUS','MINUS'),
('left','TIMES','DIVIDE'),
('right','UMINUS', 'NOT', 'TERNARY'),
)
#################################################################
#define
def p_empty(p):
'empty :'
pass
def p_define_end_of_instruction(p):
'end : SEMICOLON'
def p_literal(t):
'''literal : NUMBER
| TRUE
| FALSE
| NORMALSTRING
'''
t[0]=t[1]
def p_sequence_literal(t):
'''sequence_literal : literal COMMA sequence_literal
| literal'''
def p_define_type(t):
'''type : INT
| STRING
| BOOL'''
t[0]=t[1]
def p_variavel(t):
'''variavel : NAME
| NAME LCOLC expression RCOLC'''
t[0] = t[1]
#################################################################
# sequence = (example)+
# list = (example)*
#
#################################################################
def p_program(t):
'program : sequence_declaration'
t[0]=t[1]
def p_sequence_declaration(t):
'''sequence_declaration : declaration sequence_declaration
| declaration'''
t[0]=var_global
def p_declaration(t):
'''declaration : procedure
| function
| var_Declaration
'''
t[0]=t[1]
def p_var_declaration(t):
'''var_Declaration : type sequence_var_Especification end'''
tmp=t[2]
for element in tmp:
tmp[element].def_type(t[1])
var_global.add(tmp[element])
t[0]=tmp
def p_list_var_declaration(t):
'''list_var_Declaration : var_Declaration list_var_Declaration
| empty'''
global var_global
if(len(t)>2):
if(t[2] is not None):
tmp=t[1]
tmp.update(t[2])
t[0]=tmp
else:
t[0]=t[1]
def p_var_especification(t):
'''var_Especification : NAME LCOLC NUMBER RCOLC
| NAME ASSIGN expression
| NAME
| NAME LCOLC NUMBER RCOLC ASSIGN LBRACE sequence_literal RBRACE'''
if(len(t)==2):
t[0]=Variable(t[1], None, None)
elif(len(t)==4):
t[0]=Variable(t[1], None, t[3])
def p_sequence_var_Especification(t):
'''sequence_var_Especification : var_Especification COMMA sequence_var_Especification
| var_Especification
'''
if len(t)<4:
tmp={}
else:
tmp=t[3]
tmp[t[1].name]=t[1]
t[0]=tmp
###############################################################
def p_define_parametro(t):
'''parametro : type NAME
| type NAME LCOLC RCOLC'''
tmp={}
tmp[t[2]]=Variable(t[2], t[1], None)
t[0]= tmp
def p_list_parametro(t):
'''list_parametro : sequence_parametro
| empty'''
t[0]=t[1]
def p_sequence_parametro(t):
'''sequence_parametro : parametro COMMA sequence_parametro
| parametro'''
if(len(t)>2):
t[0]=[t[1], t[3]]
else:
t[0]=t[1]
def p_procedure(t):
'''procedure : NAME LPAREN list_parametro RPAREN LBRACE block RBRACE'''
p=Procedure(t[1], t[3], Block(t[2],t[6][0],t[6][1]))
var_global.add(p)
t[0]=p
def p_function(t):
'''function : type NAME LPAREN list_parametro RPAREN LBRACE block RBRACE'''
f=Function(t[2], t[1], t[4],Block(t[2],t[7][0],t[7][1]))
var_global.add(f)
t[0]=f
################################################################################
#expression
def p_ternary(p):
'''expression : expression INTERROGATION expression COLON expression %prec TERNARY
'''
p[0] = p[3] if p[1] else p[5]
def p_expression_uminus(p):
'expression : MINUS expression %prec UMINUS'
p[0] = -p[2]
def p_expression_not(p):
'expression : EXPLAMATION expression %prec NOT'
p[0] = not p[2]
def p_expression(p):
'expression : LPAREN expression RPAREN'
p[0] = p[2]
def p_define_expression_literal(t):
'expression : literal'
t[0]=t[1]
def p_define_expression_var(t):
'expression : variavel'
t[0]=var_global.show(t[1])
def p_expression_logop(t):
'''expression : expression MAIOR expression
| expression MENOR expression
| expression MAIOREQUALS expression
| expression MENOREQUALS expression
| expression EQUALS expression
| expression DIFF expression
| expression AND expression
| expression OR expression'''
if t[2] == '>' : t[0] = t[1] > t[3]
elif t[2] == '<': t[0] = t[1] < t[3]
elif t[2] == '>=': t[0] = t[1] >= t[3]
elif t[2] == '<=': t[0] = t[1] <= t[3]
elif t[2] == '==': t[0] = t[1] == t[3]
elif t[2] == '!=': t[0] = t[1] != t[3]
elif t[2] == '&&': t[0] = t[1] and t[3]
elif t[2] == '||': t[0] = t[1] or t[3]
else: errors.unknownSignal(t)
def p_binary_operators(p):
'''expression : expression PLUS expression
| expression MINUS expression
| expression TIMES expression
| expression DIVIDE expression
'''
if 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[3]
elif p[2] == '/':
p[0] = p[1] / p[3]
else: errors.unknownSignal(t)
def p_define_expression_subcall(t):
'expression : subCall_statement end'
t[0]=t[1]
def p_list_expression(t):
'''list_expression : sequence_expression
| empty'''
t[0]=t[1]
def p_sequence_expression(t):
'''sequence_expression : expression COMMA sequence_expression
| expression'''
t[0]= [t[1]] +t[3] if (len(t)>2) else [t[1]]
def p_assign(p):
'''assignment : variavel ASSIGN expression
| variavel MOD expression
| variavel SUMEQUALS expression
| variavel MINUSEQUALS expression
| variavel TIMESEQUALS expression
| variavel DIVIDEEQUALS expression
'''
if p[2] == '=':
var_global.change(p[1], p[3])
elif p[2] == '%=':
var_global.change(p[1], var_global.show(p[1]) /p[3])
elif p[2] == '+=':
var_global.change(p[1], var_global.show(p[1]) +p[3])
elif p[2] == '-=':
var_global.change(p[1], var_global.show(p[1])-p[3])
elif p[2] == '*=':
var_global.change(p[1], var_global.show(p[1]) *p[3])
elif p[2] == '/=':
var_global.change(p[1], var_global.show(p[1])/p[3])
else: errors.unknownSignal(t)
p[0]=var_global.show(p[1])
#########################################################################
#statements
def p_statement(t):
'''statement : if_statement
| while_statement
| for_statement
| break_statement
| return_statement
| assignment end
| subCall_statement end
| write_statement end
| read_statement end
'''
t[0]=t[1]
def p_list_statement(t):
'''list_statement : statement list_statement
| empty'''
def p_statement_if(t):
'''if_statement : IF LPAREN expression RPAREN LBRACE block RBRACE
| IF LPAREN expression RPAREN LBRACE block RBRACE ELSE LBRACE block RBRACE'''
if(t[3]):
t[0]=t[6]
else:
if(len(t)>10): #with else
t[0]=t[10]
def p_statement_while(t):
'while_statement : WHILE LPAREN expression RPAREN LBRACE block RBRACE'
def p_statement_for(t):
'for_statement : FOR LPAREN assignment SEMICOLON expression SEMICOLON assignment RPAREN LBRACE block RBRACE'
def p_statement_break(t):
'break_statement : BREAK end'
def p_statement_return(t):
'''return_statement : RETURN end
| RETURN expression end'''
def p_statement_subCall(t):
'''subCall_statement : NAME LPAREN list_expression RPAREN'''
################################################################################
#I/O
def p_statement_write(t):
'''write_statement : WRITE list_expression '''
tmp=""
for element in t[2]:
tmp+=str(element)+" "
print ("In Exectution: "+tmp)
def p_statement_read(t):
'''read_statement : READ variavel '''
t[3].value = raw_input();
##############################################################################
#block
def p_block(t):
'''block : list_var_Declaration list_statement'''
t[0]=[t[1],t[2]]
###############################################################
#errors
'''def p_var_declaration_error(p):
'var_Declaration : type error end'
errors.VarDecError(p)
def p_var_declaration_error2(p):
'var_Declaration : type var_Especification'
errors.NoSemicolonError(p)
def p_var_declaration_error3(p):
'var_Declaration : empty var_Especification end'
errors.NoTypeError(p)'''
def p_error(t):
parser.errok()
errors.unknownError(t)
parser=yacc.yacc(start='program') #build the parser