-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.py
408 lines (366 loc) · 6.39 KB
/
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
import sys
from ply import yacc
from lexer import lex, tokens
precedence = (
('nonassoc', 'EQUALS', 'DIFFERENT'),
('left', 'COMMA'),
('left', 'CONCAT'),
('left', 'OR', 'XOR'),
('left', 'AND'),
('right', 'NOT'),
('nonassoc', 'LT', 'GT', 'LE', 'GE'),
('left', 'PLUS', 'MINUS'),
('left', 'TIMES', 'DIV'),
('right', 'UNEG'),
('nonassoc', 'LENGTHOF'),
)
class Node(object):
"Linked list class for string lists"
def __init__(self, data, next):
self.data = data
self.next = next
class Context(object):
def __init__(self, output=sys.stdout):
self.vars = {}
self.out = output
class FakeOutput(object):
def write(self, _):
pass
def p_programT(p):
'''
program : TEXT
'''
text = p[1]
def f(context):
context.out.write(text)
p[0] = f
def p_programB(p):
'''
program : block
'''
p[0] = p[1]
def p_programTI(p):
'''
program : TEXT program
'''
p_programT(p)
first = p[0]
rest = p[2]
def f(context):
first(context)
rest(context)
p[0] = f
def p_programBI(p):
'''
program : block program
'''
p_programB(p)
first = p[0]
rest = p[2]
def f(context):
first(context)
rest(context)
p[0] = f
def p_block(p):
'''
block : BEGIN stmt_list END
'''
p[0] = p[2]
def p_stmtB(p):
'''
stmt_list : stmt SEMICOLON
'''
p[0] = p[1]
def p_stmtI(p):
'''
stmt_list : stmt SEMICOLON stmt_list
'''
p_stmtB(p)
first = p[0]
rest = p[3]
def f(context):
first(context)
rest(context)
p[0] = f
def print_list(context, a):
while a:
context.out.write(a.data)
a = a.next
def p_printE(p):
'''
stmt : PRINT expr
'''
arg = p[2]
def f(context):
a = arg(context)
if a==None or isinstance(a, Node):
print_list(context, a)
else:
context.out.write(str(a))
p[0] = f
def p_printL(p):
'''
stmt : PRINT str_list
'''
arg = p[2]
p[0] = lambda context: print_list(context, arg)
def p_exprPar(p):
'''
expr : LPAREN expr RPAREN
'''
p[0] = p[2]
def p_exprN(p):
'''
expr : MINUS expr %prec UNEG
'''
value = p[2]
p[0] = lambda _: -value(context)
def p_exprL(p):
'''
expr : INT
| STRING
'''
value = p[1]
p[0] = lambda _: value
def p_exprB(p):
'''
expr : TRUE
| FALSE
'''
value = p[1] == "TRUE"
p[0] = lambda _: value
def p_exprLN(p):
'''
expr : NOT expr
'''
value = p[2]
def f(context):
v = value(context)
if type(v) == bool:
return not v
else:
raise TypeError
p[0] = f
def p_exprV(p):
'''
expr : IDENTIFIER
'''
variable = p[1]
p[0] = lambda context: context.vars[variable]
def p_exprOps(p):
'''
expr : expr PLUS expr
| expr MINUS expr
| expr TIMES expr
| expr DIV expr
| expr CONCAT expr
'''
left = p[1]
op = {
'+': {(int, int): lambda x,y: x+y},
'-': {(int, int): lambda x,y: x-y},
'*': {(int, int): lambda x,y: x*y},
'/': {(int, int): lambda x,y: x/y},
'.': {(str, str): lambda x,y: x+y},
}[p[2]]
right = p[3]
def f(context):
l = left(context)
r = right(context)
k = (type(l), type(r))
if k in op:
return op[k](l, r)
else:
raise TypeError
p[0] = f
def p_exprComp(p):
'''
expr : expr LT expr
| expr LE expr
| expr GT expr
| expr GE expr
| expr EQUALS expr
| expr DIFFERENT expr
'''
left = p[1]
op = {
'<': lambda x,y: x<y,
'>': lambda x,y: x>y,
'>=': lambda x,y: x>=y,
'<=': lambda x,y: x<=y,
'=': lambda x,y: x==y,
'!=': lambda x,y: x!=y,
}[p[2]]
right = p[3]
def f(context):
l = left(context)
r = right(context)
if type(l) == type(r):
return op(l, r)
else:
raise TypeError
p[0] = f
def p_exprBinLog(p):
'''
expr : expr AND expr
| expr OR expr
| expr XOR expr
'''
left = p[1]
op = {
'AND': lambda x,y: x and y,
'OR': lambda x,y: x or y,
'XOR': lambda x,y: x ^ y,
}[p[2]]
right = p[3]
def f(context):
l = left(context)
r = right(context)
if type(l)==bool and type(r)==bool:
return op(left(context), right(context))
else:
raise TypeError
p[0] = f
def for_body(var, slist, instr, ctxt):
if isinstance(slist, Node):
while slist != None:
ctxt.vars[var] = slist.data
slist = slist.next
instr(ctxt)
else :
raise TypeError
def p_forL(p):
'''
stmt : FOR IDENTIFIER IN str_list DO stmt_list ENDFOR
'''
identif = p[2]
slist = p[4]
instr = p[6]
def f(context):
old_id = context.vars.get(identif)
for_body(identif, slist, instr, context)
if old_id != None :
context.vars[identif] = old_id
else :
del context.vars[identif]
p[0] = f
def p_forV(p):
'''
stmt : FOR IDENTIFIER IN IDENTIFIER DO stmt_list ENDFOR
'''
id1 = p[2]
id2 = p[4]
instr = p[6]
def f(context):
old_id = context.vars.get(id1)
s = context.vars.get(id2)
for_body(id1, s, instr, context)
if old_id != None :
context.vars[id1] = old_id
else :
del context.vars[id1]
p[0] = f
def p_lengthof(p):
'''
expr : LENGTHOF expr
| LENGTHOF str_list
'''
slist = p[2]
def f(context):
size = 0
if hasattr(slist, '__call__'):
s = slist(context)
else:
s = slist
if s==None or isinstance(s, Node):
while s != None:
s = s.next
size += 1
return size
elif isinstance(s, str):
return len(s)
else:
raise TypeError
p[0] = f
def p_it(p):
'''
stmt : IF expr DO stmt_list ENDIF
'''
cond = p[2]
body = p[4]
def f(context):
c = cond(context)
if type(c) == bool:
if c:
body(context)
else:
raise TypeError
p[0] = f
def p_ite(p):
'''
stmt : IF expr DO stmt_list ELSE stmt_list ENDIF
'''
cond = p[2]
posbody = p[4]
negbody = p[6]
def f(context):
c = cond(context)
if type(c) == bool:
if c:
posbody(context)
else:
negbody(context)
else:
raise TypeError
p[0] = f
def p_assignE(p):
'''
stmt : IDENTIFIER ASSIGN expr
'''
lvalue = p[1]
rvalue = p[3]
def f(context):
context.vars[lvalue] = rvalue(context)
p[0] = f
def p_assignL(p):
'''
stmt : IDENTIFIER ASSIGN str_list
'''
lvalue = p[1]
rvalue = p[3]
def f(context):
context.vars[lvalue] = rvalue
p[0] = f
def p_stringListE(p):
'''
str_list : LPAREN RPAREN
'''
p[0] = None
def p_stringList1(p):
'''
str_list : LPAREN STRING COMMA RPAREN
'''
p[0] = Node(p[2], None)
def p_stringListM(p):
'''
str_list : LPAREN STRING COMMA strs RPAREN
'''
p[0] = Node(p[2], p[4])
def p_strsB(p):
'''
strs : STRING
'''
p[0] = Node(p[1], None)
def p_strsI(p):
'''
strs : STRING COMMA strs
'''
p[0] = Node(p[1], p[3])
def p_error(p):
print "Syntax error near line", str(p.lineno)
yacc = yacc.yacc()
if __name__=="__main__":
import sys
inp = sys.stdin.read()
context = Context(sys.stdout)
yacc.parse(inp, debug=("-d" in sys.argv))(context)