This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
289 lines (232 loc) · 8.44 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
# encoding=UTF-8
# Copyright © 2007-2012 Jakub Wilk <jwilk@jwilk.net>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the “Software”), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
'''Javalette parser.'''
import ply.yacc as yacc
import syntax
import type
import expression
__all__ = ['Parser']
class Parser(object):
def __init__(self, lexer):
self.lexer = lexer
self.tokens = lexer.tokens
self.precedence = [('right', 'ELSE')]
self.start = 'program'
self.yacc = yacc.yacc(module=self, debug=0)
def parse(self):
'''Build the syntax tree.'''
return self.yacc.parse(lexer=self.lexer)
def p_program(self, p):
'program : fun_def_list'
p[0] = syntax.program(p[1])
def p_fun_def_list_empty(self, p):
'fun_def_list :'
p[0] = []
def p_fun_def_list_nonempty(self, p):
'fun_def_list : fun_def_list fun_def'
p[0] = p[1]
p[0] += p[2],
def p_fun_def(self, p):
'fun_def : type IDENT LPAREN arg_list RPAREN complex_i'
p[0] = syntax.function(p[2], p[1], p[4], p[6], p.lexpos(2))
def p_arg_list(self, p):
'''
arg_list : arg_list_empty
| arg_list_nonempty
'''
p[0] = p[1]
def p_arg_list_empty(self, p):
'arg_list_empty : '
p[0] = []
def p_arg(self, p):
'arg : type IDENT'
p[0] = syntax.variable(p[1], p[2], None, p.lexpos(2))
def p_arg_list_single(self, p):
'arg_list_nonempty : arg'
p[0] = [p[1]]
def p_arg_list_nonempty(self, p):
'arg_list_nonempty : arg_list_nonempty COMMA arg'
p[0] = p[1]
p[0] += p[3],
def p_instr(self, p):
'''
instr : complex_i
| decl_i
| cond_i
| loop_i
| return_i
| expr_i
'''
p[0] = p[1]
def p_complex_i(self, p):
'complex_i : LBRACE i_list RBRACE'
p[0] = syntax.block_statement(p[2])
def p_i_list_empty(self, p):
'i_list :'
p[0] = []
def p_i_list_nonempty(self, p):
'i_list : i_list instr'
p[0] = p[1]
p[0] += p[2],
def p_decl_i(self, p):
'decl_i : type decl_list SEMICOLON'
for var in p[2]:
var.type = p[1]
p[0] = syntax.declaration(p[2], p[2][0].position)
def p_decl_list_single(self, p):
'decl_list : decl'
p[0] = [p[1]]
def p_decl_list_multiple(self, p):
'decl_list : decl_list COMMA decl'
p[0] = p[1]
p[0] += p[3],
def p_decl(self, p):
'decl : IDENT'
p[0] = syntax.variable(None, p[1], None, p.lexpos(1))
def p_decl_value(self, p):
'decl : IDENT ASSIGN expr'
p[0] = syntax.variable(None, p[1], p[3], p.lexpos(1))
def p_inc(self, p):
'assign_e : IDENT INC'
q = expression.reference(p[1], p.lexpos(1))
p[0] = expression.assignment(q, expression.binary_operator('+', q, expression.const(1, type.int_t, p.lexpos(2)), p.lexpos(2)), p.lexpos(2))
def p_dec(self, p):
'assign_e : IDENT DEC'
q = expression.reference(p[1], p.lexpos(1))
p[0] = expression.assignment(q, expression.binary_operator('-', q, expression.const(1, type.int_t, p.lexpos(2)), p.lexpos(2)), p.lexpos(2))
def p_if(self, p):
'cond_i : IF LPAREN expr RPAREN instr else'
p[0] = syntax.if_then_else(p[3], p[5], p[6], p.lexpos(1))
def p_else(self, p):
'else : ELSE instr'
p[0] = p[2]
def p_no_else(self, p):
'else :'
p[0] = []
def p_while(self, p):
'loop_i : WHILE LPAREN expr RPAREN instr'
p[0] = syntax.while_loop(p[3], [], p[5], p.lexpos(1))
def p_for(self, p):
'loop_i : FOR LPAREN assign_e SEMICOLON expr SEMICOLON assign_e RPAREN instr'
pre = syntax.evaluation(p[3])
mid = p[5]
post = syntax.evaluation(p[7])
body = p[9]
p[0] = syntax.block_statement([pre, syntax.while_loop(mid, post, body, p.lexpos(1))])
def p_return_void(self, p):
'return_i : RETURN SEMICOLON'
p[0] = syntax.return_statement(None, p.lexpos(1))
def p_return_value(self, p):
'return_i : RETURN expr SEMICOLON'
p[0] = syntax.return_statement(p[2], p.lexpos(1))
def p_expr_i(self, p):
'expr_i : expr SEMICOLON'
p[0] = syntax.evaluation(p[1])
def p_assign_e(self, p):
'assign_e : IDENT ASSIGN expr'
p[0] = expression.assignment(expression.reference(p[1], p.lexpos(1)), p[3], p.lexpos(2))
def p_e_pass(self, p):
'''
expr : assign_e
expr : or_e
or_e : and_e
and_e : compare_e
compare_e : rel_e
rel_e : add_e
add_e : mul_e
mul_e : sa_e
sa_e : prefix_e
prefix_e : simple_e
'''
p[0] = p[1]
def p_binary_e(self, p):
'''
or_e : or_e LOR and_e
and_e : and_e LAND compare_e
compare_e : compare_e EQUAL rel_e
| compare_e NOTEQ rel_e
rel_e : rel_e LT add_e
| rel_e GT add_e
| rel_e LE add_e
| rel_e GE add_e
add_e : add_e PLUS mul_e
| add_e MINUS mul_e
mul_e : mul_e TIMES sa_e
| mul_e DIV sa_e
| mul_e MOD sa_e
'''
p[0] = expression.binary_operator(p[2], p[1], p[3], p.lexpos(2))
def p_sa_e(self, p):
'''
sa_e : NOT sa_e
| PLUS sa_e
| MINUS sa_e
'''
p[0] = expression.unary_operator(p[1], p[2], p.lexpos(1))
def p_cast_e(self, p):
'sa_e : LPAREN type RPAREN sa_e'
p[0] = expression.cast(operand=p[4], type=p[2], position=p.lexpos(1))
def p_call_e(self, p):
'prefix_e : IDENT LPAREN expr_list RPAREN'
p[0] = expression.call(expression.reference(p[1], p.lexpos(1)), p[3], p.lexpos(1))
def p_call_e_noargs(self, p):
'prefix_e : IDENT LPAREN RPAREN'
p[0] = expression.call(expression.reference(p[1], p.lexpos(1)), [], p.lexpos(1))
def p_expr_list_single(self, p):
'expr_list : expr'
p[0] = [p[1]]
def p_expr_list_multiple(self, p):
'expr_list : expr_list COMMA expr'
p[0] = p[1]
p[0] += p[3],
def p_ident_e(self, p):
'simple_e : IDENT'
p[0] = expression.reference(p[1], p.lexpos(1))
def p_const_e(self, p):
'simple_e : const'
p[0] = p[1]
def p_paren_e(self, p):
'simple_e : LPAREN expr RPAREN'
p[0] = p[2]
def p_const_int(self, p):
'const : INT'
p[0] = expression.const(p[1], type.int_t, p.lexpos(1))
def p_const_double(self, p):
'const : DOUBLE'
p[0] = expression.const(p[1], type.double_t, p.lexpos(1))
def p_const_boolean(self, p):
'const : BOOLEAN'
p[0] = expression.const(p[1], type.boolean_t, p.lexpos(1))
def p_const_string(self, p):
'const : STRING'
p[0] = expression.const(p[1], type.string_t, p.lexpos(1))
def p_type(self, p):
'type : TYPE'
p[0] = type.simple_type(p[1])
def p_error(self, token):
if token:
raise ParseError(token.lexpos, 'Syntax error near ' + repr(token.value))
else:
raise ParseError(None, 'Syntax error at the end of file')
from error import JtError
class ParseError(JtError):
pass
# vim:ts=4 sts=4 sw=4 et