fsa.hpp
3.54 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
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
164
165
166
167
168
169
170
/*
* File: fsa.hh
* Author: mlenart
*
* Created on October 17, 2013, 2:00 PM
*/
#ifndef FSA_HPP
#define FSA_HPP
//#include <iostream>
#include <cstring>
#include <typeinfo>
#include <cassert>
template <class T> class State;
template <class T> class FSA;
template <class T> class Deserializer;
template <class T>
class Deserializer {
public:
/**
* Deserialize object from ptr.
* Returns number of bytes read or -1 on error.
*/
virtual int deserialize(const unsigned char* ptr, T& object) const = 0;
};
class StringDeserializer : public Deserializer<char*> {
public:
StringDeserializer() {
}
/**
* Deserialize object from ptr.
* Returns number of bytes read or -1 on error.
*/
int deserialize(const unsigned char* ptr, char*& text) const {
text = const_cast<char*> (reinterpret_cast<const char*> (ptr));
return strlen(text) + 1;
}
};
/**
* Finite state automaton.
*/
template <class T>
class FSA {
public:
/**
* Get this automaton's initial state.
*/
State<T> getInitialState() const {
return State<T>(*this);
}
bool tryToRecognize(const char* input, T& value) const {
State<T> currState = this->getInitialState();
int i = 0;
while (!currState.isSink() && input[i] != '\0') {
currState.proceedToNext(input[i]);
i++;
}
if (currState.isAccepting()) {
value = currState.getValue();
return true;
}
else {
return false;
}
}
virtual ~FSA() {
}
protected:
FSA(const unsigned char* ptr, const Deserializer<T>& deserializer)
: startPtr(ptr), deserializer(deserializer) {
}
/**
* Proceed to next state
*
* @param fromPointer - wskaźnik
* @param c - char for the transition.
* @return next state
*/
virtual void proceedToNext(const char c, State<T>& state) const = 0;
const unsigned char* startPtr;
const Deserializer<T>& deserializer;
friend class State<T>;
private:
// FSA();
};
template <class T>
class SimpleFSA : public FSA<T> {
public:
SimpleFSA(const unsigned char* ptr, const Deserializer<T>& deserializer);
virtual ~SimpleFSA();
protected:
void proceedToNext(const char c, State<T>& state) const;
private:
};
#include "_simple_fsa_impl.hpp"
/**
* A state in an FSA.
*/
template <class T>
class State {
public:
/**
* Is this a "sink" state - non-accepting state without outgoing transitions
*/
bool isSink() const;
/**
* Is this an accepting state
*/
bool isAccepting() const;
/**
* Get next state proceeding a transition for given character.
*/
void proceedToNext(const char c);
/**
* Get value of this state.
* Makes sense only for accepting states.
* For non-accepting states is throws an exception.
*/
T getValue() const;
/**
* Get the size (in bytes) of this state's value.
* Makes sense only for accepting states.
* For non-accepting states is throws an exception.
*/
unsigned int getValueSize() const;
unsigned int getOffset() const;
void setNext(const unsigned int offset);
void setNext(const unsigned int offset, const T& value, const unsigned int valueSize);
void setNextAsSink();
State(const FSA<T>& fsa);
virtual ~State();
private:
const FSA<T>& fsa;
unsigned int offset;
bool accepting;
bool sink;
T value;
int valueSize;
};
#include "_state_impl.hpp"
#endif /* FSA_HPP */