-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocessor.py
285 lines (227 loc) · 5.64 KB
/
preprocessor.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
import ply.lex as lex
import ply.yacc as yacc
import re
err=''
syn_error=False
class Preprocessor:
class __Lexer:
tokens =['id', ]
literals = ['=', '+', '-', '/', '*', '%', '(', ')', '^']
t_id=r'[a-zA-Z_][a-zA-Z0-9_]*'
t_ignore=' \t'
def t_newline(self, t):
r'\n+'
t.lexer.lineno += t.value.count("\n")
def t_error(self, t):
global err,syn_error
syn_error=True
err=''
err=err+"Warning: ignored faulty token at line {}".format(t.lineno)
print("Warning: ignored faulty token at line {}".format(t.lineno))
t.lexer.skip(1)
def __init__(self, **kwargs):
self.lexer=lex.lex(module=self, **kwargs)
def test(self, string):
self.lexer.input(string)
for i in lexer: print(i.type, i.value)
precedence = ( ('left', '+', '-'), ('left', '/', '*', '%'), ('right', '^'), ('right', 'umin'))
ins=[]
ctr=1
def p_assignment(self, p):
'''statement : id '=' expression'''
Preprocessor.ins.append(p[1]+" = "+p[3])
def p_expression(self, p):
'''expression : expression '+' term
| expression '-' term
| term '''
if len(p)>2:
temp="_temp"+str(Preprocessor.ctr)
Preprocessor.ins.append(temp+" = 0")
Preprocessor.ins.append(temp+" = "+p[1]+" "+p[2]+" "+p[3])
p[0]=temp
Preprocessor.ctr+=1
else: p[0]=p[1]
def p_term(self, p):
'''term : term '*' exponentiation
| term '/' exponentiation
| term '%' exponentiation
| exponentiation '''
if len(p)>2:
temp="_temp"+str(Preprocessor.ctr)
Preprocessor.ins.append(temp+" = 0")
Preprocessor.ins.append(temp+" = "+p[1]+" "+p[2]+" "+p[3])
p[0]=temp
Preprocessor.ctr+=1
else: p[0]=p[1]
def p_exponentiation(self, p):
''' exponentiation : factor '^' exponentiation
| factor '''
if len(p)>2:
temp="_temp"+str(Preprocessor.ctr)
Preprocessor.ins.append(temp+" = 0")
Preprocessor.ins.append(temp+" = "+p[1]+" "+p[2]+" "+p[3])
p[0]=temp
Preprocessor.ctr+=1
else: p[0] = p[1]
def p_factor(self, p):
''' factor : '(' expression ')'
| '-' expression %prec umin
| id '''
if p[1]=='(':
p[0]=p[2]
elif p[1]=='-':
temp="_temp"+str(Preprocessor.ctr)
Preprocessor.ins.append(temp+" = 0")
Preprocessor.ins.append("zero = 0")
Preprocessor.ins.append(temp+" = zero "+p[1]+" "+p[2])
p[0]=temp
Preprocessor.ctr+=1
else: p[0]=p[1]
def p_error(self, p):
global err,syn_error
syn_error=True
err=''
try:
err=err+"Error at line no" +str(p.lineno)
print("Error at line no", p.lineno)
except:
err=err+"Error during preprocessing."
print("Error during preprocessing.")
def __init__(self):
self.tokens=Preprocessor.__Lexer.tokens
self.parser=yacc.yacc(module=self)
def parse(self, string):
lexer=Preprocessor.__Lexer()
self.parser.parse(string, lexer=lexer.lexer)
self.parser.restart()
temp=Preprocessor.ins
Preprocessor.ins=[]
return temp
def preprocessor(string):
global err,syn_error
mult_macro_code="""one = 1
zero = 0
_t_scl = 0
{0} = zero
for ( _t_scl = {2} ; _t_scl >= one ; _t_scl = _t_scl - one )
[
{0} = {0} + {1}
]
"""
div_macro_code="""one = 1
zero = 0
_t_dvsr = 0
_t_qnt = 0
_t_dvnd = 0
_t_dvsr = {2}
_t_qnt = zero
if ( _t_dvsr != zero )
[
for ( _t_dvnd = {1} ; _t_dvnd >= _t_dvsr ; _t_dvnd = _t_dvnd - _t_dvsr)
[
_t_qnt = _t_qnt + one
]
{0} = _t_qnt
]
else
[
end
]"""
mod_macro_code="""one = 1
zero = 0
_t_dvsr = 0
_t_qnt = 0
_t_dvnd = 0
_t_dvsr = {2}
_t_qnt = zero
for ( _t_dvnd = {1} ; _t_dvnd >= _t_dvsr ; _t_dvnd = _t_dvnd - _t_dvsr)
[
_t_qnt = _t_qnt + one
]
{0} = _t_dvnd"""
# r = x ^ y
exp_macro_code="""one = 1
zero = 0
_t_scl = 0
_t_sbscl = 0
_dst = 0
_t_dst = 0
_dst = one
if( {1} != zero )
[
for ( _t_scl = {2} ; _t_scl >= one ; _t_scl = _t_scl - one )
[
_t_dst = _dst
for ( _t_sbscl = {1} - one ; _t_sbscl >= one ; _t_sbscl = _t_sbscl - one )
[
_dst = _dst + _t_dst
]
]
]
else
[
_dst = zero
]
{0} = _dst"""
s=re.sub(pattern=r'(/\*.*?\*/)|(#.*?\n)', repl='', string=string, flags=re.M | re.S)
parser=Preprocessor()
ins=[]
for i in s.split("\n"):
if re.search("[/*%^-]", i)!=None:
if re.search("for\s\(", i)!=None:
syn_error=True
err=''
err=err+"Error - cannot parse such operations in the loop header."
print("Error - cannot parse such operations in the loop header.")
break
else:
syn_error=False
ins = ins + parser.parse(i)
else:
ins = ins + [i]
s=ins[:]
ins=[]
for i in s:
if i=='': continue
if re.search("\*", i)!=None:
dst, src1, src2=re.split("[=*]", i)
t=mult_macro_code.format(dst.strip(), src1.strip(), src2.strip())
t=re.sub(r"\[", "{", t, re.M)
t=re.sub(r"\]", "}", t, re.M)
ins=ins + t.split("\n")
elif re.search(r"/", i)!=None:
dst, src1, src2=re.split("[=/]", i)
t=div_macro_code.format(dst.strip(), src1.strip(), src2.strip())
t=re.sub(r"\[", "{", t, re.M)
t=re.sub(r"\]", "}", t, re.M)
ins=ins + t.split("\n")
elif re.search(r"%", i)!=None:
dst, src1, src2=re.split("[=%]", i)
t=mod_macro_code.format(dst.strip(), src1.strip(), src2.strip())
t=re.sub(r"\[", "{", t, re.M)
t=re.sub(r"\]", "}", t, re.M)
ins=ins + t.split("\n")
elif re.search(r"\^", i)!=None:
dst, src1, src2=re.split("[=^]", i)
t=exp_macro_code.format(dst.strip(), src1.strip(), src2.strip())
t=re.sub(r"\[", "{", t, re.M)
t=re.sub(r"\]", "}", t, re.M)
ins=ins + t.split("\n")
else:
ins = ins + [i]
parser=None
if 'end' not in ins[-1]: ins=ins+['end']
return "\n".join(ins)
if __name__=="__main__":
print(preprocessor("""x = 2
y = 3
z = 4
result = 0
one = 1
i = 0
bounds = 10
for ( ; i <= bounds ; i = i + one )
{
result = - ( x + - z )
}
end""")) # test input