-
Notifications
You must be signed in to change notification settings - Fork 0
/
CFG (remove non-terminals).py
97 lines (85 loc) · 3.27 KB
/
CFG (remove non-terminals).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
class CFG:
def __init__(self, **args):
self.terminals = args['terminals']
self.nonterminals = args['nonterminals']
self.start = args['start']
self.rules = args['rules']
self.remove_e()
self.remove_nongenerative_nonterminals()
self.remove_unreachable_nonterminals()
def remove_e(self):
keys = list(self.rules.keys())
for k in keys:
for el in self.rules[k]:
if el == 'e':
self.rules[k].remove(el)
def remove_nongenerative_nonterminals(self):
keys = list(self.rules.keys())
grammar_copy = []
grammar = []
for non_term in keys:
for el in self.rules[non_term]:
if el[0].islower():
grammar.append(non_term)
break
while grammar != grammar_copy:
grammar_copy = grammar
for non_term in keys:
for el in self.rules[non_term]:
if el[0].islower() or el[0] in grammar_copy:
grammar.append(non_term)
for k in keys:
if k not in grammar:
self.rules.pop(k)
self.nonterminals.remove(k)
def remove_unreachable_nonterminals(self):
keys = list(self.rules.keys())
grammar = ['S']
grammar_copy = []
while grammar != grammar_copy:
grammar_copy = grammar
for nonterminal in grammar_copy:
if nonterminal in keys:
for elem in self.rules[nonterminal]:
for el in list(elem):
if el.isupper() and el not in grammar:
grammar.append(el)
for el in keys:
if el not in grammar:
self.rules.pop(el)
self.nonterminals.remove(el)
def disappearing_nonterminals(grammar):
keys = list(grammar.rules.keys())
disappear = []
for term in keys:
for el in grammar.rules[term]:
if el == 'e':
vanishings.append(term)
gr = []
while gr != disappear:
gr = disappear
for term in keys:
for el in grammar.rules[term]:
if el.isupper():
flag = True
for elem in el.split():
if elem not in disappear:
flag = False
break
if flag:
disappear.append(term)
return disappear
terminals = ['a', 'b', 'c']
nonterminals=['S', 'A', 'B']
start = 'S'
rules = {'S': ['Ac', 'e', 'a', 'e' ], 'A':['A', 'Sb', 'e'], 'B':['e', 'a']}
#terminals = ['alt', 'cat', 'ast', 'chr', 'nil']
#nonterminals=['S', 'A', 'C', 'I']
#rules = {'S': [['A', 'alt', 'S'], ['A']], 'A':[['C', 'cat', 'A'], ['C']], 'C':[['I', 'ast'], ['I']], 'I':[['nil'], ['chr'], [('S')]]}
print("Initial grammar:")
print(rules)
grammar = CFG(terminals=terminals, nonterminals=nonterminals, start=start, rules=rules)
print("Final grammar without e and unproductive and unreachable non-terminals")
print(grammar.rules)
print("Set of disappeearing nonterminals from the given grammar:")
print(disappearing_nonterminals(grammar))