-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuser_level_parser.py
215 lines (159 loc) · 6.26 KB
/
user_level_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
from MicroCompiler.SkeletonParser import Epsilon
from MicroCompiler.parser_builder import ParserBuilder
from MicroCompiler.postfix_expression.operator import PythonBuiltinOperator
pb = ParserBuilder()
pb.add_generator("get_fallback_method", "fallback")
clazz = pb.generate()
class Parser(clazz):
def __init__(self):
self.post_expr = []
self.method_name_mapping = {
"(": "open_parenthesis",
")": "close_parenthesis",
"/": "Division",
"*": "Mul",
"+": "Add",
"-": "Sub",
"<START>": "Start",
}
def fallback(self, input_):
return input_
def flat_list(self, nested_list):
result = []
for item in nested_list:
if isinstance(item, list):
flat_item = self.flat_list(item)
result.extend(flat_item)
else:
result.append(item)
return result
def flat_nested_postfix_list(self, nested_postfix_list):
return self.flat_list(nested_postfix_list)
def get_legal_method_name(self, method_name):
return (
self.method_name_mapping[method_name]
if method_name in self.method_name_mapping
else method_name
)
def Division(self, input_):
return PythonBuiltinOperator(input_[0], 2)
def Mul(self, input_):
return PythonBuiltinOperator(input_[0], 2)
def Add(self, input_):
return PythonBuiltinOperator(input_[0], 2)
def Sub(self, input_):
return PythonBuiltinOperator(input_[0], 2)
def num(self, input_):
return input_[0]
def ExprTwo(self, input_):
# ExprTwo -> '+' Term ExprTwo
# | '-' Term ExprTwo
# | ϵ ;
if len(input_) == 1:
# ExprTwo -> ϵ
return Epsilon()
if isinstance(input_[2], Epsilon):
# ExprTwo -> '+' Term ExprTwo | '-' Term ExprTwo
# | |
# -> ϵ -> ϵ
return [input_[0], [input_[1]]]
if isinstance(input_[2], list):
# ExprTwo -> '+' Term ExprTwo | '-' Term ExprTwo
# | |
# -> ['+' num] -> ['-' num]
postfix_expr = input_[2][1]
postfix_expr = postfix_expr[:] # shallow copy
head = postfix_expr.pop(0)
operator = input_[2][0]
postfix_expr.insert(0, operator)
postfix_expr.insert(0, head)
postfix_expr.insert(0, input_[1])
return [input_[0], postfix_expr]
def TermTwo(self, input_):
# TermTwo -> '*' Factor TermTwo
# | '/' Factor TermTwo
# | ϵ ;
if len(input_) == 1:
# ExprTwo -> ϵ
return Epsilon()
if isinstance(input_[2], Epsilon):
# ExprTwo -> '+' Factor TermTwo | '-' Factor TermTwo
# | |
# -> ϵ -> ϵ
#
# output: ['*', postfix_expr]
# |----------|
# type: list
return [input_[0], [input_[1]]]
if isinstance(input_[2], list):
# TermTwo -> '*' Factor TermTwo | '/' Factor TermTwo
# | |
# -> ['/' num] -> ['*' num]
# input_: ['*', Factor, ['/', [head, rest_of_postfix_expr]]]
# |---------------------------|
# post_expr (type: list)
#
# output: ['*', [Factor, head, '/', rest_of_postfix_expr]]
postfix_expr = input_[2][1]
postfix_expr = postfix_expr[:] # shallow copy
head = postfix_expr.pop(0)
operator = input_[2][0]
postfix_expr.insert(0, operator)
postfix_expr.insert(0, head)
postfix_expr.insert(0, input_[1])
return [input_[0], postfix_expr]
def Factor(self, input_):
if len(input_) == 1:
return input_[0]
if len(input_) == 3:
return input_[1]
def Term(self, input_):
if isinstance(input_[1], Epsilon):
# Term -> Factor TermTwo ;
# |
# -> ϵ
return input_[0]
# Term -> Factor TermTwo ;
# |
# -> ['*', postfix_expr]
# input_: [Factor, ['/', [head, rest_of_postfix_expr]]]
# |---------------------------|
# post_expr (type: list)
#
# output: [Factor, head, '/', rest_of_postfix_expr]
postfix_expr = input_[1][1]
postfix_expr = postfix_expr[:] # shallow copy
head = postfix_expr.pop(0)
operator = input_[1][0]
postfix_expr.insert(0, operator)
postfix_expr.insert(0, head)
postfix_expr.insert(0, input_[0])
return postfix_expr
def Expr(self, input_):
if isinstance(input_[1], Epsilon):
# Expr -> Term ExprTwo ;
# |
# -> ϵ
return input_[0]
# Expr -> Term ExprTwo ;
# |
# -> [operator, postfix_expr]
# input_: [Factor, ['/', [head, rest_of_postfix_expr]]]
# |---------------------------|
# post_expr (type: list)
#
# output: [Factor, head, '/', rest_of_postfix_expr]
postfix_expr = input_[1][1]
postfix_expr = postfix_expr[:] # shallow copy
head = postfix_expr.pop(0)
operator = input_[1][0]
postfix_expr.insert(0, operator)
postfix_expr.insert(0, head)
postfix_expr.insert(0, input_[0])
return postfix_expr
def Goal(self, input_):
expr = input_[0]
flat_postfix_expr = self.flat_nested_postfix_list(expr)
return flat_postfix_expr
def Start(self, input_):
return input_[0]