forked from oneamitj/slr-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slr_short.py
451 lines (338 loc) · 10.9 KB
/
slr_short.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
'''
@author Amit Joshi
main():
This is the primary module that is executed at first. Here user to asked to enter the string to be parsed and also all the feedback (like: grammar rules, goto operations, generated states, reduction rules and parse table) are provided to user. Final result about if the string is successfully parsed or not, is also printed here.
read_grammar():
This function asks user to provide file name where grammar are stored. It reads the file for grammar and place it on list grammar. From the rules terminals and nonterminals are identified and stored in the lists named terminals and non_terminals respectively, where as rules are stored in the dictionary called rule_dict.
augmented_grammar():
Using the grammar list created in read_grammar(), it generates list of augmented grammar and place it on new_grammar.
compute_I0():
Here I0 is calculated separately using the rule 1 in new_grammar. The calculated I0 is stored in dictionary I_n with key as 0. So the dictionary I_n[0] represents state I0.
GOTO():
Now as I0 state is found, using this initial state I_n[0] further states are generated recursively until all the non repeated valid states of the given grammar is generated and stored in the dictionary I_n with their respective state as key. While generating all the state the list shift_list keeps track of all the shift operation that are valid for the given grammar.
follow(var):
This generates the follow of nonterminal var and returns it.
reduction():
Here all the possible reduction rules for the given grammar are calculated and stored in the list named reduction_list. Each item of reduction_list is another list whose first and second element gives reduction condition and third provides the rule to be used for for that condition.
Conflict():
Here, availability of conflict in the given grammar is analyzed. Stores each SR conflict in list SR and RR conflict in list RR. It returns True if there conflict else False.
test(string):
Using reduction_list and shift_list, action_list is created which resembles the SLR parse table. Now with the help of action_list, stack and string the parsing is done, while displaying each step and content of stack, string and current action thats been called. If the string is acceptable it returns 1 and if not accepted returns 0.
'''
import copy
grammar = []
new_grammar = []
terminals = []
non_terminals = []
I_n = {}
shift_list = []
reduction_list = []
action_list = []
rule_dict = {}
follow_dict = {}
SR = []
RR = []
def Conflict():
global SR, RR, shift_list, reduction_list
conflict = False
# SR conflict if shift and Reduce occurs for same condition
for S in shift_list:
for R in reduction_list:
if S[:2] == R[:2]:
SR.append([S, R])
conflict = True
# RR conflict if 2 Reduce occurs for same condition
for R1 in reduction_list:
for R2 in reduction_list:
if R1 == R2:
continue
if R1[:2] == R2[:2]:
RR.append(R1)
conflict = True
return conflict
def read_grammar():
global grammar, terminals, non_terminals, rule_dict
file_name = raw_input("Enter Grammar File Name:: ")
# open given file
try:
grammar_file = open(file_name, "r")
except:
# grammar_file = open("grammar", "r")
print "Cannot Find File Named", file_name
exit(0)
# add garmmar
for each_grammar in grammar_file:
grammar.append(each_grammar.strip())
# find terminals
if each_grammar[0] not in non_terminals:
non_terminals.append(each_grammar[0])
# find non terminals
for each_grammar in grammar:
for token in each_grammar.strip().replace(" ", "").replace("->", ""):
if token not in non_terminals and token not in terminals:
terminals.append(token)
# generate dictionary of rules
for l in range(1, len(grammar)+1):
rule_dict[l] = grammar[l-1]
# return grammar
# print terminals
def augmented_grammar():
global grammar, new_grammar
read_grammar()
# if non augmented grammar is given, augment it
if "'" not in grammar[0]:
grammar.insert(0, grammar[0][0]+"'"+"->"+grammar[0][0])
# just add . infornt of each rule
new_grammar = []
for each_grammar in grammar:
idx = each_grammar.index(">")
# print each_grammar.split()
each_grammar = each_grammar[:idx+1]+"."+each_grammar[idx+1:]
new_grammar.append(each_grammar)
# print new_grammar
def compute_I0():
global new_grammar, non_terminals, I_n
augmented_grammar()
grammar2add = []
# add first rule to I(0)
grammar2add.append(new_grammar[0])
i = 0
# check for terminals in new_grammar[0]
for each in grammar2add:
current_pos = each.index(".")
current_variable = each[current_pos+1]
# print current_variable
if current_variable in non_terminals:
for each_grammar in new_grammar:
if each_grammar[0] == current_variable and each_grammar not in grammar2add:
grammar2add.append(each_grammar)
I_n[i] = grammar2add
# print grammar2add
def GOTO():
global grammar, non_terminals, terminals, I_n, shift_list
compute_I0()
variables = non_terminals + terminals
# variables = ["E", "T", "F", "(", ")", "+", "*", "i"]
# I_n[0]
i = 0
current_state = 0
done = False
while (not done):
for each_variable in variables:
grammar2add = []
# print each_variable, "------------------"
try:
for each_rule in I_n[current_state]:
if each_rule[-1] == ".":
continue
dot_idx = each_rule.index(".")
if each_rule[dot_idx+1] == each_variable:
rule = copy.deepcopy(each_rule)
rule = rule.replace(".", "")
rule = rule[:dot_idx+1]+"."+rule[dot_idx+1:]
# rule[dot_idx] = rule[dot_idx+1]
# rule[dot_idx+1] = "."
# print rule
grammar2add.append(rule)
for rule in grammar2add:
dot_idx = rule.index(".")
if rule[-1] == ".":
pass
else:
current_variable = rule[dot_idx+1]
if current_variable in non_terminals:
for each_grammar in new_grammar:
if each_grammar[0] == current_variable and each_grammar[1] != "'" and each_grammar not in grammar2add:
grammar2add.append(each_grammar)
# grammar2add.append([current_state, current_variable])
# rule[dot_idx] = rule[dot_idx+1]
# rule[dot_idx+1] = "."
# grammar2add.append(rule)
except:
done = True
break
if grammar2add:
# for value in I_n.viewvalues():
# for item in I_n.iteritems():
# if grammar2add == item[1]:
# I_n[i] = item[0]
# break
if grammar2add not in I_n.values():
i += 1
I_n[i] = grammar2add
# if isinstance(I_n[i], int):
# shift_list.append([x, each_variable, I_n[i]])
# else:
# if grammar2add in I_n.values():
for k,v in I_n.iteritems():
if grammar2add == v:
idx = k
shift_list.append([current_state, each_variable, idx])
# print grammar2add, "AFFFFFFFFF", idx
current_state += 1
def follow(var):
global rule_dict, follow_dict, terminals
value = []
if var == rule_dict[1][0]:
value.append("$")
for rule in rule_dict.values():
lhs, rhs = rule.split("->")
if var == rule[-1]:
for each in follow(rule[0]):
if each not in value:
value.append(each)
if var in rhs:
idx = rhs.index(var)
try:
if rhs[idx+1] in non_terminals and rhs[idx+1] != var:
for each in follow(rhs[idx+1]):
value.append(each)
# print "AAAA", var
else:
value.append(rhs[idx+1])
# print "BBB", var
except:
# print "Error at", var
pass
# if var == "S":
# value = ["$"]
# elif var == "L":
# value = ["=", "$"]
# elif var == "R":
# value = ["=", "$"]
return value
def reduction():
global I_n, rule_dict, reduction_list
reduction_list.append([1, "$", "Accept"])
for item in I_n.iteritems():
try:
for each_production in item[1]:
lhs, rhs = each_production.split(".")
for rule in rule_dict.iteritems():
if lhs == rule[1]:
# print lhs
f = follow(lhs[0])
# print "FOOOOOO", lhs[0], f
for each_var in f:
reduction_list.append([item[0], each_var, "R"+str(rule[0])])
except:
# print item
pass
def test(string):
global action_list, shift_list, reduction_list
done = False
stack = []
stack.append(0)
print "\n\nSTACK\t\tSTRING\t\tACTION"
while not done:
Reduce = False
Shift = False
# Check for reduction
for r in reduction_list:
# Reduce
if r[0] == int(stack[-1]) and r[1] == string[0]:
Reduce = True
print ''.join(str(p) for p in stack), "\t\t", string, "\t\t", "Reduce", r[2]
if r[2] == 'Accept':
return 1
# print "---ACCEPTED---"
# done = True
# break
var = rule_dict[int(r[2][1])]
lhs, rhs = var.split("->")
for x in range(len(rhs)):
stack.pop()
stack.pop()
var = lhs
stack.append(var)
for a in action_list:
# print stack
if a[0] == int(stack[-2]) and a[1] == stack[-1]:
stack.append(str(a[2]))
break
# print "String", string
# print "RRR", stack
# else:
# Reduce = False
# Check for shift
for g in shift_list:
# Shift
if g[0] == int(stack[-1]) and g[1] == string[0]:
Shift = True
print ''.join(str(p) for p in stack), "\t\t", string, "\t\t", "Shift", "S"+str(g[2])
stack.append(string[0])
stack.append(str(g[2]))
string = string[1:]
# print "String", string
# print "RRR", stack
if not Reduce and not Shift:
print ''.join(str(p) for p in stack), "\t\t", string
# print "---NOT ACCEPTED---"
return 0
# done = True
# break
def main():
global I_n, shift_list, reduction_list, action_list, SR, RR
GOTO()
reduction()
print "\n--------------------RULES--------------------"
for item in rule_dict.iteritems():
print item
# try:
# print
# print "S", follow("S")
# print "L", follow("L")
# print "R", follow("R")
# except:
# pass
print "\n--------------------AUGMENTED RULES--------------------"
for item in new_grammar:
print item.replace(".", "")
print "\n--------------------STATES--------------------"
for item in I_n.iteritems():
print item
print "\n--------------------GOTO OPERATIONS--------------------"
for item in shift_list:
print item
print "\n--------------------REDUCTION--------------------"
for item in reduction_list:
print item
if Conflict():
if SR != []:
print "SR conflict"
for item in SR:
print item
print
if RR != []:
print "RR conflict"
for item in RR:
print item
print
exit(0)
else:
print "\nNO CONFLICT\n"
print "Terminals:", terminals
print "NonTerminals:", non_terminals
print
action_list.extend(shift_list)
action_list.extend(reduction_list)
# print "\n--------------------ACTION--------------------"
# for item in action_list:
# print item
# string = "i*i+i$"
string = raw_input("\n\nEnter String:: ")
try:
if string[-1] != "$":
string = string + "$"
except:
print "InputError"
exit(0)
print "\nTest String:", string
result = test(string)
if result == 1:
print "---ACCEPTED---"
elif result == 0:
print "---NOT ACCEPTED---"
return 0
if __name__ == '__main__':
main()