Given S=>BaB
, B=>b
and null(B)
, the code is supposed to generate S=>BaB|aB|Ba|a
and B=>b
but it is not generating Ba
i.e the output is S=>BaB|aB|a
and B=>b
which is not correct.
The code can generate values correctly if terminal is not in the middle for example S=>BBa
, B=>b
gives out S=>BBa|Ba|a
and B=>b
The only case not working is when a terminal is on the middle of a repeated null production
Other examples where null(B)
S=>aBa
,B=>b
gives outS=>aBa|aa
andB=>b
S=>aaB
,B=>b
gives outS=>aaB|aa and B=>b
grammar = ["S = B a B", "B = b"]
def remove_null_productions(grammar):
null_productions = ["B"]
print("Null productions: {0}".format(null_productions))
new_productions = []
for rule in grammar:
if('$' in rule):
continue
else:
new_productions.append(rule.split(" "))
print("\nProductions:{0}".format(new_productions))
for null in null_productions:
for param in new_productions:
if(null in param[2:]):
temp = param[2:]
temp.remove(null)
if(len(temp)==0):
pass
else:
clever_array = param[0:2]
for val in temp:
clever_array.append(val)
new_productions.append(clever_array)
else:
pass
print("\nResultant Productions")
for rule in new_productions:
print(rule)
return new_productions
remove_null_productions(grammar)
I expect the output of grammar S=>BaB
and B=>b
to be
Null productions: ['B']
Productions:[['S', '=', 'B', 'a', 'B'], ['B', '=', 'b']]
Resultant Productions
['S', '=', 'B', 'a', 'B']
['B', '=', 'b']
['S', '=', 'a', 'B']
['S', '=', 'B', 'a']
['S', '=', 'a']
But the output is
Null productions: ['B']
Productions:[['S', '=', 'B', 'a', 'B'], ['B', '=', 'b']]
Resultant Productions
['S', '=', 'B', 'a', 'B']
['B', '=', 'b']
['S', '=', 'a', 'B']
['S', '=', 'a']