-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathf2lp.c
9059 lines (7735 loc) · 177 KB
/
f2lp.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
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*********************************************************************
f2lp.c - This program turns first order formulas to
answer set programs under the stable model semantics.
version 1.3 22-Apr-2013
Copyright (C) <2009> <Joohyung Lee and Ravi Palla>
All rights reserved.
For information on how to contact the authors, please visit
"http://reasoning.eas.asu.edu/f2lp".
The authors hereby grant USER nonexclusive permission
to use, copy and/or modify this software for internal,
noncommercial, research purposes only. Any distribution,
including commercial sale or license, of this software,
copies of the software, its associated documentation and/or
modifications of either is strictly prohibited without the
prior consent of authors.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
For the complete copyright and warranty information, please read
the "COPYRIGHT" file provided along with this software.
*********************************************************************/
////NOTE TO SELF: ASP BLOCK DOES NOT WORK WHEN IT IS THE LAST ELEMENT
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <ctype.h>
#define VERSION "f2lp version 1.3"
#define USAGE "f2lp [input_file_1] . . . [input_file_n]\n"
#define NOINPUTFILE "if no input files are provided, then STDIN is considered.\n"
#define OPTION_HELP "usage information: -h --h -help --help\n"
#define OPTION_VERSION "version number: -v --v -version --version\n"
#define OPTION_CLASSICAL "treat '-' as classical negation: -c\n"
#define OPTION_DEFAULT "treat '-' as default negation: -d\n"
#define OPTION_ICLINGO "attempt to output i/oClingo compatible programs: -i\n"
#ifndef _LINE_MAX
#define _LINE_MAX 16384
#endif
#ifndef MAX_PREDICATE_LENGTH
#define MAX_PREDICATE_LENGTH 500
#endif
/*
* maximum symbols other than alnum in a formula
*/
#ifndef MAX_SYMBOLS
#define MAX_SYMBOLS 500
#endif
/*
* input to solver
*/
FILE *fpSolverInput = NULL;
FILE *fpError = NULL;
struct stack {
int top;
char item[MAX_SYMBOLS];
};
struct node {
struct node *left;
struct node *right;
char val[MAX_PREDICATE_LENGTH];
int prime;
};
#ifdef BAR_NEG_ATOM
struct baratom {
char val[MAX_PREDICATE_LENGTH];
struct baratom *next;
};
struct negatom {
char val[MAX_PREDICATE_LENGTH];
struct negatom *next;
};
#endif
struct qf {
char *ptr;
struct qf *next;
};
struct agg {
char exp[MAX_PREDICATE_LENGTH];
char rpl[MAX_PREDICATE_LENGTH];
struct agg *next;
};
typedef struct node *NODEP;
#ifdef BAR_NEG_ATOM
struct negatom *root_negatom = NULL;
struct negatom *prev_negatom = NULL;
struct baratom *root_baratom = NULL;
struct baratom *prev_baratom = NULL;
#endif
struct qf *root_qf = NULL;
struct agg *root_agg = NULL;
NODEP root_prefix = NULL;
/*
* added these
*/
int reading_index = 0;
int other_found = 0; //for considering associativity
void applyRules(NODEP root);
void checkRuleForm(NODEP root, int *rule_flag,
int ant_cons_flag);
void format_and_write(NODEP root);
void concat(char *str1, char *str2, char *dst);
int isPredicateSymbol(char c);
char* itoa(int val, int base);
/*
* until here
*/
void push(struct stack *ps, char n);
void pop(struct stack *ps);
int isempty(struct stack *ps);
int prcd(char stktop, char op);
void toPrefix(char *input, char *output);
void eliminate_imp(NODEP root);
int index_prefix = 0;
#ifdef BAR_NEG_ATOM
int no_baratoms = 0;
#endif
int var_index = 1;
int pred_index = 1;
int added_dom = 0;
int agg_index = 1;
// Added by Joe:
// Used to keep track of whether or not
// we are outputting an iclingo friendly format...
int output_iclingo = 0;
/*************
DEBUG info
*************/
int debug_count = 0;
NODEP
prefix_to_tree(char *prefix, int dummy)
{
char nextchar = '\0';
char litchar = '\0';
int i = 0;
int temp_index = 0;
NODEP newnode = (NODEP) malloc(sizeof(struct node));
if ( newnode == NULL )
{
fprintf(stderr,"Out of Memory\n");
exit(1);
}
newnode->left = NULL;
newnode->right = NULL;
#if 0 //will use convert_NNF instead
if (dummy == 1) {
/*
* add false
*/
newnode->val[0] = 'f';
newnode->val[1] = 'a';
newnode->val[2] = 'l';
newnode->val[3] = 's';
newnode->val[4] = 'e';
newnode->val[5] = '\0';
return newnode;
}
#endif
nextchar = prefix[index_prefix++];
/*
* Skip spaces.
*/
// while((nextchar == ' ') || (nextchar == '\t'))
// nextchar = prefix[index++];
/*
* Echo the character so we have a record.
*/
// putchar(nextchar);
if (nextchar == '\0') {
fprintf(stderr, "Premature end of formula. \n");
exit(1);
}
/*
* for negation - if negative literal, then do not add left child
*/
//litchar = prefix[index_prefix]; //will use convert_NNF instead
/*
* If it's a variable, it is a prefix expression by itself.
*/
if (isPredicateSymbol(nextchar)) {
do {
newnode->val[i++] = nextchar;
nextchar = prefix[index_prefix++];
} while (nextchar != '$');
/*
if (nextchar == '*') {
// printf("star atom %c\n", newnode->val[0]);
newnode->prime = 1;
} else {
newnode->prime = 0;
}*/
newnode->val[i] = '\0';
return newnode;
}
/*
* Else - must be an operator.
*/
// printf("char %c\n",nextchar);
#if 0
if (litchar == '\0') {
fprintf(stderr, "Premature end of formula.\n");
exit(1);
}
#endif
/*
* Find the next complete prefix expression, then the one after that,
* and stick it all together.
*/
#if 0
if (nextchar != '-') {
newnode->left = prefix_to_tree(prefix, 0);
}
#endif
/*
* if negative literal, then do not add left child
*/
if ( (nextchar != '-') &&
((nextchar != '!') && (nextchar != '?')) ) {
// printf("adding left child\n");
newnode->left = prefix_to_tree(prefix, 0);
}
if ( (nextchar == '!') || (nextchar == '?') )
{
temp_index = index_prefix-1;
/* proceed to char after quantifier */
while(prefix[index_prefix] != ':')
index_prefix++;
index_prefix++;
}
//if ((nextchar != '-') || isPredicateSymbol(litchar)) {
// printf("adding right child with 0\n");
newnode->right = prefix_to_tree(prefix, 0);
//}
#if 0
else {
// printf("adding right child with 1\n");
newnode->right = prefix_to_tree(prefix, 1);
}
#endif
/*
* Turn negation to implication
*/
if ( (nextchar == '!') || (nextchar == '?') )
{
/* proceed to char after quantifier */
while(prefix[temp_index] != ':')
{
newnode->val[i++] = prefix[temp_index++];
}
newnode->val[i++] = ':';
newnode->val[i] = '\0';
}
else
{
newnode->val[0] = nextchar;
newnode->val[1] = '\0';
}
return newnode;
}
void
postfix(NODEP tree)
{
if (tree == NULL)
return;
/*
* Print everything to left, then everything to right, then content of
* this node.
*/
postfix(tree->left);
postfix(tree->right);
// printf("%c",tree -> val[0]);
}
void
inorder_print(NODEP tree)
{
if (tree == NULL)
return;
if (!isPredicateSymbol(tree->val[0])
&& !isPredicateSymbol((char) ((int) tree->val[0] - 100))) {
printf("%c", '(');
}
inorder_print(tree->left);
printf("%c", tree->val[0]);
inorder_print(tree->right);
if (!isPredicateSymbol(tree->val[0])
&& !isPredicateSymbol((char) ((int) tree->val[0] - 100))) {
printf("%c", ')');
}
}
void
inorder_print_nobr(NODEP tree)
{
if (tree == NULL)
return;
inorder_print_nobr(tree->left);
printf("%c", tree->val[0]);
inorder_print_nobr(tree->right);
}
void
inorder_save_br(NODEP tree, char *infix)
{
int i = 0;
if (tree == NULL)
return;
if ( (tree->left != NULL) || (tree->val[0] == '-') ||
(tree->val[0] == '!') || (tree->val[0] == '?') )
{
infix[reading_index++] = '(';
}
inorder_save_br(tree->left, infix);
for (i = 0; tree->val[i] != '\0'; i++) {
infix[reading_index++] = tree->val[i];
}
infix[reading_index] = '\0';
inorder_save_br(tree->right, infix);
if ( tree->right != NULL )
{
infix[reading_index++] = ')';
}
infix[reading_index] = '\0';
}
void
inorder_save(NODEP tree, char *infix)
{
int i = 0;
if (tree == NULL)
return;
inorder_save(tree->left, infix);
for (i = 0; tree->val[i] != '\0'; i++) {
infix[reading_index++] = tree->val[i];
}
infix[reading_index] = '\0';
inorder_save(tree->right, infix);
}
void
push(struct stack *ps, char n)
{
ps->top++;
ps->item[ps->top] = n;
}
void
pop(struct stack *ps)
{
ps->top--;
}
int
isempty(struct stack *ps)
{
if (ps->top == -1) {
return (1);
} else {
return (0);
}
}
int
prcd(char stktop, char op)
{
if ((stktop == ':') && ((op == '&') || (op == '|'))) {
return 0;
}
if (((stktop == '&') || (stktop == '|')) && (op == ':')) {
return 1;
}
#if 0
if (stktop == op) {
return 0;
}
#endif
if (stktop == ')') {
return 0;
}
#if 0
if ((stktop != '(') && (op == ')')) {
return 0;
}
#endif
if (op == '(') {
return 1;
}
if ( (stktop == '-') || (stktop == '_') ) {
return 1;
}
if ( (op == '-') || (op == '_') ) {
return 0;
}
return 0;
}
void
toPrefix(char *input, char *output)
{
struct stack s;
char optop,
ans;
char *p = input;
char op = (char)0;
int i = 0,
j = 0,
k = 0,
m;
int qf_index = 0, qf_base = 0;
struct qf *temp = NULL, *temp1 = NULL;
// int tuple_start = 0;
int paran_count = 0; // to take care of reification
s.top = -1;
while (*p != '\0') {
p++;
i++;
}
i--; /* now size of infix expression is known
* and counter 'i' indicates to end of
* expression */
p = input;
while (*p != '\0') {
if (p != input) {
/*
* indicate tuple start
*/
if (((*p == '(') && isPredicateSymbol(*(p - 1))) && (paran_count == 0)) {
// tuple_start = 1;
paran_count = 1;
j++;
p++;
continue;
}
}
if (((*p == '(') && (paran_count == 0)) ||
((*p == ')') && (paran_count == 0))) {
p++;
} else if ((*p == '(') && (paran_count != 0)) {
paran_count++;
j++;
p++;
} else if ((*p == ')') && (paran_count != 0)) {
paran_count--;
j++;
p++;
} else {
j++;
p++;
}
} /* now size of postfix expression is known
*/
m = j;
j--; /* now j indicates end of prefix
* expression */
// printf("infix %s with j val i val %d %d\n", input,j,i);
while (i >= 0) {
if ((input[i] == ')')
&& ((input[i + 1] == '$'))) {
// tuple_start = 1;
paran_count = 1;
output[j] = input[i];
j--;
} else if ((isPredicateSymbol(input[i])) ||
(input[i] == '$') ||
(paran_count != 0)) {
if (input[i] == '(') {
// tuple_start = 0;
paran_count--;
}
if (input[i] == ')') {
// tuple_start = 0;
paran_count++;
}
output[j] = input[i];
j--;
} else {
/* check for quantifiers */
if ((input[i] == ':') && (input[i-1] == ']') )
op = '_';
else
op = input[i];
/* if stack top has more precedence */
while (!isempty(&s) && (prcd(s.item[s.top], op)))
{
optop = s.item[s.top];
pop(&s);
//printf("popping %c op %c\n",optop,input[i]);
/* if stack top is _, then read the whole quantifier */
if ( optop == '_' )
{
qf_index = 0;
while(root_qf->ptr[qf_index] != '\0')
{
qf_index++;
}
qf_index--;
while ( qf_index >= 0 )
output[j--] = root_qf->ptr[qf_index--];
root_qf = root_qf->next;
}
else
{
output[j--] = optop;
}
}
if (isempty(&s) || input[i] != '(') {
if ((input[i] == ':') && (input[i-1] == ']') )
{
//printf("pushing _\n");
push(&s,'_');
qf_index = i;
while( (input[qf_index] != '!') && (input[qf_index] != '?') )
{
qf_index--;
}
/* store quantifier in the list */
temp = (struct qf *) malloc(sizeof (struct qf));
temp->ptr = (char *)malloc(i-qf_index+2);
if ( (temp == NULL) || (temp->ptr == NULL) )
{
fprintf(stderr,"Out of Memory\n");
exit(1);
}
temp->ptr[i-qf_index+1] = '\0';
temp->next = NULL;
qf_base = qf_index;
while( qf_index <= i )
{
temp->ptr[qf_index-qf_base] = input[qf_index];
qf_index++;
}
if ( root_qf == NULL )
{
root_qf = temp;
}
else
{
temp1 = root_qf;
while(temp1->next != NULL)
temp1 = temp1->next;
temp1->next = temp;
}
/* ignore the rest of the quantifier */
while ( (input[i] != '!') && (input[i] != '?') )
i--;
}
else
{
//printf("pushing %c\n",input[i]);
push(&s, input[i]);
}
} else {
//printf("popping\n");
pop(&s);
}
}
i--;
}
while (!isempty(&s)) {
optop = s.item[s.top];
pop(&s);
//printf("popping %c\n",optop);
if ( optop == '_' )
{
qf_index = 0;
while(root_qf->ptr[qf_index] != '\0')
{
qf_index++;
}
qf_index--;
while ( qf_index >= 0 )
output[j--] = root_qf->ptr[qf_index--];
root_qf = root_qf->next;
}
else
{
output[j--] = optop;
}
}
// printf("\nprefix form:\t");
output[m] = '\0';
}
#ifdef BAR_NEG_ATOM
void
convert_neg_lp(char *inF, int lineNum, int *size_incr)
{
int i = 0;
int j = 0, k = 0;
int neg_exist = 0;
struct negatom *newnode = NULL;
struct negatom *negnode = NULL;
int neg_index = 0;
int tuple_start = 0;
int paran_count = 0;
for (i = 0; inF[i] != '\0'; i++) {
if ( inF[i] == '-' ) {
if ( i > 0 && inF[i-1] == ':' )
continue;
if (!isPredicateSymbol(inF[i + 1])) {
fprintf
(stderr,"Parse Error At Line %d - Strong negation can only be applied to an atom\n",
lineNum + 1);
exit(1);
}
/*
* create atoms ending with 'neg'
*/
for (j = i; inF[j] != '\0'; j++) {
inF[j] = inF[j + 1];
}
newnode = (struct negatom *) malloc(sizeof(struct negatom));
if ( newnode == NULL )
{
fprintf(stderr,"Out of Memory\n");
exit(1);
}
newnode->next = NULL;
k = 0;
/*
* read the entire atom
*/
while (isPredicateSymbol(inF[i])) {
newnode->val[k++] = inF[i++];
}
neg_index = i;
if (inF[i] == '(') {
paran_count = 1;
while (paran_count != 0) {
newnode->val[k++] = inF[i++];
if (inF[i] == '(')
paran_count++;
if (inF[i] == ')')
paran_count--;
}
newnode->val[k++] = ')';
// i++;
}
newnode->val[k] = '\0';
neg_exist = 0;
for (negnode = root_negatom; negnode != NULL;
negnode = negnode->next) {
if (!strcmp(negnode->val, newnode->val)) {
neg_exist = 1;
}
}
if (!neg_exist) {
if (prev_negatom != NULL) {
prev_negatom->next = newnode;
prev_negatom = newnode;
} else {
root_negatom = newnode;
prev_negatom = root_negatom;
}
}
/*
* shift all char right by 3 places
*/
for (j = neg_index; inF[j] != '\0'; j++);
for (; j >= neg_index; j--) {
inF[j + 3] = inF[j];
}
/*
* add 'neg'
*/
inF[neg_index++] = 'n';
inF[neg_index++] = 'e';
inF[neg_index] = 'g';
*size_incr = *size_incr+2;
// inF[i] = '~';
}
}
}
#endif
int isParan( char c )
{
if ( (c == '(') || (c == ')' ) )
return 1;
return 0;
}
void replaceSpecialPred ( char *inF, int lineNum )
{
int start_index = -1;
int i = 0, j = 0;
int le = 0, ge = 0;
int compare = 0;
int paran_count = 0;
for ( i = 0; inF[i] != '\0'; i++ )
{
compare = 0;
switch (inF[i])
{
case '=':
paran_count = 0;
if ( i == 0 || (!isPredicateSymbol(inF[i-1]) && !isParan(inF[i-1])) )
{
fprintf(stderr,"parse error at line %d\n",lineNum+1);
exit(1);
}
if ( (!isPredicateSymbol(inF[i+1]) && !isParan(inF[i+1])) && (inF[i+1] != '=') )
{
fprintf(stderr,"parse error at line %d\n",lineNum+1);
exit(1);
}
if ( inF[i+1] == '=' )
compare = 1;
start_index = i-1;
while ( (isPredicateSymbol(inF[start_index]) ||
isParan(inF[start_index]) ||
(paran_count != 0)) &&
(start_index != 0))
{
if ( inF[start_index] == ')' )
paran_count--;
if ( inF[start_index] == '(' )
{
if (paran_count == 0)
break;
paran_count++;
}
start_index--;
}
if ( start_index == 0 )
{
if ( (paran_count == 0) && (inF[start_index] == '(') )
start_index = 0;
else
start_index = -1;
}
j = i;
/* shift by 3 or 8 characters */
while ( inF[j] != '\0' )
j++;
while ( j != start_index )
{
if ( compare == 1 )
{
inF[j+3] = inF[j];
}
else
{
inF[j+8] = inF[j];
}
j--;
}
j++;
if ( compare == 1 )
{
inF[j++] = 'e';
inF[j++] = 'q';
}
else
{
inF[j++] = 'a';
inF[j++] = 's';
inF[j++] = 's';
inF[j++] = 'i';
inF[j++] = 'g';
inF[j++] = 'n';
}
inF[j++] = '(';
if ( compare == 0 )
{
while ( inF[j+1] != '=' )
{
inF[j] = inF[j+1];
j++;
}
}
else
{
while ( inF[j] != '=' )
j++;
}
inF[j] = ',';
if ( !isPredicateSymbol(inF[j+2]) && !isParan(inF[j+2]) )
{
fprintf(stderr,"parse error at line %d\n",lineNum+1);
exit(1);
}
j++;
paran_count = 0;
while ( isPredicateSymbol(inF[j+1]) || isParan(inF[j+1]) || (paran_count != 0))
{
if ( inF[j+1] == ')' )
{
if ( paran_count == 0 )
break;
paran_count--;
}
if ( inF[j+1] == '(' )
{
paran_count++;
}
inF[j] = inF[j+1];
j++;
}
inF[j] = ')';
i = j;
break;
case '!':
paran_count = 0;
if ( inF[i+1] == '[' )
break;
if ( i == 0 || (!isPredicateSymbol(inF[i-1]) && !isParan(inF[i-1])) || (inF[i+1] != '=') )
{
fprintf(stderr,"parse error at line %d\n",lineNum+1);
exit(1);
}
start_index = i-1;
while ( (isPredicateSymbol(inF[start_index]) ||
isParan(inF[start_index]) ||
(paran_count != 0)) &&
(start_index != 0) )
{
if ( inF[start_index] == ')' )
paran_count--;
if ( inF[start_index] == '(' )
{
if (paran_count == 0)
break;
paran_count++;
}
start_index--;
}
if ( start_index == 0 )
{
if ( (paran_count == 0) && (inF[start_index] == '(') )
start_index = 0;
else
start_index = -1;
}
j = i;
/* shift by 4 characters */
while ( inF[j] != '\0' )
j++;
while ( j != start_index )
{
inF[j+4] = inF[j];
j--;
}
j++;
inF[j++] = 'n';
inF[j++] = 'e';
inF[j++] = 'q';
inF[j++] = '(';
while ( inF[j] != '!' )
j++;
inF[j] = ',';
if ( !isPredicateSymbol(inF[j+2]) && !isParan(inF[j+2]) )
{
fprintf(stderr,"parse error at line %d %c\n",lineNum+1,inF[j+2]);
exit(1);
}
j++;
paran_count = 0;
while ( isPredicateSymbol(inF[j+1]) || isParan(inF[j+1]) || (paran_count != 0))
{
if ( inF[j+1] == ')' )
{
if ( paran_count == 0 )
break;
paran_count--;
}
if ( inF[j+1] == '(' )