This repository has been archived by the owner on Mar 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules.y
1689 lines (1547 loc) · 50.3 KB
/
rules.y
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
/* Miranda token declarations and syntax rules for "YACC" */
/**************************************************************************
* Copyright (C) Research Software Limited 1985-90. All rights reserved. *
* The Miranda system is distributed as free software under the terms in *
* the file "COPYING" which is included in the distribution. *
* *
* Revised to C11 standard and made 64bit compatible, January 2020 *
*------------------------------------------------------------------------*/
/* miranda symbols */
%token VALUE EVAL WHERE IF TO LEFTARROW COLONCOLON COLON2EQ
TYPEVAR NAME CNAME CONST DOLLAR2 OFFSIDE ELSEQ
ABSTYPE WITH DIAG EQEQ FREE INCLUDE EXPORT TYPE
OTHERWISE SHOWSYM PATHNAME BNF LEX ENDIR ERRORSY ENDSY
EMPTYSY READVALSY LEXDEF CHARCLASS ANTICHARCLASS LBEGIN
%right ARROW
%right PLUSPLUS ':' MINUSMINUS
%nonassoc DOTDOT
%right VEL
%right '&'
%nonassoc '>' GE '=' NE LE '<'
%left '+' '-'
%left '*' '/' REM DIV
%right '^'
%left '.' /* fiddle to make '#' behave */
%left '!'
%right INFIXNAME INFIXCNAME
%token CMBASE /* placeholder to start combinator values - see combs.h */
%{
/* the following definition has to be kept in line with the token declarations
above */
char *yysterm[]= {
0,
"VALUE",
"EVAL",
"where",
"if",
"&>",
"<-",
"::",
"::=",
"TYPEVAR",
"NAME",
"CONSTRUCTOR-NAME",
"CONST",
"$$",
"OFFSIDE",
"OFFSIDE =",
"abstype",
"with",
"//",
"==",
"%free",
"%include",
"%export",
"type",
"otherwise",
"show",
"PATHNAME",
"%bnf",
"%lex",
"%%",
"error",
"end",
"empty",
"readvals",
"NAME",
"`char-class`",
"`char-class`",
"%%begin",
"->",
"++",
"--",
"..",
"\\/",
">=",
"~=",
"<=",
"mod",
"div",
"$NAME",
"$CONSTRUCTOR"};
%}
/* Miranda syntax rules */
/* the associated semantic actions perform the compilation */
%{
#include "data.h"
#include "big.h"
#include "lex.h"
extern word nill,k_i,Void;
extern word message,standardout;
extern word big_one;
#define isltmess_t(t) (islist_t(t)&&tl[t]==message)
#define isstring_t(t) (islist_t(t)&&tl[t]==char_t)
extern word SYNERR,errs,echoing,gvars;
extern word listdiff_fn,indent_fn,outdent_fn;
word lastname=0;
word suppressids=NIL;
word idsused=NIL;
word tvarscope=0;
word includees=NIL,embargoes=NIL,exportfiles=NIL,freeids=NIL,exports=NIL;
word lexdefs=NIL,lexstates=NIL,inlex=0,inexplist=0;
word inbnf=0,col_fn=0,fnts=NIL,eprodnts=NIL,nonterminals=NIL,sreds=0;
word ihlist=0,ntspecmap=NIL,ntmap=NIL,lasth=0;
word obrct=0;
void evaluate(x)
word x;
{ word t;
t=type_of(x);
if(t==wrong_t)return;
lastexp=x;
x=codegen(x);
if(polyshowerror)return;
if(process())
/* setup new process for each evaluation */
{ (void)signal(SIGINT,(sighandler)dieclean);
/* if interrupted will flush output etc before going */
compiling=0;
resetgcstats();
output(isltmess_t(t)?x:
cons(ap(standardout,isstring_t(t)?x
:ap(mkshow(0,0,t),x)),NIL));
(void)signal(SIGINT,SIG_IGN);/* otherwise could do outstats() twice */
putchar('\n');
outstats();
exit(0); }
}
void obey(x) /* like evaluate but no fork, no stats, no extra '\n' */
word x;
{ word t=type_of(x);
x=codegen(x);
if(polyshowerror)return;
compiling=0;
output(isltmess_t(t)?x:
cons(ap(standardout,isstring_t(t)?x:ap(mkshow(0,0,t),x)),NIL));
}
int isstring(x)
word x;
{ return(x==NILS||tag[x]==CONS&&is_char(hd[x]));
}
word compose(x) /* used in compiling 'cases' */
word x;
{ word y=hd[x];
if(hd[y]==OTHERWISE)y=tl[y]; /* OTHERWISE was just a marker - lose it */
else y=tag[y]==LABEL?label(hd[y],ap(tl[y],FAIL)):
ap(y,FAIL); /* if all guards false result is FAIL */
x = tl[x];
if(x!=NIL)
{ while(tl[x]!=NIL)y=label(hd[hd[x]],ap(tl[hd[x]],y)), x=tl[x];
y=ap(hd[x],y);
/* first alternative has no label - label of enclosing rhs applies */
}
return(y);
}
int eprod(word);
word starts(x) /* x is grammar rhs - returns list of nonterminals in start set */
word x;
{ L: switch(tag[x])
{ case ID: return(cons(x,NIL));
case LABEL:
case LET:
case LETREC: x=tl[x]; goto L;
case AP: switch(hd[x])
{ case G_SYMB:
case G_SUCHTHAT:
case G_RULE: return(NIL);
case G_OPT:
case G_FBSTAR:
case G_STAR: x=tl[x]; goto L;
default: if(hd[x]==outdent_fn)
{ x=tl[x]; goto L; }
if(tag[hd[x]]==AP)
if(hd[hd[x]]==G_ERROR)
{ x=tl[hd[x]]; goto L; }
if(hd[hd[x]]==G_SEQ)
{ if(eprod(tl[hd[x]]))
return(UNION(starts(tl[hd[x]]),starts(tl[x])));
x=tl[hd[x]]; goto L; } else
if(hd[hd[x]]==G_ALT)
return(UNION(starts(tl[hd[x]]),starts(tl[x])));
else
if(hd[hd[x]]==indent_fn)
{ x=tl[x]; goto L; }
}
default: return(NIL);
}
}
int eprod(x) /* x is grammar rhs - does x admit empty production? */
word x;
{ L: switch(tag[x])
{ case ID: return(member(eprodnts,x));
case LABEL:
case LET:
case LETREC: x=tl[x]; goto L;
case AP: switch(hd[x])
{ case G_SUCHTHAT:
case G_ANY:
case G_SYMB: return(0);
case G_RULE: return(1);
case G_OPT:
case G_FBSTAR:
case G_STAR: return(1);
default: if(hd[x]==outdent_fn)
{ x=tl[x]; goto L; }
if(tag[hd[x]]==AP)
if(hd[hd[x]]==G_ERROR)
{ x=tl[hd[x]]; goto L; }
if(hd[hd[x]]==G_SEQ)
return(eprod(tl[hd[x]])&&eprod(tl[x])); else
if(hd[hd[x]]==G_ALT)
return(eprod(tl[hd[x]])||eprod(tl[x]));
else
if(hd[hd[x]]==indent_fn)
{ x=tl[x]; goto L; }
}
default: return(x==G_STATE||x==G_UNIT);
/* G_END is special case, unclear whether it counts as an e-prodn.
decide no for now, sort this out later */
}
}
word add_prod(d,ps,hr)
word d,ps,hr;
{ word p,n=dlhs(d);
for(p=ps;p!=NIL;p=tl[p])
if(dlhs(hd[p])==n)
if(dtyp(d)==undef_t&&dval(hd[p])==UNDEF)
{ dval(hd[p])=dval(d); return(ps); } else
if(dtyp(d)!=undef_t&&dtyp(hd[p])==undef_t)
{ dtyp(hd[p])=dtyp(d); return(ps); }
else
errs=hr,
printf(
"%ssyntax error: conflicting %s of nonterminal \"%s\"\n",
echoing?"\n":"",
dtyp(d)==undef_t?"definitions":"specifications",
get_id(n)),
acterror();
return(cons(d,ps));
}
/* clumsy - this algorithm is quadratic in number of prodns - fix later */
word getloc(nt,prods) /* get here info for nonterminal */
word nt,prods;
{ while(prods!=NIL&&dlhs(hd[prods])!=nt)prods=tl[prods];
if(prods!=NIL)return(hd[dval(hd[prods])]);
return(0); /* should not happen, but just in case */
}
void findnt(nt) /* set errs to here info of undefined nonterminal */
word nt;
{ word p=ntmap;
while(p!=NIL&&hd[hd[p]]!=nt)p=tl[p];
if(p!=NIL)
{ errs=tl[hd[p]]; return; }
p=ntspecmap;
while(p!=NIL&&hd[hd[p]]!=nt)p=tl[p];
if(p!=NIL)errs=tl[hd[p]];
}
#define isap2(fn,x) (tag[x]==AP&&tag[hd[x]]==AP&&hd[hd[x]]==(fn))
#define firstsymb(term) tl[hd[term]]
void binom(rhs,x)
/* performs the binomial optimisation on rhs of nonterminal x
x: x alpha1| ... | x alphaN | rest ||need not be in this order
==>
x: rest (alpha1|...|alphaN)*
*/
word rhs,x;
{ word *p= &tl[rhs]; /* rhs is of form label(hereinf, stuff) */
word *lastp=0,*holdrhs,suffix,alpha=NIL;
if(tag[*p]==LETREC)p = &tl[*p]; /* ignore trailing `where defs' */
if(isap2(G_ERROR,*p))p = &tl[hd[*p]];
holdrhs=p;
while(isap2(G_ALT,*p))
if(firstsymb(tl[hd[*p]])==x)
alpha=cons(tl[tl[hd[*p]]],alpha),
*p=tl[*p],p = &tl[*p];
else lastp=p,p = &tl[tl[*p]];
/* note each (G_ALT a b) except the outermost is labelled */
if(lastp&&firstsymb(*p)==x)
alpha=cons(tl[*p],alpha),
*lastp=tl[hd[*lastp]];
if(alpha==NIL)return;
suffix=hd[alpha],alpha=tl[alpha];
while(alpha!=NIL)
suffix=ap2(G_ALT,hd[alpha],suffix),
alpha=tl[alpha];
*holdrhs=ap2(G_SEQ,*holdrhs,ap(G_FBSTAR,suffix));
}
/* should put some labels on the alpha's - fix later */
word getcol_fn()
{ extern char *dicp,*dicq;
if(!col_fn)
strcpy(dicp,"bnftokenindentation"),
dicq=dicp+20,
col_fn=name();
return(col_fn);
}
void startbnf()
{ ntspecmap=ntmap=nonterminals=NIL;
if(fnts==0)col_fn=0; /* reinitialise, a precaution */
}
word ih_abstr(x) /* abstract inherited attributes from grammar rule */
word x;
{ word ih=ihlist;
while(ih!=NIL) /* relies on fact that ihlist is reversed */
x=lambda(hd[ih],x),ih=tl[ih];
return(x);
}
int can_elide(x) /* is x of the form $1 applied to ih attributes in order? */
word x;
{ word ih;
if(ihlist)
for(ih=ihlist;ih!=NIL&&tag[x]==AP;ih=tl[ih],x=hd[x])
if(hd[ih]!=tl[x])return(0);
return(x==mkgvar(1));
}
int e_re(x) /* does regular expression x match empty string ? */
word x;
{ L: if(tag[x]==AP)
{ if(hd[x]==LEX_STAR||hd[x]==LEX_OPT)return(1);
if(hd[x]==LEX_STRING)return(tl[x]==NIL);
if(tag[hd[x]]!=AP)return(0);
if(hd[hd[x]]==LEX_OR)
{ if(e_re(tl[hd[x]]))return(1);
x=tl[x]; goto L; } else
if(hd[hd[x]]==LEX_SEQ)
{ if(!e_re(tl[hd[x]]))return(0);
x=tl[x]; goto L; } else
if(hd[hd[x]]==LEX_RCONTEXT)
{ x=tl[hd[x]]; goto L; }
}
return(0);
}
%}
%%
entity: /* the entity to be parsed is either a definition script or an
expression (the latter appearing as a command line) */
error|
script
= { lastname=0; /* outstats(); */ }|
/* statistics not usually wanted after compilation */
/* MAGIC exp '\n' script
= { lastexp=$2; }| /* change to magic scripts 19.11.2013 */
VALUE exp
= { lastexp=$2; }| /* next line of `$+' */
EVAL exp
= { if(!SYNERR&&yychar==0)
{ evaluate($2); }
}|
EVAL exp COLONCOLON
/* boring problem - how to make sure no junk chars follow here?
likewise TO case -- trick used above doesn't work, yychar is
here always -1 Why? Too fiddly to bother with just now */
= { word t=type_of($2);
if(t!=wrong_t)
{ lastexp=$2;
if(tag[$2]==ID&&id_type($2)==wrong_t)t=wrong_t;
out_type(t);
putchar('\n'); }
}|
EVAL exp TO
= { FILE *fil=NULL,*efil;
word t=type_of($2);
char *f=token(),*ef;
if(f)keep(f); ef=token(); /* wasteful of dic space, FIX LATER */
if(f){ fil= fopen(f,$3?"a":"w");
if(fil==NULL)
printf("cannot open \"%s\" for writing\n",f); }
else printf("filename missing after \"&>\"\n");
if(ef)
{ efil= fopen(ef,$3?"a":"w");
if(efil==NULL)
printf("cannot open \"%s\" for writing\n",ef); }
if(t!=wrong_t)$2=codegen(lastexp=$2);
if(!polyshowerror&&t!=wrong_t&&fil!=NULL&&(!ef||efil))
{ int pid;/* launch a concurrent process to perform task */
sighandler oldsig;
oldsig=signal(SIGINT,SIG_IGN); /* ignore interrupts */
if(pid=fork())
{ /* "parent" */
if(pid==-1)perror("cannot create process");
else printf("process %d\n",pid);
fclose(fil);
if(ef)fclose(efil);
(void)signal(SIGINT,oldsig); }else
{ /* "child" */
(void)signal(SIGQUIT,SIG_IGN); /* and quits */
#ifndef SYSTEM5
(void)signal(SIGTSTP,SIG_IGN); /* and stops */
#endif
close(1); dup(fileno(fil)); /* subvert stdout */
close(2); dup(fileno(ef?efil:fil)); /* subvert stderr */
/* FUNNY BUG - if redirect stdout stderr to same file by two
calls to freopen, their buffers get conflated - whence do
by subverting underlying file descriptors, as above
(fix due to Martin Guy) */
/* formerly used dup2, but not present in system V */
fclose(stdin);
/* setbuf(stdout,NIL);
/* not safe to change buffering of stream already in use */
/* freopen would have reset the buffering automatically */
lastexp = NIL; /* what else should we set to NIL? */
/*atcount= 1; */
compiling= 0;
resetgcstats();
output(isltmess_t(t)?$2:
cons(ap(standardout,isstring_t(t)?$2:
ap(mkshow(0,0,t),$2)),NIL));
putchar('\n');
outstats();
exit(0); } } };
script:
/* empty */|
defs;
exp:
op | /* will later suppress in favour of (op) in arg */
e1;
op:
'~'
= { $$ = NOT; }|
'#'
= { $$ = LENGTH; }|
diop;
diop:
'-'
= { $$ = MINUS; }|
diop1;
diop1:
'+'
= { $$ = PLUS; }|
PLUSPLUS
= { $$ = APPEND; }|
':'
= { $$ = P; }|
MINUSMINUS
= { $$ = listdiff_fn; }|
VEL
= { $$ = OR; }|
'&'
= { $$ = AND; }|
relop |
'*'
= { $$ = TIMES; }|
'/'
= { $$ = FDIV; }|
DIV
= { $$ = INTDIV; }|
REM
= { $$ = MOD; }|
'^'
= { $$ = POWER; }|
'.'
= { $$ = B; }|
'!'
= { $$ = ap(C,SUBSCRIPT); }|
INFIXNAME|
INFIXCNAME;
relop:
'>'
= { $$ = GR; }|
GE
= { $$ = GRE; }|
eqop
= { $$ = EQ; }|
NE
= { $$ = NEQ; }|
LE
= { $$ = ap(C,GRE); }|
'<'
= { $$ = ap(C,GR); };
eqop:
EQEQ| /* silently accept for benefit of Haskell users */
'=';
rhs:
cases WHERE ldefs
= { $$ = block($3,compose($1),0); }|
exp WHERE ldefs
= { $$ = block($3,$1,0); }|
exp|
cases
= { $$ = compose($1); };
cases:
exp ',' if exp
= { $$ = cons(ap2(COND,$4,$1),NIL); }|
exp ',' OTHERWISE
= { $$ = cons(ap(OTHERWISE,$1),NIL); }|
cases reindent ELSEQ alt
= { $$ = cons($4,$1);
if(hd[hd[$1]]==OTHERWISE)
syntax("\"otherwise\" must be last case\n"); };
alt:
here exp
= { errs=$1,
syntax("obsolete syntax, \", otherwise\" missing\n");
$$ = ap(OTHERWISE,label($1,$2)); }|
here exp ',' if exp
= { $$ = label($1,ap2(COND,$5,$2)); }|
here exp ',' OTHERWISE
= { $$ = ap(OTHERWISE,label($1,$2)); };
if:
/* empty */
= { extern word strictif;
if(strictif)syntax("\"if\" missing\n"); }|
IF;
indent:
/* empty */
= { if(!SYNERR){layout(); setlmargin();}
};
/* note that because of yacc's one symbol look ahead, indent must usually be
invoked one symbol earlier than the non-terminal to which it applies
- see `production:' for an exception */
outdent:
separator
= { unsetlmargin(); };
separator:
OFFSIDE | ';' ;
reindent:
/* empty */
= { if(!SYNERR)
{ unsetlmargin(); layout(); setlmargin(); }
};
liste: /* NB - returns list in reverse order */
exp
= { $$ = cons($1,NIL); }|
liste ',' exp /* left recursive so as not to eat YACC stack */
= { $$ = cons($3,$1); };
e1:
'~' e1 %prec '='
= { $$ = ap(NOT,$2); }|
e1 PLUSPLUS e1
= { $$ = ap2(APPEND,$1,$3); }|
e1 ':' e1
= { $$ = cons($1,$3); }|
e1 MINUSMINUS e1
= { $$ = ap2(listdiff_fn,$1,$3); }|
e1 VEL e1
= { $$ = ap2(OR,$1,$3); }|
e1 '&' e1
= { $$ = ap2(AND,$1,$3); }|
reln |
e2;
es1: /* e1 or presection */
'~' e1 %prec '='
= { $$ = ap(NOT,$2); }|
e1 PLUSPLUS e1
= { $$ = ap2(APPEND,$1,$3); }|
e1 PLUSPLUS
= { $$ = ap(APPEND,$1); }|
e1 ':' e1
= { $$ = cons($1,$3); }|
e1 ':'
= { $$ = ap(P,$1); }|
e1 MINUSMINUS e1
= { $$ = ap2(listdiff_fn,$1,$3); }|
e1 MINUSMINUS
= { $$ = ap(listdiff_fn,$1); }|
e1 VEL e1
= { $$ = ap2(OR,$1,$3); }|
e1 VEL
= { $$ = ap(OR,$1); }|
e1 '&' e1
= { $$ = ap2(AND,$1,$3); }|
e1 '&'
= { $$ = ap(AND,$1); }|
relsn |
es2;
e2:
'-' e2 %prec '-'
= { $$ = ap(NEG,$2); }|
'#' e2 %prec '.'
= { $$ = ap(LENGTH,$2); }|
e2 '+' e2
= { $$ = ap2(PLUS,$1,$3); }|
e2 '-' e2
= { $$ = ap2(MINUS,$1,$3); }|
e2 '*' e2
= { $$ = ap2(TIMES,$1,$3); }|
e2 '/' e2
= { $$ = ap2(FDIV,$1,$3); }|
e2 DIV e2
= { $$ = ap2(INTDIV,$1,$3); } |
e2 REM e2
= { $$ = ap2(MOD,$1,$3); }|
e2 '^' e2
= { $$ = ap2(POWER,$1,$3); } |
e2 '.' e2
= { $$ = ap2(B,$1,$3); }|
e2 '!' e2
= { $$ = ap2(SUBSCRIPT,$3,$1); }|
e3;
es2: /* e2 or presection */
'-' e2 %prec '-'
= { $$ = ap(NEG,$2); }|
'#' e2 %prec '.'
= { $$ = ap(LENGTH,$2); }|
e2 '+' e2
= { $$ = ap2(PLUS,$1,$3); }|
e2 '+'
= { $$ = ap(PLUS,$1); }|
e2 '-' e2
= { $$ = ap2(MINUS,$1,$3); }|
e2 '-'
= { $$ = ap(MINUS,$1); }|
e2 '*' e2
= { $$ = ap2(TIMES,$1,$3); }|
e2 '*'
= { $$ = ap(TIMES,$1); }|
e2 '/' e2
= { $$ = ap2(FDIV,$1,$3); }|
e2 '/'
= { $$ = ap(FDIV,$1); }|
e2 DIV e2
= { $$ = ap2(INTDIV,$1,$3); } |
e2 DIV
= { $$ = ap(INTDIV,$1); } |
e2 REM e2
= { $$ = ap2(MOD,$1,$3); }|
e2 REM
= { $$ = ap(MOD,$1); }|
e2 '^' e2
= { $$ = ap2(POWER,$1,$3); } |
e2 '^'
= { $$ = ap(POWER,$1); } |
e2 '.' e2
= { $$ = ap2(B,$1,$3); }|
e2 '.'
= { $$ = ap(B,$1); }|
e2 '!' e2
= { $$ = ap2(SUBSCRIPT,$3,$1); }|
e2 '!'
= { $$ = ap2(C,SUBSCRIPT,$1); }|
es3;
e3:
comb INFIXNAME e3
= { $$ = ap2($2,$1,$3); }|
comb INFIXCNAME e3
= { $$ = ap2($2,$1,$3); }|
comb;
es3: /* e3 or presection */
comb INFIXNAME e3
= { $$ = ap2($2,$1,$3); }|
comb INFIXNAME
= { $$ = ap($2,$1); }|
comb INFIXCNAME e3
= { $$ = ap2($2,$1,$3); }|
comb INFIXCNAME
= { $$ = ap($2,$1); }|
comb;
comb:
comb arg
= { $$ = ap($1,$2); }|
arg;
reln:
e2 relop e2
= { $$ = ap2($2,$1,$3); }|
reln relop e2
= { word subject;
subject = hd[hd[$1]]==AND?tl[tl[$1]]:tl[$1];
$$ = ap2(AND,$1,ap2($2,subject,$3));
}; /* EFFICIENCY PROBLEM - subject gets re-evaluated (and
retypechecked) - fix later */
relsn: /* reln or presection */
e2 relop e2
= { $$ = ap2($2,$1,$3); }|
e2 relop
= { $$ = ap($2,$1); }|
reln relop e2
= { word subject;
subject = hd[hd[$1]]==AND?tl[tl[$1]]:tl[$1];
$$ = ap2(AND,$1,ap2($2,subject,$3));
}; /* EFFICIENCY PROBLEM - subject gets re-evaluated (and
retypechecked) - fix later */
arg:
{ if(!SYNERR)lexstates=NIL,inlex=1; }
LEX lexrules ENDIR
= { inlex=0; lexdefs=NIL;
if(lexstates!=NIL)
{ word echoed=0;
for(;lexstates!=NIL;lexstates=tl[lexstates])
{ if(!echoed)printf(echoing?"\n":""),echoed=1;
if(!(tl[hd[lexstates]]&1))
printf("warning: lex state %s is never entered\n",
get_id(hd[hd[lexstates]])); else
if(!(tl[hd[lexstates]]&2))
printf("warning: lex state %s has no associated rules\n",
get_id(hd[hd[lexstates]])); }
}
if($3==NIL)syntax("%lex with no rules\n");
else tag[$3]=LEXER;
/* result is lex-list, in reverse order, of items of the form
cons(scstuff,cons(matcher,rhs))
where scstuff is of the form
cons(0-or-list-of-startconditions,1+newstartcondition)
*/
$$ = $3; }|
NAME |
CNAME |
CONST |
READVALSY
= { $$ = readvals(0,0); }|
SHOWSYM
= { $$ = show(0,0); }|
DOLLAR2
= { $$ = lastexp;
if(lastexp==UNDEF)
syntax("no previous expression to substitute for $$\n"); }|
'[' ']'
= { $$ = NIL; }|
'[' exp ']'
= { $$ = cons($2,NIL); }|
'[' exp ',' exp ']'
= { $$ = cons($2,cons($4,NIL)); }|
'[' exp ',' exp ',' liste ']'
= { $$ = cons($2,cons($4,reverse($6))); }|
'[' exp DOTDOT exp ']'
= { $$ = ap3(STEPUNTIL,big_one,$4,$2); }|
'[' exp DOTDOT ']'
= { $$ = ap2(STEP,big_one,$2); }|
'[' exp ',' exp DOTDOT exp ']'
= { $$ = ap3(STEPUNTIL,ap2(MINUS,$4,$2),$6,$2); }|
'[' exp ',' exp DOTDOT ']'
= { $$ = ap2(STEP,ap2(MINUS,$4,$2),$2); }|
'[' exp '|' qualifiers ']'
= { $$ = SYNERR?NIL:compzf($2,$4,0); }|
'[' exp DIAG qualifiers ']'
= { $$ = SYNERR?NIL:compzf($2,$4,1); }|
'(' op ')' /* RSB */
= { $$ = $2; }|
'(' es1 ')' /* presection or parenthesised e1 */
= { $$ = $2; }|
'(' diop1 e1 ')' /* postsection */
= { $$ = (tag[$2]==AP&&hd[$2]==C)?ap(tl[$2],$3): /* optimisation */
ap2(C,$2,$3); }|
'(' ')'
= { $$ = Void; }| /* the void tuple */
'(' exp ',' liste ')'
= { if(tl[$4]==NIL)$$=pair($2,hd[$4]);
else { $$=pair(hd[tl[$4]],hd[$4]);
$4=tl[tl[$4]];
while($4!=NIL)$$=tcons(hd[$4],$$),$4=tl[$4];
$$ = tcons($2,$$); }
/* representation of the tuple (a1,...,an) is
tcons(a1,tcons(a2,...pair(a(n-1),an))) */
};
lexrules:
lexrules lstart here re indent { if(!SYNERR)inlex=2; }
ARROW exp lpostfix { if(!SYNERR)inlex=1; } outdent
= { if($9<0 && e_re($4))
errs=$3,
syntax("illegal lex rule - lhs matches empty\n");
$$ = cons(cons(cons($2,1+$9),cons($4,label($3,$8))),$1); }|
lexdefs
= { $$ = NIL; };
lstart:
/* empty */
= { $$ = 0; }|
'<' cnames '>'
= { word ns=NIL;
for(;$2!=NIL;$2=tl[$2])
{ word *x = &lexstates,i=1;
while(*x!=NIL&&hd[hd[*x]]!=hd[$2])i++,x = &tl[*x];
if(*x == NIL)*x = cons(cons(hd[$2],2),NIL);
else tl[hd[*x]] |= 2;
ns = add1(i,ns); }
$$ = ns; };
cnames:
CNAME
= { $$=cons($1,NIL); }|
cnames CNAME
= { if(member($1,$2))
printf("%ssyntax error: repeated name \"%s\" in start conditions\n",
echoing?"\n":"",get_id($2)),
acterror();
$$ = cons($2,$1); };
lpostfix:
/* empty */
= { $$ = -1; }|
LBEGIN CNAME
= { word *x = &lexstates,i=1;
while(*x!=NIL&&hd[hd[*x]]!=$2)i++,x = &tl[*x];
if(*x == NIL)*x = cons(cons($2,1),NIL);
else tl[hd[*x]] |= 1;
$$ = i;
}|
LBEGIN CONST
= { if(!isnat($2)||get_int($2)!=0)
syntax("%begin not followed by IDENTIFIER or 0\n");
$$ = 0; };
lexdefs:
lexdefs LEXDEF indent '=' re outdent
= { lexdefs = cons(cons($2,$5),lexdefs); }|
/* empty */
= { lexdefs = NIL; };
re: /* regular expression */
re1 '|' re
{ $$ = ap2(LEX_OR,$1,$3); }|
re1;
re1:
lterm '/' lterm
{ $$ = ap2(LEX_RCONTEXT,$1,$3); }|
lterm '/'
{ $$ = ap2(LEX_RCONTEXT,$1,0); }|
lterm;
lterm:
lfac lterm
{ $$ = ap2(LEX_SEQ,$1,$2); }|
lfac;
lfac:
lunit '*'
{ if(e_re($1))
syntax("illegal regular expression - arg of * matches empty\n");
$$ = ap(LEX_STAR,$1); }|
lunit '+'
{ $$ = ap2(LEX_SEQ,$1,ap(LEX_STAR,$1)); }|
lunit '?'
{ $$ = ap(LEX_OPT,$1); }|
lunit;
lunit:
'(' re ')'
= { $$ = $2; }|
CONST
= { if(!isstring($1))
printf("%ssyntax error - unexpected token \"",
echoing?"\n":""),
out(stdout,$1),printf("\" in regular expression\n"),
acterror();
$$ = $1==NILS?ap(LEX_STRING,NIL):
tl[$1]==NIL?ap(LEX_CHAR,hd[$1]):
ap(LEX_STRING,$1);
}|
CHARCLASS
= { if($1==NIL)
syntax("empty character class `` cannot match\n");
$$ = tl[$1]==NIL?ap(LEX_CHAR,hd[$1]):ap(LEX_CLASS,$1); }|
ANTICHARCLASS
= { $$ = ap(LEX_CLASS,cons(ANTICHARCLASS,$1)); }|
'.'
= { $$ = LEX_DOT; }|
name
= { word x=lexdefs;
while(x!=NIL&&hd[hd[x]]!=$1)x=tl[x];
if(x==NIL)
printf(
"%ssyntax error: undefined lexeme %s in regular expression\n",
echoing?"\n":"",
get_id($1)),
acterror();
else $$ = tl[hd[x]]; };
name: NAME|CNAME;
qualifiers:
exp
= { $$ = cons(cons(GUARD,$1),NIL); }|
generator
= { $$ = cons($1,NIL); }|
qualifiers ';' generator
= { $$ = cons($3,$1); }|
qualifiers ';' exp
= { $$ = cons(cons(GUARD,$3),$1); };
generator:
e1 ',' generator
= { /* fix syntax to disallow patlist on lhs of iterate generator */
if(hd[$3]==GENERATOR)
{ word e=tl[tl[$3]];
if(tag[e]==AP&&tag[hd[e]]==AP&&
(hd[hd[e]]==ITERATE||hd[hd[e]]==ITERATE1))
syntax("ill-formed generator\n"); }
$$ = cons(REPEAT,cons(genlhs($1),$3)); idsused=NIL; }|
generator1;
generator1:
e1 LEFTARROW exp
= { $$ = cons(GENERATOR,cons(genlhs($1),$3)); idsused=NIL; }|
e1 LEFTARROW exp ',' exp DOTDOT
= { word p = genlhs($1); idsused=NIL;
$$ = cons(GENERATOR,
cons(p,ap2(irrefutable(p)?ITERATE:ITERATE1,
lambda(p,$5),$3)));
};
defs:
def|
defs def;
def:
v act2 indent '=' here rhs outdent
= { word l = $1, r = $6;
word f = head(l);
if(tag[f]==ID&&!isconstructor(f)) /* fnform defn */
while(tag[l]==AP)r=lambda(tl[l],r),l=hd[l];
r = label($5,r); /* to help locate type errors */
declare(l,r),lastname=l; }|
spec
= { word h=reverse(hd[$1]),hr=hd[tl[$1]],t=tl[tl[$1]];
while(h!=NIL&&!SYNERR)specify(hd[h],t,hr),h=tl[h];
$$ = cons(nill,NIL); }|
ABSTYPE here typeforms indent WITH lspecs outdent
= { extern word TABSTRS;
extern char *dicp,*dicq;
word x=reverse($6),ids=NIL,tids=NIL;
while(x!=NIL&&!SYNERR)
specify(hd[hd[x]],cons(tl[tl[hd[x]]],NIL),hd[tl[hd[x]]]),
ids=cons(hd[hd[x]],ids),x=tl[x];
/* each id in specs has its id_type set to const(t,NIL) as a way
of flagging that t is an abstract type */
x=reverse($3);
while(x!=NIL&&!SYNERR)
{ word shfn;
decltype(hd[x],abstract_t,undef_t,$2);
tids=cons(head(hd[x]),tids);
/* check for presence of showfunction */
(void)strcpy(dicp,"show");
(void)strcat(dicp,get_id(hd[tids]));
dicq = dicp+strlen(dicp)+1;
shfn=name();
if(member(ids,shfn))
t_showfn(hd[tids])=shfn;
x=tl[x]; }
TABSTRS = cons(cons(tids,ids),TABSTRS);
$$ = cons(nill,NIL); }|
typeform indent act1 here EQEQ type act2 outdent
= { word x=redtvars(ap($1,$6));
decltype(hd[x],synonym_t,tl[x],$4);
$$ = cons(nill,NIL); }|
typeform indent act1 here COLON2EQ construction act2 outdent
= { word rhs = $6, r_ids = $6, n=0;