-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser_dfa.c
387 lines (347 loc) · 12.5 KB
/
parser_dfa.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include "parser_dfa.h"
#include <ctype.h>
#include <string.h>
#include <stdio.h>
static dfa_transition_t dfa_transitions[] = {
{STATE_START, A_ALPHA, STATE_ALPHA},
{STATE_START, A_DIGIT, STATE_DIGIT},
{STATE_START, A_RIGHT_PAREN, STATE_RIGHT_PAREN},
{STATE_START, A_LEFT_PAREN, STATE_LEFT_PAREN},
{STATE_START, A_COMMA, STATE_COMMA},
{STATE_START, A_AND, STATE_AND},
{STATE_START, A_OR, STATE_OR},
{STATE_START, A_PLUS, STATE_PLUS},
{STATE_START, A_MINUS, STATE_MINUS},
{STATE_START, A_CMP, STATE_CMP},
{STATE_START, A_LESS, STATE_LESS},
{STATE_START, A_MORE, STATE_MORE},
{STATE_START, A_EOL, STATE_END},
{STATE_START, A_SPACE, STATE_START},
{STATE_START, A_QUOTE, STATE_CAPTURE_STRING},
// From alpha
{STATE_ALPHA, A_ALPHA, STATE_ALPHA},
{STATE_ALPHA, A_DIGIT, STATE_ALPHA},
{STATE_ALPHA, A_RIGHT_PAREN, STATE_RIGHT_PAREN},
{STATE_ALPHA, A_LEFT_PAREN, STATE_LEFT_PAREN},
{STATE_ALPHA, A_COMMA, STATE_COMMA},
{STATE_ALPHA, A_AND, STATE_AND},
{STATE_ALPHA, A_OR, STATE_OR},
{STATE_ALPHA, A_PLUS, STATE_PLUS},
{STATE_ALPHA, A_MINUS, STATE_MINUS},
{STATE_ALPHA, A_CMP, STATE_CMP},
{STATE_ALPHA, A_LESS, STATE_LESS},
{STATE_ALPHA, A_MORE, STATE_MORE},
{STATE_ALPHA, A_DOT, STATE_ALPHA},
{STATE_ALPHA, A_EOL, STATE_END},
{STATE_ALPHA, A_SPACE, STATE_ALPHA},
// From digit
{STATE_DIGIT, A_DIGIT, STATE_DIGIT},
{STATE_DIGIT, A_RIGHT_PAREN, STATE_RIGHT_PAREN},
{STATE_DIGIT, A_COMMA, STATE_COMMA},
{STATE_DIGIT, A_AND, STATE_AND},
{STATE_DIGIT, A_OR, STATE_OR},
{STATE_DIGIT, A_PLUS, STATE_PLUS},
{STATE_DIGIT, A_MINUS, STATE_MINUS},
{STATE_DIGIT, A_CMP, STATE_CMP},
{STATE_DIGIT, A_LESS, STATE_LESS},
{STATE_DIGIT, A_MORE, STATE_MORE},
{STATE_DIGIT, A_DOT, STATE_DIGIT},
{STATE_DIGIT, A_ALPHA, STATE_ALPHA},
{STATE_DIGIT, A_EOL, STATE_END},
{STATE_DIGIT, A_SPACE, STATE_START},
// From left parenthesis
{STATE_LEFT_PAREN, A_ALPHA, STATE_ALPHA},
{STATE_LEFT_PAREN, A_DIGIT, STATE_DIGIT},
{STATE_LEFT_PAREN, A_RIGHT_PAREN, STATE_RIGHT_PAREN},
{STATE_LEFT_PAREN, A_LEFT_PAREN, STATE_LEFT_PAREN},
{STATE_LEFT_PAREN, A_SPACE, STATE_START},
{STATE_LEFT_PAREN, A_MINUS, STATE_UNARY_MINUS},
{STATE_LEFT_PAREN, A_QUOTE, STATE_CAPTURE_STRING},
// From right parenthesis
{STATE_RIGHT_PAREN, A_RIGHT_PAREN, STATE_RIGHT_PAREN},
{STATE_RIGHT_PAREN, A_COMMA, STATE_COMMA},
{STATE_RIGHT_PAREN, A_AND, STATE_AND},
{STATE_RIGHT_PAREN, A_OR, STATE_OR},
{STATE_RIGHT_PAREN, A_PLUS, STATE_PLUS},
{STATE_RIGHT_PAREN, A_MINUS, STATE_MINUS},
{STATE_RIGHT_PAREN, A_CMP, STATE_CMP},
{STATE_RIGHT_PAREN, A_LESS, STATE_LESS},
{STATE_RIGHT_PAREN, A_MORE, STATE_MORE},
{STATE_RIGHT_PAREN, A_EOL, STATE_END},
{STATE_RIGHT_PAREN, A_SPACE, STATE_START},
// From comma
{STATE_COMMA, A_ALPHA, STATE_ALPHA},
{STATE_COMMA, A_DIGIT, STATE_DIGIT},
{STATE_COMMA, A_LEFT_PAREN, STATE_LEFT_PAREN},
{STATE_COMMA, A_QUOTE, STATE_CAPTURE_STRING},
{STATE_COMMA, A_SPACE, STATE_START},
// From AND
{STATE_AND, A_ALPHA, STATE_ALPHA},
{STATE_AND, A_DIGIT, STATE_DIGIT},
{STATE_AND, A_LEFT_PAREN, STATE_LEFT_PAREN},
{STATE_AND, A_SPACE, STATE_START},
// From OR
{STATE_OR, A_ALPHA, STATE_ALPHA},
{STATE_OR, A_DIGIT, STATE_DIGIT},
{STATE_OR, A_LEFT_PAREN, STATE_LEFT_PAREN},
{STATE_OR, A_SPACE, STATE_START},
// From PLUS
{STATE_PLUS, A_ALPHA, STATE_ALPHA},
{STATE_PLUS, A_DIGIT, STATE_DIGIT},
{STATE_PLUS, A_LEFT_PAREN, STATE_LEFT_PAREN},
{STATE_PLUS, A_SPACE, STATE_START},
// From MINUS
{STATE_MINUS, A_ALPHA, STATE_ALPHA},
{STATE_MINUS, A_DIGIT, STATE_DIGIT},
{STATE_MINUS, A_LEFT_PAREN, STATE_LEFT_PAREN},
{STATE_MINUS, A_SPACE, STATE_START},
// From MINUS
{STATE_UNARY_MINUS, A_DIGIT, STATE_DIGIT},
{STATE_UNARY_MINUS, A_LEFT_PAREN, STATE_LEFT_PAREN},
// From CMP
{STATE_CMP, A_ALPHA, STATE_ALPHA},
{STATE_CMP, A_DIGIT, STATE_DIGIT},
{STATE_CMP, A_LEFT_PAREN, STATE_LEFT_PAREN},
{STATE_CMP, A_SPACE, STATE_START},
// From LESS
{STATE_LESS, A_ALPHA, STATE_ALPHA},
{STATE_LESS, A_DIGIT, STATE_DIGIT},
{STATE_LESS, A_LEFT_PAREN, STATE_LEFT_PAREN},
{STATE_LESS, A_SPACE, STATE_START},
// From MORE
{STATE_MORE, A_ALPHA, STATE_ALPHA},
{STATE_MORE, A_DIGIT, STATE_DIGIT},
{STATE_MORE, A_LEFT_PAREN, STATE_LEFT_PAREN},
{STATE_MORE, A_SPACE, STATE_START},
// Quote
{STATE_CAPTURE_STRING, A_ALPHA, STATE_CAPTURE_STRING},
{STATE_CAPTURE_STRING, A_DIGIT, STATE_CAPTURE_STRING},
{STATE_CAPTURE_STRING, A_RIGHT_PAREN, STATE_CAPTURE_STRING},
{STATE_CAPTURE_STRING, A_LEFT_PAREN, STATE_CAPTURE_STRING},
{STATE_CAPTURE_STRING, A_COMMA, STATE_CAPTURE_STRING},
{STATE_CAPTURE_STRING, A_AND, STATE_CAPTURE_STRING},
{STATE_CAPTURE_STRING, A_OR, STATE_CAPTURE_STRING},
{STATE_CAPTURE_STRING, A_PLUS, STATE_CAPTURE_STRING},
{STATE_CAPTURE_STRING, A_MINUS, STATE_CAPTURE_STRING},
{STATE_CAPTURE_STRING, A_CMP, STATE_CAPTURE_STRING},
{STATE_CAPTURE_STRING, A_LESS, STATE_CAPTURE_STRING},
{STATE_CAPTURE_STRING, A_MORE, STATE_CAPTURE_STRING},
{STATE_CAPTURE_STRING, A_DOT, STATE_CAPTURE_STRING},
{STATE_CAPTURE_STRING, A_EOL, STATE_CAPTURE_STRING},
{STATE_CAPTURE_STRING, A_SPACE, STATE_CAPTURE_STRING},
{STATE_CAPTURE_STRING, A_QUOTE, STATE_START},
{STATE_ERROR, A_ALPHA, STATE_END},
{STATE_ERROR, A_DIGIT, STATE_END},
{STATE_ERROR, A_RIGHT_PAREN, STATE_END},
{STATE_ERROR, A_LEFT_PAREN, STATE_END},
{STATE_ERROR, A_COMMA, STATE_END},
{STATE_ERROR, A_AND, STATE_END},
{STATE_ERROR, A_OR, STATE_END},
{STATE_ERROR, A_PLUS, STATE_END},
{STATE_ERROR, A_MINUS, STATE_END},
{STATE_ERROR, A_CMP, STATE_END},
{STATE_ERROR, A_LESS, STATE_END},
{STATE_ERROR, A_MORE, STATE_END},
{STATE_ERROR, A_DOT, STATE_END},
{STATE_ERROR, A_EOL, STATE_END},
{STATE_ERROR, A_SPACE, STATE_END},
{STATE_ERROR, A_QUOTE, STATE_END},
};
State parser_dfa_next_state(State source_state, AlphabetToken token)
{
int i;
State target_state = STATE_INVALID;
if(A_INVALID == token)
{
return target_state;
}
for(i = 0; i < sizeof(dfa_transitions) / sizeof(dfa_transition_t) && target_state == STATE_INVALID; i++ )
{
if(dfa_transitions[i].source_state == source_state && dfa_transitions[i].token == token)
{
target_state = dfa_transitions[i].target_state;
}
}
return target_state;
}
AlphabetToken parser_dfa_get_token(const char* str, State source_state)
{
if(isalpha(*str) || *str == '$' || *str == ':' || *str == '"' || *str == '[' || *str == ']' || *str == '_')
{
return A_ALPHA;
}
else if(*str == '.')
{
return A_DOT;
}
else if(isdigit(*str))
{
return A_DIGIT;
}
else if(isspace(*str))
{
return A_SPACE;
}
else if(*str == ')')
{
return A_RIGHT_PAREN;
}
else if(*str == '(')
{
return A_LEFT_PAREN;
}
else if(*str == ',')
{
return A_COMMA;
}
else if(*str == '\0' || *str == '\n' || *str == '\r')
{
return A_EOL;
}
else if(*str == '<')
{
return A_LESS;
}
else if(*str == '>')
{
return A_MORE;
}
else if(*str == '\'')
{
return A_QUOTE;
}
else if(*str == '+')
{
return A_PLUS;
}
else if(*str == '-')
{
return A_MINUS;
}
else if(strlen(str) == 2)
{
if(strcmp(str, "&&") == 0)
{
return A_AND;
}
else if(strcmp(str, "||") == 0)
{
return A_OR;
}
else if(strcmp(str, "==") == 0)
{
return A_CMP;
}
else if(strcmp(str, "\\\'") == 0)
{
return A_ALPHA;
}
}
else if(*str == '\\')
{
return A_INVALID;
}
if(source_state == STATE_CAPTURE_STRING)
{
return A_ALPHA;
}
return A_INVALID;
}
AlphabetToken parser_dfa_read_next_token(state_context_t* state_context)
{
//state_context->pos++;
if(NULL == state_context->expression)
{
return A_INVALID;
}
char ch = state_context->expression[state_context->pos++];
if(state_context->token_parsing_state == PS_NONE)
{
memset(state_context->parsed_token, 0, MAX_TOKEN_LEN);
state_context->token_parsing_state = PS_STARTED;
state_context->parse_cycle = 0;
state_context->parsed_token[state_context->parse_cycle++] = ch;
}
else if(state_context->token_parsing_state == PS_STARTED)
{
state_context->parsed_token[state_context->parse_cycle++] = ch;
}
else if(state_context->token_parsing_state == PS_ERROR)
{
return A_INVALID;
}
AlphabetToken token = parser_dfa_get_token(state_context->parsed_token, state_context->current_state);
if(A_INVALID == token)
{
if(state_context->parse_cycle <= MAX_TOKEN_LEN)
{
return parser_dfa_read_next_token(state_context);
}
else
{
state_context->token_parsing_state = PS_ERROR;
state_context->current_token = A_INVALID;
}
}
else
{
state_context->token_parsing_state = PS_COMPLETED;
state_context->current_token = token;
/*if(token == A_ALPHA || token == A_DIGIT || token == A_DOT)
{
strncat(state_context->current_data_buf, state_context->parsed_token, state_context->parse_cycle);
}*/
//*state_context->parsed_token = '\0';
}
return state_context->current_token;
}
state_descriptor_t* parser_dfa_find_descriptor(State state, state_descriptor_t state_map[], int map_size)
{
state_descriptor_t* descr = NULL;
int i;
// Find the state
for(i = 0; i < map_size && descr == NULL; i++)
{
if(state_map[i].state == state)
{
descr = &state_map[i];
}
}
return descr;
}
bool parser_dfa_run(State start_state, State end_state, State error_state, state_descriptor_t state_map[], int map_size, const char* expression, void* priv)
{
int i;
State current_state = start_state;
bool retVal;
state_descriptor_t* state_descriptor = parser_dfa_find_descriptor(start_state, state_map, map_size);
state_descriptor_t* end_descriptor = parser_dfa_find_descriptor(end_state, state_map, map_size);
state_descriptor_t* error_descriptor = parser_dfa_find_descriptor(error_state, state_map, map_size);
state_context_t state_context = {0};
state_context.expression = expression;
state_context.current_state = start_state;
while(current_state != end_state && current_state != error_state)
{
current_state = state_descriptor->state_callback(&state_context, priv);
state_context.token_parsing_state = PS_NONE;
state_descriptor = parser_dfa_find_descriptor(current_state, state_map, map_size);
if(NULL == state_descriptor)
{
state_descriptor = error_descriptor;
}
}
switch(current_state)
{
case STATE_ERROR:
error_descriptor->state_callback(&state_context, priv);
retVal = false;
break;
case STATE_END:
{
State final_state = end_descriptor->state_callback(&state_context, priv);
retVal = (final_state != STATE_ERROR);
}
break;
}
return retVal;
}