-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFA.c
More file actions
68 lines (58 loc) · 1.59 KB
/
DFA.c
File metadata and controls
68 lines (58 loc) · 1.59 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
/*
*Batch Number 6
*Abhinav Bhatia (2011A7PS371P)
*Mukul Bhutani (2011A7PS343P)
*/
#include "DFA.h"
#include <stdlib.h>
#include <string.h>
void dfa_makeNextTransition(DFA* dfa, char c)
{
int transitionID;
DFAState* pCurrentState = dfa->states[dfa->currentStateID];
for (transitionID = 0; transitionID < pCurrentState->numberOfTransitions; transitionID++)
{
if (pCurrentState->transitions[transitionID].condition(c))
{
dfa->currentStateID = pCurrentState->transitions[transitionID].toStateID;
return;
}
}
//take the default transition
dfa->currentStateID = pCurrentState->defaultToStateID;
}
void dfa_addState(DFA* pDFA, DFAState* newState)
{
newState->ID = pDFA->numberOfStates;
pDFA->states[pDFA->numberOfStates] = newState;
pDFA->numberOfStates++;
}
void dfa_addTransition(DFA* dfa, int fromStateID, int(*condition)(char), int toStateID)
{
DFAState* state = dfa->states[fromStateID];
state->transitions[state->numberOfTransitions].condition = condition;
state->transitions[state->numberOfTransitions].toStateID = toStateID;
state->numberOfTransitions++;
}
DFAState* dfa_createState(int hasAction, char* actionName)
{
DFAState* newState = (DFAState*)malloc(sizeof(DFAState));
strcpy(newState->actionName, actionName);
newState->defaultToStateID = -1;
newState->hasAction = hasAction;
newState->ID = -1;
newState->numberOfTransitions = 0;
return newState;
}
DFA* dfa_createDFA()
{
DFA* dfa = (DFA*)malloc(sizeof(DFA));
dfa->numberOfStates = 0;
dfa->startStateID = -1;
dfa->currentStateID = -1;
return dfa;
}
void dfa_reset(DFA* dfa)
{
dfa->currentStateID = dfa->startStateID;
}