-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsat_parser.c
430 lines (363 loc) · 8.15 KB
/
sat_parser.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#include "sat.h"
#include "sat_parser.h"
#include "char_stream.h"
#include <stdio.h>
#include <string.h>
/* quantifiers */
#define AND "and"
#define OR "or"
#define MAX_QUANTIFIER_LEN 3
#define IS_SPACE(c) ((c) == ' ')
#define IS_TAB(c) ((c) == '\t')
#define IS_LPAREN(c) ((c) == '(')
#define IS_RPAREN(c) ((c) == ')')
#define IS_NEGATION(c) ((c) == '-')
#define IS_UNDERSCORE(c) ((c) == '_')
#define IS_ALPHA(c) (('a' <= (c) && (c) <= 'z') || ('A' <= (c) && (c) <= 'Z'))
#define IS_NUMERIC(c) (('0' <= (c) && (c) <= '9'))
#define IS_NEWLINE(c) ((c) == '\n')
#define IS_EOF(c) ((c) == EOF)
#define IS_ALPHANUMERIC(c) (IS_ALPHA(c) || IS_NUMERIC(c))
#define C_NORMAL "\033[00;00m"
#define C_HIGHLIGHT "\033[01;41m"
#define C_BLINK "\033[00;05m"
#define ARRAY_SZ(arr) (sizeof(arr) / sizeof((arr)[0]))
typedef enum parse_errno_t {
ERR_EARLY_EOF = 0,
ERR_LPAREN = 1,
ERR_NON_ADJACENT_NEGATION = 2,
ERR_VARIABLE_PREFIX = 3,
ERR_VARIABLE_ILLEGAL = 4,
ERR_NO_OR = 5,
ERR_OR_WS = 6,
ERR_NO_AND = 7,
ERR_AND_WS = 8,
ERR_RPAREN = 9,
ERR_RESERVED_WORDS = 10,
ERR_MEM_ALLOC = 11,
} parse_errno_t;
typedef struct parser_t {
parser_cb_t *cb;
cs_t *cs;
long error_pos;
int error_no;
} parser_t;
static parser_t parser;
static void st_clause_start(void *o);
static void st_literal_pre(void *o);
static int is_variable_prefix(char c)
{
return IS_UNDERSCORE(c) || IS_ALPHA(c);
}
static int is_variable_char(char c)
{
return IS_UNDERSCORE(c) || IS_ALPHANUMERIC(c);
}
static int is_whitespace(char c)
{
return IS_SPACE(c) || IS_TAB(c);
}
static int is_variable_delim(char c)
{
return IS_RPAREN(c) || is_whitespace(c);
}
static int is_reserved_word(char *s)
{
return !strcasecmp(s, OR) || !strcasecmp(s, AND);
}
static int is_predicate_end(char c)
{
return IS_NEWLINE(c) || IS_EOF(c);
}
static int eat_whitespace(cs_t *cs)
{
char c;
int eaten = 0;
while (is_whitespace(c = cs_getc(cs)))
eaten = 1;
cs_ungetc(cs);
return eaten;
}
static void clause_new(parser_cb_t *cb)
{
if (cb->clause_new)
cb->clause_new();
}
static int literal_new(parser_cb_t *cb, char *name, int val)
{
return cb->literal_new ? cb->literal_new(name, val) : 0;
}
static int is_quantifier(cs_t *cs, char *quantifier)
{
char qfr[MAX_QUANTIFIER_LEN + 1], *qptr;
int len;
sprintf(qfr, "%s", quantifier);
qptr = qfr;
len = strlen(qfr);
while (qptr < qfr + len && (cs_getc(cs)) == *qptr++);
return qptr == qfr + len;
}
static int is_quantifier_and(cs_t *cs)
{
return is_quantifier(cs, AND);
}
static int is_quantifier_or(cs_t *cs)
{
return is_quantifier(cs, OR);
}
static void st_error(void *o)
{
parser_t *p = (parser_t *)o;
cs_t *cs = p->cs;
char c, *err_msg, str[100] = {0};
switch(p->error_no)
{
case ERR_EARLY_EOF:
p->error_pos = -1;
snprintf(str, ARRAY_SZ(str), "%s%s%s%s", C_BLINK, C_HIGHLIGHT,
"_", C_NORMAL);
err_msg = "expression terminated prematurely";
break;
case ERR_LPAREN:
err_msg = "a clause must start with a '('";
break;
case ERR_NON_ADJACENT_NEGATION:
err_msg = "'-' must be adjacent to the variable";
break;
case ERR_VARIABLE_PREFIX:
err_msg = "variable prefix can be '_', ['a'-'z'] or ['A'-'Z']";
break;
case ERR_VARIABLE_ILLEGAL:
err_msg = "variables consist of alpha numeric characters only";
break;
case ERR_NO_OR:
err_msg = "literals in a clause must be separated by an \"or\"";
break;
case ERR_OR_WS:
err_msg = "literals are separated by an \"or\" surrounded by "
"whitespaces";
break;
case ERR_NO_AND:
err_msg = "clauses are separated by an \"and\"";
break;
case ERR_AND_WS:
err_msg = "clauses are separated by an \"and\" surrounded by "
"whitespaces";
break;
case ERR_RPAREN:
err_msg = "')' is either end of predicate or is followed by "
"an \"and\" surrounded by white spaces and followed by "
"another clause";
break;
case ERR_RESERVED_WORDS:
err_msg = "\"or\" and \"and\" are reserved words";
break;
case ERR_MEM_ALLOC:
err_msg = "could not allocate memory for new literal";
break;
default:
/* should never get here */
err_msg = "";
break;
}
cs_rewind(cs);
printf("parse error: %s\nCNF expression: ", err_msg);
while (!is_predicate_end(c = cs_getc(cs))) {
char *ch_prefix = cs_getpos(cs) - 1 == p->error_pos ?
C_HIGHLIGHT : C_NORMAL;
printf("%s%c%s", ch_prefix, c, C_NORMAL);
}
printf("%s\n", str);
event_add(p->cb->fail_cb, p->cb->fail_data);
}
static void st_clause_end(void *o)
{
parser_t *p = (parser_t *)o;
cs_t *cs = p->cs;
char c;
event_func_t next_cb;
void *next_data;
int eaten;
eaten = eat_whitespace(cs);
if (is_predicate_end(cs_getc(cs))) {
/* successful parse completion */
next_cb = p->cb->success_cb;
next_data = p->cb->success_data;
goto Exit;
}
cs_ungetc(cs);
next_data = o;
p->error_pos = cs_getpos(cs);
if (!eaten) {
p->error_no = ERR_RPAREN;
next_cb = st_error;
goto Exit;
}
if (!is_quantifier_and(cs)) {
p->error_no = ERR_NO_AND;
next_cb = st_error;
goto Exit;
}
if (!is_whitespace(c = cs_getc(cs))) {
p->error_pos = cs_getpos(cs) - 1;
if (is_predicate_end(c))
p->error_no = ERR_EARLY_EOF;
else if (IS_LPAREN(c))
p->error_no = ERR_AND_WS;
else
p->error_no = ERR_NO_AND;
next_cb = st_error;
goto Exit;
}
next_cb = st_clause_start;
Exit:
event_add(next_cb, next_data);
}
static void st_literal_post(void *o)
{
parser_t *p = (parser_t *)o;
cs_t *cs = p->cs;
char c;
event_func_t next_cb;
eat_whitespace(cs);
if (is_predicate_end(c = cs_getc(cs))) {
p->error_no = ERR_EARLY_EOF;
next_cb = st_error;
goto Exit;
}
if (IS_RPAREN(c)) {
next_cb = st_clause_end;
goto Exit;
}
cs_ungetc(cs);
p->error_pos = cs_getpos(cs);
if (!is_quantifier_or(cs)) {
p->error_no = ERR_NO_OR;
next_cb = st_error;
goto Exit;
}
if (!is_whitespace(c = cs_getc(cs))) {
p->error_pos = cs_getpos(cs) - 1;
p->error_no = is_predicate_end(c) ? ERR_EARLY_EOF : ERR_OR_WS;
next_cb = st_error;
goto Exit;
}
cs_ungetc(cs);
next_cb = st_literal_pre;
Exit:
event_add(next_cb, o);
}
static void st_literal(void *o)
{
parser_t *p = ((parser_t *)o);
cs_t *cs = p->cs;
char c, var[MAX_VARIABLE_SZ], *cptr;
event_func_t next_cb;
int val = -1;
p->error_pos = cs_getpos(cs);
cptr = var;
while (is_variable_char(c = cs_getc(cs)) || val == -1) {
if (cptr == var + sizeof(var) - 1) {
cs_setpos(cs, -1);
break;
}
if (val == -1 && !(val = IS_NEGATION(c) ? 0 : 1))
continue;
*cptr = c;
cptr++;
}
*cptr = 0;
cs_ungetc(cs);
if (is_predicate_end(c)) {
p->error_no = ERR_EARLY_EOF;
next_cb = st_error;
goto Exit;
}
if (!is_variable_delim(c)) {
p->error_pos = cs_getpos(cs);
p->error_no = ERR_VARIABLE_ILLEGAL;
next_cb = st_error;
goto Exit;
}
if (is_reserved_word(var)) {
p->error_no = ERR_RESERVED_WORDS;
next_cb = st_error;
goto Exit;
}
if (literal_new(p->cb, var, val)) {
p->error_no = ERR_MEM_ALLOC;
next_cb = st_error;
goto Exit;
}
next_cb = st_literal_post;
Exit:
event_add(next_cb, o);
}
static void st_literal_pre(void *o)
{
parser_t *p = (parser_t *)o;
cs_t *cs = p->cs;
event_func_t next_cb;
char c;
int val;
eat_whitespace(cs);
if (is_predicate_end(c = cs_getc(cs))) {
p->error_no = ERR_EARLY_EOF;
next_cb = st_error;
goto Exit;
}
p->error_pos = cs_getpos(cs) - 1;
if (!(val = IS_NEGATION(c) ? 0 : 1))
c = cs_getc(cs);
cs_ungetc(cs);
if (!is_variable_prefix(c)) {
if (is_predicate_end(c)) {
p->error_no = ERR_EARLY_EOF;
}
else if (is_whitespace(c)) {
p->error_no = ERR_NON_ADJACENT_NEGATION;
}
else {
p->error_no = ERR_VARIABLE_PREFIX;
if (!val)
p->error_pos++;
}
next_cb = st_error;
goto Exit;
}
if (!val)
cs_setpos(cs, -1);
next_cb = st_literal;
Exit:
event_add(next_cb, o);
}
static void st_clause_start(void *o)
{
parser_t *p = (parser_t *)o;
cs_t *cs = p->cs;
event_func_t next_cb;
char c;
clause_new(p->cb);
eat_whitespace(cs);
if (!IS_LPAREN(c = cs_getc(cs))) {
if (is_predicate_end(c)) {
p->error_no = ERR_EARLY_EOF;
}
else {
p->error_no = ERR_LPAREN;
p->error_pos = cs_getpos(cs) - 1;
}
next_cb = st_error;
goto Exit;
}
next_cb = st_literal_pre;
Exit:
event_add(next_cb, o);
}
void parser_init(parser_cb_t *cb, cs_t *cs)
{
parser.cb = cb;
parser.cs = cs;
parser.error_pos = EOF;
event_add(st_clause_start, &parser);
}