-
Notifications
You must be signed in to change notification settings - Fork 0
/
tl_parse.c
410 lines (377 loc) · 8.02 KB
/
tl_parse.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
/***** tl_spin: tl_parse.c *****/
/*
* This file is part of the public release of Spin. It is subject to the
* terms in the LICENSE file that is included in this source directory.
* Tool documentation is available at http://spinroot.com
*
* Based on the translation algorithm by Gerth, Peled, Vardi, and Wolper,
* presented at the PSTV Conference, held in 1995, Warsaw, Poland 1995.
*/
#include "tl.h"
extern int tl_yylex(void);
extern int tl_verbose, tl_errs;
int tl_yychar = 0;
YYSTYPE tl_yylval;
static Node *tl_formula(void);
static Node *tl_factor(void);
static Node *tl_level(int);
static int prec[2][4] = {
{ U_OPER, V_OPER, 0, 0 }, /* left associative */
{ OR, AND, IMPLIES, EQUIV, }, /* left associative */
};
static Node *
tl_factor(void)
{ Node *ptr = ZN;
switch (tl_yychar) {
case '(':
ptr = tl_formula();
if (tl_yychar != ')')
tl_yyerror("expected ')'");
tl_yychar = tl_yylex();
break;
case NOT:
ptr = tl_yylval;
tl_yychar = tl_yylex();
ptr->lft = tl_factor();
if (!ptr->lft)
{ fatal("malformed expression", (char *) 0);
}
ptr = push_negation(ptr);
break;
case ALWAYS:
tl_yychar = tl_yylex();
ptr = tl_factor();
#ifndef NO_OPT
if (ptr->ntyp == FALSE
|| ptr->ntyp == TRUE)
break; /* [] false == false */
if (ptr->ntyp == V_OPER)
{ if (ptr->lft->ntyp == FALSE)
break; /* [][]p = []p */
ptr = ptr->rgt; /* [] (p V q) = [] q */
}
#endif
ptr = tl_nn(V_OPER, False, ptr);
break;
#ifdef NXT
case NEXT:
tl_yychar = tl_yylex();
ptr = tl_factor();
if (ptr->ntyp == TRUE)
break; /* X true = true */
ptr = tl_nn(NEXT, ptr, ZN);
break;
#endif
case CEXPR:
tl_yychar = tl_yylex();
ptr = tl_factor();
if (ptr->ntyp != PREDICATE)
{ tl_yyerror("expected {...} after c_expr");
}
ptr = tl_nn(CEXPR, ptr, ZN);
break;
case EVENTUALLY:
tl_yychar = tl_yylex();
ptr = tl_factor();
#ifndef NO_OPT
if (ptr->ntyp == TRUE
|| ptr->ntyp == FALSE)
break; /* <> true == true */
if (ptr->ntyp == U_OPER
&& ptr->lft->ntyp == TRUE)
break; /* <><>p = <>p */
if (ptr->ntyp == U_OPER)
{ /* <> (p U q) = <> q */
ptr = ptr->rgt;
/* fall thru */
}
#endif
ptr = tl_nn(U_OPER, True, ptr);
break;
case PREDICATE:
ptr = tl_yylval;
tl_yychar = tl_yylex();
break;
case TRUE:
case FALSE:
ptr = tl_yylval;
tl_yychar = tl_yylex();
break;
}
if (!ptr) tl_yyerror("expected predicate");
#if 0
printf("factor: ");
tl_explain(ptr->ntyp);
printf("\n");
#endif
return ptr;
}
static Node *
bin_simpler(Node *ptr)
{ Node *a, *b;
if (ptr)
switch (ptr->ntyp) {
case U_OPER:
#ifndef NO_OPT
if (ptr->rgt->ntyp == TRUE
|| ptr->rgt->ntyp == FALSE
|| ptr->lft->ntyp == FALSE)
{ ptr = ptr->rgt;
break;
}
if (isequal(ptr->lft, ptr->rgt))
{ /* p U p = p */
ptr = ptr->rgt;
break;
}
if (ptr->lft->ntyp == U_OPER
&& isequal(ptr->lft->lft, ptr->rgt))
{ /* (p U q) U p = (q U p) */
ptr->lft = ptr->lft->rgt;
break;
}
if (ptr->rgt->ntyp == U_OPER
&& ptr->rgt->lft->ntyp == TRUE)
{ /* p U (T U q) = (T U q) */
ptr = ptr->rgt;
break;
}
#ifdef NXT
/* X p U X q == X (p U q) */
if (ptr->rgt->ntyp == NEXT
&& ptr->lft->ntyp == NEXT)
{ ptr = tl_nn(NEXT,
tl_nn(U_OPER,
ptr->lft->lft,
ptr->rgt->lft), ZN);
}
#endif
#endif
break;
case V_OPER:
#ifndef NO_OPT
if (ptr->rgt->ntyp == FALSE
|| ptr->rgt->ntyp == TRUE
|| ptr->lft->ntyp == TRUE)
{ ptr = ptr->rgt;
break;
}
if (isequal(ptr->lft, ptr->rgt))
{ /* p V p = p */
ptr = ptr->rgt;
break;
}
/* F V (p V q) == F V q */
if (ptr->lft->ntyp == FALSE
&& ptr->rgt->ntyp == V_OPER)
{ ptr->rgt = ptr->rgt->rgt;
break;
}
/* p V (F V q) == F V q */
if (ptr->rgt->ntyp == V_OPER
&& ptr->rgt->lft->ntyp == FALSE)
{ ptr->lft = False;
ptr->rgt = ptr->rgt->rgt;
break;
}
#endif
break;
case IMPLIES:
#ifndef NO_OPT
if (isequal(ptr->lft, ptr->rgt))
{ ptr = True;
break;
}
#endif
ptr = tl_nn(OR, Not(ptr->lft), ptr->rgt);
ptr = rewrite(ptr);
break;
case EQUIV:
#ifndef NO_OPT
if (isequal(ptr->lft, ptr->rgt))
{ ptr = True;
break;
}
#endif
a = rewrite(tl_nn(AND,
dupnode(ptr->lft),
dupnode(ptr->rgt)));
b = rewrite(tl_nn(AND,
Not(ptr->lft),
Not(ptr->rgt)));
ptr = tl_nn(OR, a, b);
ptr = rewrite(ptr);
break;
case AND:
#ifndef NO_OPT
/* p && (q U p) = p */
if (ptr->rgt->ntyp == U_OPER
&& isequal(ptr->rgt->rgt, ptr->lft))
{ ptr = ptr->lft;
break;
}
if (ptr->lft->ntyp == U_OPER
&& isequal(ptr->lft->rgt, ptr->rgt))
{ ptr = ptr->rgt;
break;
}
/* p && (q V p) == q V p */
if (ptr->rgt->ntyp == V_OPER
&& isequal(ptr->rgt->rgt, ptr->lft))
{ ptr = ptr->rgt;
break;
}
if (ptr->lft->ntyp == V_OPER
&& isequal(ptr->lft->rgt, ptr->rgt))
{ ptr = ptr->lft;
break;
}
/* (p U q) && (r U q) = (p && r) U q*/
if (ptr->rgt->ntyp == U_OPER
&& ptr->lft->ntyp == U_OPER
&& isequal(ptr->rgt->rgt, ptr->lft->rgt))
{ ptr = tl_nn(U_OPER,
tl_nn(AND, ptr->lft->lft, ptr->rgt->lft),
ptr->lft->rgt);
break;
}
/* (p V q) && (p V r) = p V (q && r) */
if (ptr->rgt->ntyp == V_OPER
&& ptr->lft->ntyp == V_OPER
&& isequal(ptr->rgt->lft, ptr->lft->lft))
{ ptr = tl_nn(V_OPER,
ptr->rgt->lft,
tl_nn(AND, ptr->lft->rgt, ptr->rgt->rgt));
break;
}
#ifdef NXT
/* X p && X q == X (p && q) */
if (ptr->rgt->ntyp == NEXT
&& ptr->lft->ntyp == NEXT)
{ ptr = tl_nn(NEXT,
tl_nn(AND,
ptr->rgt->lft,
ptr->lft->lft), ZN);
break;
}
#endif
if (isequal(ptr->lft, ptr->rgt) /* (p && p) == p */
|| ptr->rgt->ntyp == FALSE /* (p && F) == F */
|| ptr->lft->ntyp == TRUE) /* (T && p) == p */
{ ptr = ptr->rgt;
break;
}
if (ptr->rgt->ntyp == TRUE /* (p && T) == p */
|| ptr->lft->ntyp == FALSE) /* (F && p) == F */
{ ptr = ptr->lft;
break;
}
/* (p V q) && (r U q) == p V q */
if (ptr->rgt->ntyp == U_OPER
&& ptr->lft->ntyp == V_OPER
&& isequal(ptr->lft->rgt, ptr->rgt->rgt))
{ ptr = ptr->lft;
break;
}
#endif
break;
case OR:
#ifndef NO_OPT
/* p || (q U p) == q U p */
if (ptr->rgt->ntyp == U_OPER
&& isequal(ptr->rgt->rgt, ptr->lft))
{ ptr = ptr->rgt;
break;
}
/* p || (q V p) == p */
if (ptr->rgt->ntyp == V_OPER
&& isequal(ptr->rgt->rgt, ptr->lft))
{ ptr = ptr->lft;
break;
}
/* (p U q) || (p U r) = p U (q || r) */
if (ptr->rgt->ntyp == U_OPER
&& ptr->lft->ntyp == U_OPER
&& isequal(ptr->rgt->lft, ptr->lft->lft))
{ ptr = tl_nn(U_OPER,
ptr->rgt->lft,
tl_nn(OR, ptr->lft->rgt, ptr->rgt->rgt));
break;
}
if (isequal(ptr->lft, ptr->rgt) /* (p || p) == p */
|| ptr->rgt->ntyp == FALSE /* (p || F) == p */
|| ptr->lft->ntyp == TRUE) /* (T || p) == T */
{ ptr = ptr->lft;
break;
}
if (ptr->rgt->ntyp == TRUE /* (p || T) == T */
|| ptr->lft->ntyp == FALSE) /* (F || p) == p */
{ ptr = ptr->rgt;
break;
}
/* (p V q) || (r V q) = (p || r) V q */
if (ptr->rgt->ntyp == V_OPER
&& ptr->lft->ntyp == V_OPER
&& isequal(ptr->lft->rgt, ptr->rgt->rgt))
{ ptr = tl_nn(V_OPER,
tl_nn(OR, ptr->lft->lft, ptr->rgt->lft),
ptr->rgt->rgt);
break;
}
/* (p V q) || (r U q) == r U q */
if (ptr->rgt->ntyp == U_OPER
&& ptr->lft->ntyp == V_OPER
&& isequal(ptr->lft->rgt, ptr->rgt->rgt))
{ ptr = ptr->rgt;
break;
}
#endif
break;
}
return ptr;
}
static Node *
tl_level(int nr)
{ int i; Node *ptr = ZN;
if (nr < 0)
return tl_factor();
ptr = tl_level(nr-1);
again:
for (i = 0; i < 4; i++)
if (tl_yychar == prec[nr][i])
{ tl_yychar = tl_yylex();
ptr = tl_nn(prec[nr][i],
ptr, tl_level(nr-1));
ptr = bin_simpler(ptr);
goto again;
}
if (!ptr) tl_yyerror("syntax error");
#if 0
printf("level %d: ", nr);
tl_explain(ptr->ntyp);
printf("\n");
#endif
return ptr;
}
static Node *
tl_formula(void)
{ tl_yychar = tl_yylex();
return tl_level(1); /* 2 precedence levels, 1 and 0 */
}
void
tl_parse(void)
{ Node *n;
/* tl_verbose = 1; */
n = tl_formula();
if (tl_verbose)
{ printf("formula: ");
dump(n);
printf("\n");
}
if (tl_Getchar() != -1)
{ tl_yyerror("syntax error");
tl_errs++;
return;
}
trans(n);
}