8def get_automaton(gr):
9 dfa = Automaton()
10 dfa.states = [closure(gr, [(0, 0)])]
11 next_id = 0
12
13 dfa.id_from_state[dfa.states[-1]] = next_id
14 next_id += 1
15
16 seen =
set(dfa.states)
17 set_queue = dfa.states
18 while len(set_queue) > 0:
19 new_elements = []
20 for item_set in set_queue:
21 item_set_id = dfa.id_from_state[item_set]
22
23 for symbol in gr.symbols:
24 next_item_set = goto(gr, item_set, symbol)
25 if len(next_item_set) == 0:
26 continue
27
28 if next_item_set not in seen:
29 new_elements += [next_item_set]
30 seen.add(next_item_set)
31
32 dfa.states += [next_item_set]
33 dfa.id_from_state[dfa.states[-1]] = next_id
34 next_id += 1
35
36 dfa.goto[(item_set_id, symbol)] = dfa.id_from_state[next_item_set]
37
38 set_queue = new_elements
39
40 return dfa
41
42