-
Notifications
You must be signed in to change notification settings - Fork 1
/
fsm.cpp
125 lines (109 loc) · 2.87 KB
/
fsm.cpp
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
#include"state.h"
#include"fsm.h"
#include<iostream>
#include<fstream>
using namespace std;
fsm::fsm(char* filename) {
init(filename);
readSymbols();
readStates();
readTransitions();
close();
}
void fsm::init(char* filename) {
this->readFile.open(filename);
}
void fsm::readSymbols() {
readFile >> no_of_ip_symbols;
inputs = new char[no_of_ip_symbols];
for(int n=0;n<no_of_ip_symbols;n++) {
readFile >> inputs[n];
}
}
void fsm::dump() {
cout << "The Details of the fsm : " << "\n\n";
cout << "The number of input symbols are : " << this->no_of_ip_symbols << "\n";
cout << "The input symbols are : " << "{ ";
for(int i=0;i<this->no_of_ip_symbols;i++) {
cout << this->inputs[i] << " ";
} cout << " }" << "\n";
cout << "There are "<< this->no_of_states << " states in the automata...\n";
cout << "The states are as : { ";
for(int i=0;i<this->no_of_states;i++) {
cout << states[i]->name() << " ";
} cout << "}\n";
cout << "Initial state : " << states[0]->name() << "\n";
cout << "Final state(s) : { ";
for(int i=0;i<no_of_states;i++) {
if(states[i]->isFinal()) {
cout << states[i]->name() << " ";
}
}cout << "}\n";
}
void fsm::close() {
this->readFile.close();
}
void fsm::readStates() {
string tmp;
state* tstate;
readFile >> this->no_of_states;
this->states = new state*[no_of_states];
for(int i=0;i<this->no_of_states;i++) {
readFile >> tmp;
states[i] = new state(tmp);
}
readFile >> no_of_fstates;
for(int i=0;i<no_of_fstates;i++) {
readFile >> tmp;
tstate=lookup(tmp);
if(tstate!=NULL) {
tstate->setFinal(true);
}
}
currentState=states[0];
}
state* fsm::lookup(string name) {
for(int i=0;i<no_of_states;i++) {
if(states[i]->name() == name) {
return states[i];
}
}
return NULL;
}
void fsm::dumpCurrentState() {
cout << "The current state is : " << currentState->name();
}
state* fsm::giveCurrentState() {
return currentState;
}
void fsm::readTransitions() {
string tmp;
state* tstate;
for(int i=0;i<no_of_states;i++) {
states[i]->transitions = new state*[no_of_ip_symbols];
states[i]->no_of_transitions = no_of_ip_symbols;
for(int j=0;j<no_of_ip_symbols;j++) {
readFile >> tmp;
tstate=lookup(tmp);
states[i]->transitions[j]=tstate;
}
}
}
void fsm::dumpAllStates() {
for(int i=0;i<no_of_states;i++) {
states[i]->dump();
cout <<"\n";
}
}
void fsm::makeTransition(char k) {
int ip = getInputId(k);
currentState = currentState->transitions[ip];
}
int fsm::getInputId(char k) {
for(int i=0;i<no_of_ip_symbols;i++) {
if(inputs[i]==k) {
return i;
}
}
return -1;
}