-
Notifications
You must be signed in to change notification settings - Fork 0
/
STokenizer.cpp
140 lines (115 loc) · 4.47 KB
/
STokenizer.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "make_table_functions.h"
#include "STokenizer.h"
STokenizer::STokenizer()
{
memset(_buffer, 0, sizeof(_buffer));
_pos = 0;
make_table(_table);
}
STokenizer::STokenizer(char str[]) {
strcpy(_buffer, str);
_pos = 0;
make_table(_table);
}
STokenizer::STokenizer(string str) {
strcpy(_buffer, str.c_str());
_pos = 0;
make_table(_table);
}
bool STokenizer::more() {
return static_cast<unsigned>(_pos) < strlen(_buffer);
}
bool STokenizer::done() {
return static_cast<unsigned>(_pos) >= strlen(_buffer);
}
STokenizer& operator>>(STokenizer& s, Token& t) {
int t_type = UNKNWN;
string token;
if(isalpha(s._buffer[s._pos]))
t_type = ALPHA;
else if (isdigit(s._buffer[s._pos]))
t_type = NUM;
else if (ispunct(s._buffer[s._pos]))
t_type = PUNCT;
else if (isspace(s._buffer[s._pos]))
t_type = SPACE;
else
t_type = UNKNWN;
if(s.get_token(token))
t = Token(token, t_type);
return s;
}
void STokenizer::set_string(char *str) {
strcpy(_buffer, str);
_pos = 0;
}
void STokenizer::make_table(int _table[][MAX_COLUMNS]) {
for (unsigned i = 0; i < MAX_ROWS; i++)
for(unsigned j = 0; j < MAX_COLUMNS; j++)
_table[i][j] = -1;
_table[0][0] = fail; // start-state is a fail state
// ====================== ALPHA ================================
mark_cells(0, _table, 'A', 'Z', 1); // setting AB..YZ to state 1
mark_cells(0, _table, 'a', 'z', 1); // setting ab..yz to state 1
mark_cells(1, _table, 'A', 'Z', 1); // setting AB..YZ to state 1
mark_cells(1, _table, 'a', 'z', 1); // setting ab..yz to state 1
_table[1][0] = success;
// ====================== PUNCT ================================
mark_cells(0, _table, '!', '/', 2); // setting !".../ to state 2
mark_cells(0, _table, ':', '@', 2); // setting :;..?@ to state 2
mark_cells(0, _table, '[', '`', 2); // setting [\.._' to state 2
mark_cells(0, _table, '{', '~', 2); // setting {\}~ to state 2
mark_cells(2, _table, '!', '/', 2); // setting !".../ to state 2
mark_cells(2, _table, ':', '@', 2); // setting :;..?@ to state 2
mark_cells(2, _table, '[', '`', 2); // setting [\.._' to state 2
mark_cells(2, _table, '{', '~', 2); // setting {\}~ to state 2
_table[2][0] = success;
// ====================== WHITE SPACE ===========================
_table[0][static_cast<int>(' ')] = 3; // setting up white space chars to
_table[0][static_cast<int>('\t')] = 3; // state 3 for initial state
_table[0][static_cast<int>('\r')] = 3;
_table[0][static_cast<int>('\n')] = 3;
_table[0][static_cast<int>('\v')] = 3;
_table[0][static_cast<int>('\f')] = 3;
_table[3][static_cast<int>(' ')] = 3; // -||- for the 3rd state
_table[3][static_cast<int>('\t')] = 3;
_table[3][static_cast<int>('\r')] = 3;
_table[3][static_cast<int>('\n')] = 3;
_table[3][static_cast<int>('\v')] = 3;
_table[3][static_cast<int>('\f')] = 3;
_table[3][0] = success;
// ====================== NUMBERS ===============================
mark_cells(0, _table, '0', '9', 4); // setting initial char recognition
mark_cells(4, _table, '0', '9', 4); // setting a loop on 4th state
_table[4][static_cast<int>('.')] = 5; // input of a decimal
mark_cells(5, _table, '0', '9', 6); // setting a loop on 4th state
mark_cells(6, _table, '0', '9', 6); // setting a loop on 4th state
_table[4][0] = success;
_table[5][0] = fail;
_table[6][0] = success;
}
bool STokenizer::get_token(string &token) {
token.clear();
bool token_found = false;
string tmp(_buffer);
unsigned last_successfull_position = _pos;
unsigned current_pos = _pos;
int current_state = 0;
while(current_state != -1 && _buffer[current_pos] != '\0') {
current_state = _table[current_state][static_cast<int>(
_buffer[current_pos])];
if(current_state != -1) {
if(_buffer[current_pos] >= 0 && _table[current_state][0] != fail)
last_successfull_position = current_pos;
token = tmp.substr(_pos, last_successfull_position - _pos + 1);
token_found = true;
current_pos++;
if(_buffer[current_pos] < 0)
break;
}
if(_buffer[last_successfull_position] < 0)
current_state = -1;
}
_pos = ++last_successfull_position;
return token_found;
}