-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdfaType.c
163 lines (129 loc) · 3.12 KB
/
dfaType.c
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
DV2: Algoritmer och problemlösning.
File: dfaType.c
Name: Adam Lindgren & Jakob Lundin.
CS-user: dv17aln & c14jln
Date: 28 Februari 2018
Description: Creates a definite finite automaton.
*/
#include "dfaType.h"
#include <stdio.h>
//Internal struct of node/state.
typedef struct{
char *label;
table *connections;
}dfaNode;
struct dfaType{
size_t range;
char *start;
table *nodes;
table *accepted;
};
//Declaration of helpfunctions.
static void freeNode(void *n);
static char *nodeGetLabel(dfaNode *n);
static dfaNode *getNode(dfaType *dfa, char *str);
//Defining helpfunctions.
/* Function: nodeGetLabel
* Description: Returns label string of given dfaNode
* Input: dfaNode
* Output: string
*/
static char *nodeGetLabel(dfaNode *n){
return n->label;
}
/* Function: nodeFree
* Description: Frees all allocated memory in dfaNode structure
* Input: dfaNode
* Output: None
*/
static void freeNode(void *v){
dfaNode *n = v;
table_kill(n->connections);
free(n);
}
/* Function: getNode
* Description: Returns the node corresponding to the given string.
* Input: dfaNode, string
* Output: dfaNode
*/
static dfaNode *getNode(dfaType *dfa, char *str){
return table_lookup(dfa->nodes, str);
}
bool stringcmp(void *p1, void *p2){
int i = 0;
char *str1 = p1;
char *str2 = p2;
while(str1[i] != 0) {
if (str1[i] != str2[i]) {
return 0;
}
i++;
}
if (str2[i] != 0) {
return 0;
} else {
return 1;
}
}
size_t hashFunc(void *key){
int e = 0;
char *str = key;
for (int i = 0; str[i]; i++) {
e += str[i];
}
return e;
}
dfaType *dfa_init(size_t cap, char *start, size_t range){
dfaType *dfa = malloc(sizeof(dfaType));
dfa->range = range;
dfa->start = start;
dfa->nodes = table_empty(cap, &stringcmp, &hashFunc, *free, &freeNode);
dfa->accepted = table_empty(cap, &stringcmp, &hashFunc, NULL, NULL);
return dfa;
}
char *dfa_getStart(dfaType *dfa){
char *s = dfa->start;
return s;
}
void dfa_addNode(dfaType *dfa, char *label){
dfaNode *n = malloc(sizeof(dfaNode));
n->label = label;
n->connections = table_empty(dfa->range, stringcmp, hashFunc,
*free, NULL);
table_insert(dfa->nodes, (void*)label, n);
}
void dfa_addConnection(dfaType *dfa, char *orig, char *input, char *dest){
dfaNode *origNode = getNode(dfa, orig);
dfaNode *destNode = getNode(dfa, dest);
table *t = origNode->connections;
table_insert(t, input, destNode);
}
void dfa_setAccepted(dfaType *dfa, char *label){
dfaNode *n = getNode(dfa, label);
table_insert(dfa->accepted, nodeGetLabel(n), n);
}
void dfa_kill(dfaType *dfa){
table_kill(dfa->nodes);
table_kill(dfa->accepted);
free(dfa->start);
free(dfa);
}
char *dfa_traverse(dfaType *dfa, char *orig, char *input){
dfaNode *origNode = getNode(dfa, orig);
dfaNode *destNode = table_lookup(origNode->connections, input);
if (destNode != NULL) {
return nodeGetLabel(destNode);
} else {
fprintf(stderr, "\nINVALID INPUT FOR THIS DFA: '%s'\n", input);
printf("Exiting....\n");
exit(2);
}
}
bool dfa_checkAccepted(dfaType *dfa, char *endNodeLabel){
if (table_lookup(dfa->accepted, endNodeLabel)) {
return true;
} else {
return false;
}
}