-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser_ruby.py
More file actions
326 lines (278 loc) · 9.59 KB
/
parser_ruby.py
File metadata and controls
326 lines (278 loc) · 9.59 KB
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
from lexer import *
class AST(object):
pass
class BinOp(AST):
def __init__(self, left, op, right):
self.left = left
self.token = self.op = op
self.right = right
class Num(AST):
def __init__(self, token):
self.token = token
self.value = token.value
class UnaryOp(AST):
def __init__(self, op, expr):
self.token = self.op = op
self.expr = expr
class Compound(AST):
def __init__(self):
self.children = []
class Assign(AST):
def __init__(self, left, op, right):
self.left = left
self.token = self.op = op
self.right = right
class Var(AST):
"""Var node is constructed from ID token"""
def __init__(self,token):
self.token=token
self.value=token.value
class If(AST):
def __init__(self, condition, body, rest):
self.condition = condition
self.body = body
self.rest = rest
class Else(AST):
def __init__(self, body):
self.body=body
class While(AST):
def __init__(self, condition, body):
self.condition = condition
self.body = body
class NoOp(AST):
pass
class Parser(object):
def __init__(self,lexer):
self.lexer=lexer
#set current token to the first token taken from the input
self.current_token=self.lexer.get_next_token()
def error(self):
raise Exception('invalid syntax')
def eat(self,token_type):
"""Compares the current token type with the passed token type"""
#If these two match then eat the current token and set it to the next token
#Else raise an exception
if self.current_token.type==token_type:
self.current_token=self.lexer.get_next_token()
else:
self.error()
def program(self):
"""program : compound_statement"""
node=self.compound_statement()
return node
def compound_statement(self):
"""compound_statement : statement_list"""
nodes = self.statement_list()
root=Compound()
for node in nodes:
root.children.append(node)
return root
def statement_list(self):
"""statement_list : statement | statement SEMI statement_list"""
node = self.statement()
results = [node]
while self.current_token.type != EOF:
results.append(self.statement())
return results
def statement(self):
"""
statement : compound_statement | assignment_statement | if_statement | elsif_statement| else statement | while statement| empty
"""
if self.current_token.type==ID:
node=self.assignment_statement()
elif self.current_token.type==IF:
node=self.if_statement()
elif self.current_token.type==ELSIF:
node=self.elsif_statement()
elif self.current_token.type==ELSE:
node=self.else_statement()
elif self.current_token.type==WHILE:
node=self.while_statement()
else:
node=self.conditional_statement()
return node
def assignment_statement(self):
"""assignment_statement : variable ASSIGN expr"""
left = self.variable()
token = self.current_token
self.eat(ASSIGN)
right = self.expr()
node = Assign(left, token, right)
return node
def if_statement(self):
"""
if_statement : IF conditional_statement statement_list (elsif_statement | else_statement | empty) END
"""
self.eat(IF)
condition = self.conditional_statement()
body=[]
rest=[]
while self.current_token.type!=ELSIF and self.current_token.type!=ELSE and self.current_token.type!=END:
body.append(self.statement())
if self.current_token.type==ELSIF:
rest = self.elsif_statement()
if self.current_token.type==ELSE:
rest = self.else_statement()
node = If(condition, body, rest)
return node
def while_statement(self):
"""
while_statement : WHILE conditional_statement statement_list END
"""
self.eat(WHILE)
condition=self.conditional_statement()
body=[]
while self.current_token.type!=END:
body.append(self.statement())
self.eat(END)
node=While(condition,body)
return node
def variable(self):
"""
variable : ID
"""
node = Var(self.current_token)
self.eat(ID)
return node
def empty(self):
"""An empty production"""
return NoOp()
def elsif_statement(self):
"""
elsif_statement : ELSIF conditional_statement statement_list (else | empty)
"""
self.eat(ELSIF)
elsif_condition=self.conditional_statement()
elseif_body=[]
rest=[]
while self.current_token.type!=ELSE:
elseif_body.append(self.statement())
if self.current_token.type==ELSE:
rest=self.else_statement()
node=If(elsif_condition, elseif_body,rest)
return node
def else_statement(self):
"""else_statement : ELSE statement_list """
self.eat(ELSE)
else_body=[]
while self.current_token.type!=END:
else_body.append(self.statement())
self.eat(END)
node = Else(else_body)
return node
def conditional_statement(self):
"""conditional_statement : expr (EQUAL|GRE|NOT|GRET|LESE|LEST) expr"""
node=self.expr()
while self.current_token.type in (EQUAL,GRE,NOT,GRET,LESE,LEST):
token=self.current_token()
self.eat(token.type)
node=BinOp(node,token,self.expr())
return node
def expr(self):
"""
expr : term ((PLUS | MINUS) term)*
"""
node = self.term()
while self.current_token.type in (PLUS, MINUS):
token = self.current_token
if token.type == PLUS:
self.eat(PLUS)
elif token.type == MINUS:
self.eat(MINUS)
node = BinOp(node,token,self.term())
return node
def term(self):
"""term : factor ((MUL | DIV | MOD | EQUAL | NOT | LEST | GRET | LESE | GRE) factor)*"""
node = self.factor()
while self.current_token.type in (MUL,DIV, MOD,EQUAL,NOT,LEST,GRET,LESE,GRE):
token = self.current_token
if token.type == MUL:
self.eat(MUL)
elif token.type == DIV:
self.eat(DIV)
elif token.type == MOD:
self.eat(MOD)
elif token.type == EQUAL:
self.eat(EQUAL)
elif token.type == NOT:
self.eat(NOT)
elif token.type == LEST:
self.eat(LEST)
elif token.type == GRET:
self.eat(GRET)
elif token.type == LESE:
self.eat(LESE)
elif token.type == GRE:
self.eat(GRE)
node = BinOp(node,token,self.factor())
return node
def factor(self):
"""factor : PLUS factor
| MINUS factor
| INTEGER
| REAL
| LPAREN expr RPAREN
| variable
"""
token = self.current_token
if token.type == INTEGER:
self.eat(INTEGER)
return Num(token)
elif token.type == REAL:
self.eat(REAL)
return Num(token)
elif token.type == PLUS:
self.eat(PLUS)
node = UnaryOp(token, self.factor())
return node
elif token.type == MINUS:
self.eat(MINUS)
node = UnaryOp(token, self.factor())
return node
elif token.type == LPAREN:
self.eat(LPAREN)
node = self.expr()
self.eat(RPAREN)
return node
elif token.type == ID:
self.eat(ID)
return Var(token)
elif token.type == STR:
self.eat(STR)
return Num(token)
else:
node = self.variable()
return node
def parse(self):
"""
program : compound_statement
compound_statement : statement_list
statement_list : statement | statement SEMI statement_list
statement : compound_statement | assignment_statement | if_statement | elsif_statement| else statement | while statement| empty
assignment_statement : variable ASSIGN expr
if_statement : IF conditional_statement statement_list (elsif_statement | else_statement | empty) END
elsif_statement : ELSIF conditional_statement statement_list (else | empty)
else_statement : ELSE statement_list
while_statement : WHILE conditional_statement statement_list END
conditional_statement : expr (EQUAL|GRE|NOT|GRET|LESE|LEST) expr
expr : term ((PLUS | MINUS) term)*
term : factor ((MUL | DIV | MOD | EQUAL | NOT | LEST | GRET | LESE | GRE) factor)*
factor : PLUS factor
| MINUS factor
| INTEGER
| REAL
| LPAREN expr RPAREN
| variable
variable : ID
"""
node = self.program()
while self.current_token.type != EOF:
self.error()
return node
class NodeVisitor(object):
def visit(self, node):
method_name = 'visit_' + type(node).__name__
visitor = getattr(self, method_name, self.generic_visit)
return visitor(node)
def generic_visit(self, node):
raise Exception('No visit_{} method'.format(type(node).__name__))