-
Notifications
You must be signed in to change notification settings - Fork 0
/
DK_test.py
32 lines (25 loc) · 1.08 KB
/
DK_test.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
from CFG_to_NFA import CFG_to_NFA
from NFA_to_DFA import NFA_to_DFA
def DK_test(G):
terminals = G['terminals']
N, rule_in = CFG_to_NFA(G)
D, state_index = NFA_to_DFA(N)
F = D['final']
# collect all the rules in all the final states
Final_rules = [[rule_in[q] for q in state_index[j]] for j in F]
print('\nRules in the final states:')
for rules in Final_rules: print(rules)
for rules in Final_rules:
c_rules = 0 # initialize the number of completed rules
for rule in rules:
a = rule['dot']
# rule is complete, then check if c_rules is already 1
if a == '':
if c_rules == 1:
return('\nFails, Failing rule: ' + str(rule) + '\n')
c_rules = c_rules + 1
# check for rules of type A -> u.av where a in terminal
if a in terminals:
return('\nFails, Failing rule: ' + str(rule) + '\n')
# G has passed the test if it has not failed yet
return('\nPasses\n')