-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnfa.c
More file actions
executable file
·103 lines (83 loc) · 2.71 KB
/
nfa.c
File metadata and controls
executable file
·103 lines (83 loc) · 2.71 KB
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
#include "nfa.h"
/* Initializes an NFA structure */
NFA* nfa(int states_no, int edges_no) {
NFA *n = malloc(sizeof(NFA));
n->states_no = states_no;
n->states = malloc(sizeof(int)*states_no);
n->transitions = malloc(sizeof(Edge)*edges_no);
n->trans_no = edges_no;
return n;
}
/* Initializes an Edge structure */
Edge* edge(int initial_id, int final_id, char symbol) {
Edge *e = malloc(sizeof(Edge));
e->src = initial_id;
e->dst = final_id;
e->val = symbol;
return e;
}
/* Prints a NFA structure */
void nfa_print(NFA *nfa) {
printf("\n");
int i;
int j;
printf("========== NFA ==========\n");
printf("| Initial state: %d, Final state: %d\n", nfa->initial_state, nfa->final_state);
printf("| States: ");
for (i=0; i<nfa->states_no; i++) {
printf("%d ", nfa->states[i]);
}
printf("\n");
printf("| Edges: \n");
for (i=0; i<nfa->trans_no; i++) {
Edge *e = nfa->transitions[i];
printf("| %d --%c--> %d\n", e->src, e->val, e->dst);
}
printf("=========================\n");
}
/* Frees up memory by deallocating NFA */
void nfa_free(NFA *nfa) {
free(nfa->states); // deallocate states array
int i;
for(i=0; i<nfa->trans_no; i++) { // deallocate transitions array
free(nfa->transitions[i]);
}
free(nfa->transitions);
free(nfa); // deallocate nfa itself
}
/* Topological sort algorithm */
void topsort(NFA *nfa, IntStack *s) {
int max_number = nfa->final_state+1; // final state is the highest possible state id (itself, that's why the +1) in the nfa
int visited[max_number];
// set visited to false
int i;
for (i=0; i<=max_number; i++) {
visited[i] = 0;
}
// iterate through states; if not visited yet, visit it
int state;
for (i=0; i<nfa->states_no; i++) {
state = nfa->states[i];
if (visited[state] == 0) {
topsort_rec(nfa, s, visited, state);
}
}
}
/* Recursive part for topological sort */
void topsort_rec(NFA *nfa, IntStack *s, int *visited, int i) {
// visit the node
visited[i] = 1;
// iterare through all transitions looking for adjacents to i
int j, dest;
for (j=0; j<nfa->trans_no; j++) {
// if a node is adjacent to i and it's not visited yet, visit it
if (nfa->transitions[j]->src == i) {
dest = nfa->transitions[j]->dst;
if (visited[dest] == 0) {
topsort_rec(nfa, s, visited, dest);
}
}
}
// push node to stack
int_stack_push(s, i);
}