-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpreterext.py
executable file
·393 lines (302 loc) · 8.34 KB
/
interpreterext.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
#!/usr/bin/python
#
# interpreterext.py -
# A python implementation of the mini language, with user-defined
# functions
#
# Kurt Schmidt
# 7/07
#
# EDITOR: cols=80, tabstop=2
#
# NOTES:
# the display() method everybody has is just to graphically spit the
# actual parse tree to the screen
#
# The grammar can be found in programext.py (probably should be here)
#
#
# Paul DeMicco/Joseph Heenan/Joshua Datko/Jon Boone
# 2013.04.22
#
# ADDED the List Grammar from Lecture 1.
# <list> -> [<sequence>]|[]
# <sequence> -> <listelement>,<sequence>|<listelement>
# <listelement> -> <list>|NUMBER
import sys
from programext import *
# Debug Flag
DEBUG = None
###### LEXER ###############################
# Note: This is precisely the same lexer that exp1 uses. Could've pulled
# it out to a different file.
from ply import lex
tokens = (
'PLUS',
'MINUS',
'TIMES',
'LPAREN',
'RPAREN',
'SEMICOLON',
'COMMA',
'NUMBER',
'ASSIGNOP',
'WHILE',
'DO',
'OD',
'IF',
'THEN',
'ELSE',
'FI',
'DEFINE',
'PROC',
'END',
'IDENT',
'LBRACKET',
'RBRACKET',
'LISTCONCATENATOR'
)
# These are all caught in the IDENT rule, typed there.
reserved = {
'while' : 'WHILE',
'do' : 'DO',
'od' : 'OD',
'if' : 'IF',
'then' : 'THEN',
'else' : 'ELSE',
'fi' : 'FI',
'define' : 'DEFINE',
'proc' : 'PROC',
'end' : 'END'
}
# Now, this section. We have a mapping, REs to token types (please note
# the t_ prefix). They simply return the type.
# t_ignore is special, and does just what it says. Spaces and tabs
t_ignore = ' \t'
# These are the simple maps
t_PLUS = r'\+'
t_MINUS = r'-'
t_TIMES = r'\*'
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_ASSIGNOP = r':='
t_SEMICOLON = r';'
t_COMMA = r','
t_LBRACKET = r'\['
t_RBRACKET = r'\]'
t_LISTCONCATENATOR = r'\|\|'
def t_IDENT( t ):
#r'[a-zA-Z_][a-zA-Z_0-9]*'
r'[a-zA-Z]+'
t.type = reserved.get( t.value, 'IDENT' ) # Check for reserved words
return t
def t_NUMBER( t ) :
r'[0-9]+'
# t.value holds the string that matched. Dynamic typing - no unions
t.value = int( t.value )
return t
# These are standard little ditties:
def t_newline( t ):
r'\n+'
t.lexer.lineno += len( t.value )
# Error handling rule
def t_error( t ):
print "Illegal character '%s' on line %d" % ( t.value[0], t.lexer.lineno )
return t
#t.lexer.skip( 1 )
lex.lex()
#----- LEXER (end) -------------------------------
###### YACC #####################################
import ply.yacc as yacc
# create a function for each production (note the prefix)
# The rule is given in the doc string
def p_program( p ) :
'program : stmt_list'
P = Program( p[1] )
P.display()
print 'Running Program'
P.eval()
P.dump()
def p_stmt_list( p ) :
'''stmt_list : stmt SEMICOLON stmt_list
| stmt'''
if len( p ) == 2 : # single stmt => new list
p[0] = StmtList()
p[0].insert( p[1] )
else : # we have a stmtList, keep adding to front
p[3].insert( p[1] )
p[0] = p[3]
def p_stmt( p ) :
'''stmt : assign_stmt
| while_stmt
| if_stmt
| define_stmt'''
p[0] = p[1]
def p_add( p ) :
'expr : expr PLUS term'
_debugMessage("p_add")
p[0] = Plus( p[1], p[3] )
def p_sub( p ) :
'expr : expr MINUS term'
_debugMessage("p_sub")
p[0] = Minus( p[1], p[3] )
def p_expr_list( p ) :
'''expr_list : element COMMA expr_list
| element'''
_debugMessage("p_expr_list")
if len( p ) == 2 : # single expr => new list
p[0] = [ p[1] ]
else : # we have a expr_list, keep adding to front
p[3].insert( 0, p[1] )
p[0] = p[3]
def p_expr_term( p ) :
'expr : term'
_debugMessage("p_expr_term")
p[0] = p[1]
def p_mult( p ) :
'term : term TIMES fact'
_debugMessage("p_mult")
p[0] = Times( p[1], p[3] )
def p_term_fact( p ) :
'term : fact'
_debugMessage("p_term_fact")
p[0] = p[1]
def p_fact_expr( p ) :
'fact : LPAREN expr RPAREN'
_debugMessage("p_fact_expr")
p[0] = p[2]
def p_fact_NUM( p ) :
'fact : NUMBER'
_debugMessage("p_fact_NUMBER")
p[0] = Number( p[1] )
def p_fact_IDENT( p ) :
'fact : IDENT'
_debugMessage("p_fact_IDENT")
p[0] = Ident( p[1] )
def p_fact_funcall( p ) :
'fact : func_call'
_debugMessage("p_fact_funcall")
p[0] = p[1]
def p_assn( p ) :
'assign_stmt : IDENT ASSIGNOP element'
_debugMessage("p_assn")
p[0] = AssignStmt( p[1], p[3] )
def p_while( p ) :
'while_stmt : WHILE expr DO stmt_list OD'
_debugMessage("p_while")
p[0] = WhileStmt( p[2], p[4] )
def p_if( p ) :
'if_stmt : IF expr THEN stmt_list ELSE stmt_list FI'
_debugMessage("p_if")
p[0] = IfStmt( p[2], p[4], p[6] )
def p_def( p ) :
'define_stmt : DEFINE IDENT PROC LPAREN param_list RPAREN stmt_list END'
_debugMessage("p_define_stmt")
p[0] = DefineStmt( p[2], Proc( p[5], p[7] ))
def p_param_list( p ) :
'''param_list : IDENT COMMA param_list
| IDENT'''
_debugMessage("p_param_list")
if len( p ) == 2 : # single param => new list
p[0] = [ p[1] ]
else : # we have a param_list, keep adding to front
p[3].insert( 0, p[1] )
p[0] = p[3]
def p_func_call( p ) :
'func_call : IDENT LPAREN expr_list RPAREN'
_debugMessage("p_func_call")
p[0] = FunCall( p[1], p[3] )
# List parsing rules #
def p_list_lbracket_sequence_rbracket(p):
'list : LBRACKET sequence RBRACKET'
# print("p_list_lbracket_sequence_rbracket")
p[0] = List(p[2])
def p_list_leftparen_rightparen(p):
'list : LBRACKET RBRACKET'
# print("p_list_leftparen_rightparen")
p[0] = List()
def p_sequence_element_comma_sequence(p):
'sequence : element COMMA sequence'
# print("p_sequence_element_comma_sequence")
p[0] = Sequence(p[1],p[3])
def p_sequence_element(p):
'sequence : element'
# print("p_sequence_element")
p[0] = Sequence(p[1])
def p_element_list(p):
'element : list'
# print("p_element_list")
p[0] = p[1]
def p_element_expr(p):
'element : expr'
# print("p_element_expr")
p[0] = p[1]
def p_element_element_LISTCONCATENATOR_list(p):
'element : element LISTCONCATENATOR list'
# print("p_list_element_LISTCONCATENATOR element")
p[0] = Concat(p[1],p[3])
def p_element_element_LISTCONCATENATOR_fact(p):
'element : element LISTCONCATENATOR fact'
p[0] = Concat(p[1], p[3])
# Error rule for syntax errors
def p_error( p ):
print "Syntax error in input!", str( p )
sys.exit( 2 )
# now, build the parser
yacc.yacc()
###### MAIN #################################
def _debugMessage(message):
"""Will write a debug message to the screen.
Args:
message: Data to be printed out for debug.
Returns:
NONE
"""
if DEBUG:
print message
def test_scanner(data) :
""" Test the lexer to make sure we
don't have any invalid tokens.
:param data: string data from either
a file or text input.
"""
lex.input(data)
# attempt to get that first token
tok = lex.token()
while tok:
tok = lex.token()
def test_parser(data) :
""" Test method for the parser.
:param data: string data from either
a file or text input.
"""
yacc.parse(data)
def main() :
""" Main method.
Will process file input or text input
and will execute the scanner and the parser.
"""
data = []
nArgs = len(sys.argv) - 1
if nArgs == 1:
# One argument, hopefully it a filename.
# If not, it will error out when attempting
# to open.
try:
fSpec = sys.argv[1]
_debugMessage("Reading %s" % fSpec)
data = open(fSpec, 'r').read()
except Exception as e:
print "({0}): {1}".format(type(e), e.message)
elif nArgs < 1:
# No Arguments, just enter manual mode.
data=sys.stdin.read()
_debugMessage("Input program is: ")
_debugMessage(data)
_debugMessage("End input program")
_debugMessage("Call lexer")
test_scanner(data)
_debugMessage("Call parser")
test_parser(data)
if __name__ == '__main__':
main()