-
Notifications
You must be signed in to change notification settings - Fork 0
/
retro_basic.py
318 lines (285 loc) · 9.69 KB
/
retro_basic.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
# must set EOF in terminal file
# change location of terminal and grammar
# change start symbol
# define lambda
lam = 'EMPTY'
###################################################################
# generate terminal ###############################################
###################################################################
file = open('./terminal.txt','r')
terminal = set([str(line.strip()) for line in file])
non_terminal = set()
file.close()
###################################################################
# generate grammar ################################################
###################################################################
grammar = dict()
startSymbol = 'pgm'
file = open('./grammar.txt','r')
for line in file:
tmp = line.strip().split()
key = tmp[0]
non_terminal.add(key)
grammar[key] = []
tmp = tmp[2:]
a = []
for i in range(len(tmp)):
if tmp[i] == '|':
grammar[key].append(a)
a = []
continue
if i == len(tmp)-1:
a.append(tmp[i])
grammar[key].append(a)
a = []
a.append(tmp[i])
#print(grammar)
#print(non_terminal)
file.close()
#####################################################################
# find first set ####################################################
#####################################################################
#print("find first set")
firstSet = dict()
def first_set(c,grammar):
global firstSet
# if c is terminal firstSet(terminal) = terminal or c is empty string
if c in terminal or c == lam:
return set([c])
# if c already in firstSet return it
if c in firstSet:
return firstSet[c]
# if c is first seen create empty set
if c not in firstSet:
firstSet[c] = set()
for g in grammar[c]:
i = 0
while i < len(g):
term = g[i]
toUnion = first_set(term,grammar)
firstSet[c] = firstSet[c].union(toUnion.difference(set([lam])))
if lam in toUnion:
i += 1
else:
break
if i == len(g):
firstSet[c] = firstSet[c].union(set([lam]))
return firstSet[c]
for a in non_terminal:
first_set(a,grammar)
#print(firstSet)
#####################################################################
# find follow set ###################################################
#####################################################################
#print("find follow set")
followSet = dict()
# init followSet followSet(startSymbol) = EOF other is empty set
for nt in non_terminal:
if nt == startSymbol:
followSet[nt] = set(['EOF'])
continue
followSet[nt] = set()
change = True
# while there are update
while change:
change = False
for g in non_terminal:
list_g = grammar[g]
# each l is each RHS term in grammar
for l in list_g:
for i in range(len(l)-1):
# A -> NB ,firstSet(B) is in followSet(N)
if (l[i] in non_terminal) and (l[i+1] in non_terminal):
tmp = followSet[l[i]].copy()
followSet[l[i]] = followSet[l[i]].union(firstSet[l[i+1]].difference(set([lam])))
# check change
if tmp != followSet[l[i]]:
change = True
# A -> Nx ,x is in followSet(N)
elif (l[i] in non_terminal) and (l[i+1] in terminal):
tmp = followSet[l[i]].copy()
followSet[l[i]] = followSet[l[i]].union(set([l[i+1]]))
# check change
if tmp != followSet[l[i]]:
change = True
# init j point to last of list l
j = len(l)-1
while j > 0:
# B -> XNB and lamda is in firstSet(B)
if l[j] == g and (lam in firstSet[l[j]]):
j -= 1
# A -> XNB and lamda is in firstSet(B), followSet(A) is in followSet(B)
elif (l[j] in non_terminal) and (lam in firstSet[l[j]]):
tmp = followSet[l[j]].copy()
followSet[l[j]] = followSet[l[j]].union(followSet[g])
# check change
if tmp != followSet[l[j]]:
change = True
j -= 1
# A -> XN, followSet(A) is in followSet(N)
elif l[j] in non_terminal:
tmp = followSet[l[j]].copy()
followSet[l[j]] = followSet[l[j]].union(followSet[g])
# check change
if tmp != followSet[l[j]]:
change = True
break
# A -> Bx
elif l[j] in terminal:
break
#print(followSet)
#####################################################################
# use scanner and factorize grammar #################################
#####################################################################
#print("use scanner")
from scanner import Scanner
# put location of input code
scan = Scanner("./input.txt")
stream = scan.getStream()
list_of_grammar = []
haveEmpty = set()
for key in grammar:
tmp_g = grammar[key]
for l in tmp_g:
list_of_grammar.append({key : l})
if lam in l:
haveEmpty.add(key)
#print(stream)
#####################################################################
# parsing ###########################################################
#####################################################################
from Stack import Stack
s = Stack()
s.push("EOF")
s.push(startSymbol)
# pointer in stream
pointer = 0
# output B-code
output = []
valid = True
def fs(c):
if c in firstSet:
return firstSet[c]
if c in terminal:
return set([c])
def isInFirstSet(token_type,top):
return token_type in fs(top) or (token_type == 'number' and 'line_num' in fs(top)) or (token_type == 'number' and 'const' in fs(top))
def updateOutput(type,val):
global output
if type == 'line_num':
if len(output) > 0 and output[-1] == 14:
output.append(val)
elif len(output) > 7 and output[-8] == 13:
output.append(14)
output.append(val)
else:
output.append(10)
output.append(val)
elif type == 'id':
output.append(11)
output.append(val+1)
elif type == 'const':
output.append(12)
output.append(val)
elif type == 'IF':
output.append(13)
output.append(0)
elif type == 'GOTO':
output.append(14)
#output.append(val)
elif type == 'PRINT':
output.append(15)
output.append(0)
elif type == 'STOP':
output.append(16)
output.append(0)
elif type == '+':
output.append(17)
output.append(1)
elif type == '-':
output.append(17)
output.append(2)
elif type == '<':
output.append(17)
output.append(3)
elif type == '=':
output.append(17)
output.append(4)
while pointer < len(stream):
if s.isEmpty():
break
top = s.top()
current_token = stream[pointer]
token_type = current_token[0]
token_val = current_token[1]
#print(top,token_type,token_val)
# token_type is in firstSet[top]
if isInFirstSet(token_type,top):
grammar_can_use = []
for c in list_of_grammar:
if top in c:
grammar_can_use.append(c[top])
#print("grammar can use "+str(grammar_can_use))
choose = False
for g in grammar_can_use:
if isInFirstSet(token_type,g[0]):
if top == 'exp' and stream[pointer+1][0] == '+':
if g[1] != '+':
continue
if top == 'exp' and stream[pointer+1][0] == '-':
if g[1] != '-':
continue
if top == 'cond' and stream[pointer+1][0] == '<':
if g[1] != '<':
continue
if top == 'cond' and stream[pointer+1][0] == '=':
if g[1] != '=':
continue
if top == 'asgmnt' and stream[pointer+3][0] not in ['+','-'] and g[2] == 'exp':
continue
#print("grammar choose "+str(g))
choose = True
s.pop()
for i in range(1,len(g)+1):
s.push(g[len(g)-i])
break
#print(token_type,top)
if (token_type == 'number' and top == 'const') or (token_type == 'number' and top == 'line_num') or (token_type == top):
choose = True
#print(choose)
if choose == False:
valid = False
break
else:
valid = False
if not valid:
break
# case match token
if top == token_type:
pointer += 1
updateOutput(top,token_val)
s.pop()
elif top == 'line_num' and token_type == 'number':
pointer += 1
updateOutput('line_num',token_val)
s.pop()
elif top == 'const' and token_type == 'number':
pointer += 1
updateOutput('const',token_val)
s.pop()
#print(output)
# case not valid
if (top == 'EOF' and token_type != 'EOF') or (top in terminal and ((token_type == 'number' and top not in ['line_num','const']) or (token_type != 'number' and top != token_type))):
valid = False
break
if valid:
#print("out")
#print(output)
file = open("./output.txt","w")
for o in output:
file.write(str(o))
file.write(" ")
file.close()
print("Compile success, check output.txt file.")
else:
print("Compile error, please check your code.")