/
parse.ym
624 lines (532 loc) · 15.6 KB
/
parse.ym
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
%{
/****************************************************************************/
/* parse.ym */
/* */
/* Author: Richard Dale */
/* Date: 28/01/93 */
/* */
/* Copyright (c) 1993-2009 Richard Dale. */
/****************************************************************************/
#include <sys/types.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <limits.h>
#import "prolog.h"
#import "Prolog.h"
#import "Term.h"
#import "ListTerm.h"
#import "FunctionTerm.h"
#import "VariableTerm.h"
#import "NamedVariable.h"
#import "NumericTerm.h"
#import "Structure.h"
#import "PrefixExpression.h"
#import "InfixExpression.h"
#import "PostfixExpression.h"
#import "Clause.h"
#import "NestedStructureIterator.h"
// yydebug = 1;
#define YYDEBUG 1
#undef YYDEBUG
#ifdef YYDEBUG
#define TRACE_RULE(RULE, TERM) [[prolog consoleOut] printWithFormat: @"%@ -> ", RULE]; \
[TERM printForDebugger: [prolog consoleOut]]; \
[[prolog consoleOut] printWithFormat: @"\n"];
#else
#define TRACE_RULE(RULE, TERM)
#endif
extern void ResetLex(void);
extern void yyerror(char * fmt, ...);
extern int yywrap(void);
id CurrentTerm(void);
static id addNamedVariable(char * name);
static id currentTerm = nil;
static id variableTable = nil;
static void clearLookaheadToken(void);
%}
%union {
int intValue;
char * stringValue;
id idValue;
}
%token
T_ANONYMOUS_VARIABLE
T_APPLY_PAIR_CONSTRUCTOR
T_BEGIN_TUPLE
T_COMMA
T_CURLY_LPAREN
T_CURLY_RPAREN
T_DIV_OP
T_END_TUPLE
T_EQ_OP
T_EVAL_ATOM_LIST
T_EXPRESSION_RULE_OP
T_FLOATING_POINT_LITERAL
T_FN
T_FIND_FIRST_SOLUTION
T_GE_OP
T_GT_OP
T_HEAD_TAIL_SEPARATOR
T_IDENTIFIER
T_IF_OP
T_IMPLY_OP
T_INFIX_OP
T_IS_OP
T_LE_OP
T_LOGICAL_AND_OP
T_LOGICAL_COMPLEMENT_OP
T_LOGICAL_OR_OP
T_LPAREN
T_LT_OP
T_MINUS_OP
T_MOD_OP
T_MULT_OP
T_NE_OP
T_NOT_OP
T_NUMERIC_LIT
T_DOT
T_PLUS_OP
T_QUERY_OP
T_QUOTIENT_OP
T_RPAREN
T_OR_OP
T_SHIFT_LEFT_OP
T_SHIFT_RIGHT_OP
T_SQUARE_LPAREN
T_SQUARE_RPAREN
T_STANDARD_LPAREN
T_STANDARD_RPAREN
T_STRICT_EQ_OP
T_STRICT_NE_OP
T_STRING_LIT
T_UNIV_OP
T_VARIABLE
%left T_IF_OP
%left T_QUERY_OP
%left T_OR_OP
%left T_IMPLY_OP
%left T_COMMA
%left T_PLUS_OP T_MINUS_OP
%left T_MULT_OP T_DIV_OP
%token <intValue>
T_NUMERIC_LIT
%token <stringValue>
T_DOT
T_IDENTIFIER
T_NOT_OP
T_STRING_LIT
T_VARIABLE
T_EQ_OP
T_NE_OP
T_LT_OP
T_GT_OP
T_LE_OP
T_GE_OP
T_STRICT_EQ_OP
T_STRICT_NE_OP
T_UNIV_OP
T_OR_OP
T_IMPLY_OP
T_INFIX_OP
T_IS_OP
T_PLUS_OP
T_MINUS_OP
T_DIV_OP
T_MULT_OP
T_MOD_OP
T_IF_OP
T_QUERY_OP
T_COMMA
%type <intValue>
numeric_literal
%type <stringValue>
infix_op
%type <idValue>
clause
clause_term
or_expression
goal_sequence
goal
arg_sequence
variable
structure
list_sequence
list
list_pair
list_end
string_literal
%start prolog_start
%%
prolog_start
: /* empty */
{
currentTerm = nil;
}
| clause_start clause T_DOT
{
currentTerm = $2;
[currentTerm setVariableTable: variableTable];
clearLookaheadToken();
TRACE_RULE(@"clause_start clause T_DOT", currentTerm);
}
| clause_start or_expression T_DOT
{
currentTerm = [[Clause alloc] initList: $2];
[currentTerm setVariableTable: variableTable];
clearLookaheadToken();
TRACE_RULE(@"clause_start or_expression T_DOT", currentTerm);
}
;
clause_start
: /* empty */
{
variableTable = [[NSMutableDictionary alloc] init];
}
;
clause
: T_IF_OP or_expression
{
id listTerm;
listTerm = [[ListTerm alloc] initTerm: nil tail: $2];
$$ = [[Clause alloc] initTerm: [[FunctionTerm alloc] initFunction: [NSString stringWithUTF8String: $1]] tail: listTerm];
TRACE_RULE(@"T_IF_OP goal_sequence", $$);
}
| goal T_IF_OP or_expression
{
[$1 setTail: $3];
$$ = [[Clause alloc] initTerm: [[FunctionTerm alloc] initFunction: [NSString stringWithUTF8String: $2]] tail: $1];
TRACE_RULE(@"goal T_IF_OP goal_sequence", $$);
}
| T_QUERY_OP or_expression
{
id listTerm;
listTerm = [[ListTerm alloc] initTerm: nil tail: $2];
$$ = [[Clause alloc] initTerm: [[FunctionTerm alloc] initFunction: [NSString stringWithUTF8String: $1]] tail: listTerm];
TRACE_RULE(@"T_QUERY_OP goal_sequence", $$);
}
| goal T_EXPRESSION_RULE_OP or_expression
;
clause_term
: clause
{
$$ = [[ListTerm alloc] initTerm: $1 tail: nil];
}
| or_expression
{
$$ = $1;
}
;
or_expression
: goal_sequence T_OR_OP or_expression
{
id infixExpression;
[$1 setTail: $3];
infixExpression = [[InfixExpression alloc] initHead: [NSString stringWithUTF8String: $2] tail: $1];
$$ = [[ListTerm alloc] initTerm: infixExpression tail: nil];
TRACE_RULE(@"goal_sequence T_OR_OP or_expression", $$);
}
| goal_sequence
;
goal_sequence
: goal
{
TRACE_RULE(@"goal", $$);
}
| goal T_COMMA goal_sequence
{
id infixExpression;
[$1 setTail: $3];
infixExpression = [[InfixExpression alloc] initHead: [NSString stringWithUTF8String: $2] tail: $1];
$$ = [[ListTerm alloc] initTerm: infixExpression tail: nil];
TRACE_RULE(@"goal T_COMMA goal_sequence", $$);
}
| goal_sequence T_IMPLY_OP goal_sequence T_OR_OP goal_sequence
{
id ifExpression;
id orExpression;
id listTerm;
[$3 setTail: $5];
orExpression = [[InfixExpression alloc] initHead: [NSString stringWithUTF8String: $4] tail: $3];
listTerm = [[ListTerm alloc] initTerm: orExpression tail: nil];
[$1 setTail: listTerm];
ifExpression = [[InfixExpression alloc] initHead: [NSString stringWithUTF8String: $2] tail: $1];
$$ = [[ListTerm alloc] initTerm: ifExpression tail: nil];
TRACE_RULE(@"goal_sequence T_IMPLY_OP goal_sequence", $$);
}
| goal_sequence T_IMPLY_OP goal_sequence
{
id ifExpression;
[$1 setTail: $3];
ifExpression = [[InfixExpression alloc] initHead: [NSString stringWithUTF8String: $2] tail: $1];
$$ = [[ListTerm alloc] initTerm: ifExpression tail: nil];
TRACE_RULE(@"goal_sequence T_IMPLY_OP goal_sequence", $$);
}
;
arg_sequence
: goal
{
TRACE_RULE(@"goal", $$);
}
| goal T_COMMA arg_sequence
{
[$1 setTail: $3];
TRACE_RULE(@"goal T_COMMA arg_sequence", $$);
}
;
goal
: T_LPAREN clause_term T_RPAREN
{
$$ = $2;
TRACE_RULE(@"T_LPAREN clause_term T_RPAREN", $$);
}
| T_IDENTIFIER structure
{
id structure;
structure = [[Structure alloc] initHead: [NSString stringWithUTF8String: $1] tail: $2];
$$ = [[ListTerm alloc] initTerm: structure tail: nil];
TRACE_RULE(@"T_IDENTIFIER structure", $$);
}
| variable structure
{
id structure;
structure = [[Structure alloc] initTerm: $1 tail: $2];
$$ = [[ListTerm alloc] initTerm: structure tail: nil];
TRACE_RULE(@"variable structure", $$);
}
| goal infix_op goal
{
id infixExpression;
[$1 setTail: $3];
infixExpression = [[InfixExpression alloc] initHead: [NSString stringWithUTF8String: $2] tail: $1];
$$ = [[ListTerm alloc] initTerm: infixExpression tail: nil];
TRACE_RULE(@"goal infix_op goal", $$);
}
| T_NOT_OP goal
{
id prefixExpression;
prefixExpression = [[PrefixExpression alloc] initHead: [NSString stringWithUTF8String: $1] tail: $2];
$$ = [[ListTerm alloc] initTerm: prefixExpression tail: nil];
TRACE_RULE(@"T_NOT_OP goal", $$);
}
| T_IDENTIFIER
{
$$ = [[ListTerm alloc] initTerm: [[FunctionTerm alloc] initFunction: [NSString stringWithUTF8String: $1]] tail: nil];
TRACE_RULE(@"T_IDENTIFIER", $$);
}
| list
{
TRACE_RULE(@"list", $$);
}
| numeric_literal
{
$$ = [[ListTerm alloc] initTerm: [[NumericTerm alloc] initNumeric: $1] tail: nil];
TRACE_RULE(@"numeric_literal", $$);
}
| string_literal
{
$$ = [[ListTerm alloc] initTerm: $1 tail: nil];
TRACE_RULE(@"string_literal", $$);
}
| variable
{
$$ = [[ListTerm alloc] initTerm: $1 tail: nil];
TRACE_RULE(@"variable", $$);
}
;
infix_op
: T_EQ_OP
| T_NE_OP
| T_LT_OP
| T_GT_OP
| T_LE_OP
| T_GE_OP
| T_STRICT_EQ_OP
| T_STRICT_NE_OP
| T_UNIV_OP
| T_INFIX_OP
| T_PLUS_OP
| T_MINUS_OP
| T_DIV_OP
| T_MULT_OP
| T_MOD_OP
| T_IS_OP
;
variable
: T_VARIABLE
{
$$ = addNamedVariable($1);
TRACE_RULE(@"T_VARIABLE", $$);
}
| T_ANONYMOUS_VARIABLE
{
$$ = [[[VariableTerm alloc] init] retain];
TRACE_RULE(@"T_ANONYMOUS_VARIABLE", $$);
}
;
numeric_literal
: T_NUMERIC_LIT
| T_MINUS_OP T_NUMERIC_LIT
{
$$ = ($2 * -1);
}
| T_PLUS_OP T_NUMERIC_LIT
{
$$ = $2;
}
;
string_literal
: T_STRING_LIT
{
int index;
id listTerm;
for ( index = strlen($1) - 1, listTerm = nil;
index >= 0;
index-- )
{
listTerm = [ [ListTerm alloc]
initTerm: [[NumericTerm alloc] initNumeric: $1[index]]
tail: listTerm ];
}
$$ = listTerm;
}
;
structure
: T_LPAREN arg_sequence T_RPAREN
{
$$ = $2;
TRACE_RULE(@"T_LPAREN arg_sequence T_RPAREN", $$);
}
;
list
: T_SQUARE_LPAREN list_sequence
{
$$ = [[ListTerm alloc] initTerm: $2 tail: nil];
TRACE_RULE(@"T_SQUARE_LPAREN list_sequence", $$);
}
| T_SQUARE_LPAREN T_SQUARE_RPAREN
{
$$ = [[ListTerm alloc] initTerm: [[ListTerm alloc] init] tail: nil];
TRACE_RULE(@"T_SQUARE_LPAREN T_SQUARE_RPAREN", $$);
}
| T_DOT T_LPAREN goal T_COMMA list_pair T_RPAREN
{
[$3 setTail: $5];
$$ = [[ListTerm alloc] initTerm: $3 tail: nil];
TRACE_RULE(@"T_DOT T_LPAREN goal T_COMMA list_pair T_RPAREN", $$);
}
| T_DOT T_LPAREN goal T_SQUARE_LPAREN T_SQUARE_RPAREN T_RPAREN
{
$$ = [[ListTerm alloc] initTerm: $3 tail: nil];
TRACE_RULE(@"T_DOT T_LPAREN goal T_SQUARE_LPAREN T_SQUARE_RPAREN T_RPAREN", $$);
}
;
list_sequence
: goal T_SQUARE_RPAREN
{
id listTerm;
listTerm = [[ListTerm alloc] initTerm: nil tail: nil];
[$1 setTail: listTerm];
$$ = $1;
TRACE_RULE(@"goal T_SQUARE_RPAREN", $$);
}
| goal T_HEAD_TAIL_SEPARATOR list_end
{
[$1 setTail: $3];
TRACE_RULE(@"goal T_HEAD_TAIL_SEPARATOR list_end", $$);
}
| goal T_COMMA list_sequence
{
[$1 setTail: $3];
TRACE_RULE(@"goal T_COMMA list_sequence", $$);
}
;
list_end
: list T_SQUARE_RPAREN
{
id listTerm;
listTerm = [[ListTerm alloc] initTerm: nil tail: nil];
[$1 setTail: listTerm];
$$ = $1;
TRACE_RULE(@"list T_SQUARE_RPAREN", $$);
}
| variable T_SQUARE_RPAREN
{
TRACE_RULE(@"variable T_SQUARE_RPAREN", $$);
}
;
list_pair
: T_DOT T_LPAREN goal T_COMMA list_pair T_RPAREN
{
[$3 setTail: $5];
$$ = $3;
TRACE_RULE(@"T_DOT T_LPAREN goal T_COMMA list_pair T_RPAREN", $$);
}
| T_DOT T_LPAREN goal T_COMMA T_SQUARE_LPAREN T_SQUARE_RPAREN T_RPAREN
{
id listTerm;
listTerm = [[ListTerm alloc] initTerm: nil tail: nil];
[$3 setTail: listTerm];
$$ = $3;
TRACE_RULE(@"T_DOT T_LPAREN goal T_COMMA T_SQUARE_LPAREN T_SQUARE_RPAREN T_RPAREN", $$);
}
;
standard_goal_sequence
: standard_goal
| standard_goal_sequence standard_goal
;
standard_goal
: T_IDENTIFIER
| variable
| T_STRING_LIT
| numeric_literal
| standard_list
;
standard_list
: T_STANDARD_LPAREN standard_list_sequence
| T_STANDARD_LPAREN T_STANDARD_RPAREN
;
standard_list_sequence
: standard_goal T_STANDARD_RPAREN
| standard_goal T_HEAD_TAIL_SEPARATOR standard_list_end
| standard_goal standard_list_sequence
;
standard_list_end
: standard_list T_STANDARD_RPAREN
| variable T_STANDARD_RPAREN
;
%%
id
CurrentTerm()
{
return currentTerm;
}
id
addNamedVariable(char * name)
{
NSString * variableName = [NSString stringWithUTF8String: name];
if ([variableTable objectForKey: variableName] == nil) {
[variableTable setValue: [[[NamedVariable alloc] initVariable: variableName] retain] forKey: variableName];
}
return (NamedVariable *) [[variableTable objectForKey: variableName] retain];
}
/*--------------------------------------------------------------------------*/
/* yywrap() */
/*--------------------------------------------------------------------------*/
int
yywrap()
{
return YES;
}
/*--------------------------------------------------------------------------*/
/* clearLookaheadToken()
/* - discards both the yacc lookahead token and the lex lookahead buffer.
/*--------------------------------------------------------------------------*/
static void
clearLookaheadToken(void)
{
yyclearin;
ResetLex();
return;
}