File: yorick.y

package info (click to toggle)
yorick 1.4-14
  • links: PTS
  • area: main
  • in suites: potato
  • size: 5,948 kB
  • ctags: 6,609
  • sloc: ansic: 63,898; yacc: 889; makefile: 605; sh: 65; lisp: 60; fortran: 19
file content (1255 lines) | stat: -rw-r--r-- 37,829 bytes parent folder | download | duplicates (3)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
/*
    YORICK.Y

    YACC description of Yorick grammar.

    $Id$
 */
/*    Copyright (c) 1994.  The Regents of the University of California.
                    All rights reserved.  */

/*--------------------------------------------------------------------------*/
/* Begin C declarations required for actions */
%{

#include "parse.h"
#include "defstr.h"

/* The HP yacc needs this to avoid stack overflow errors --
   The default max stack depth of 150 only allows about 12 else-if
   clauses before it blows.  If this sort of problem occurs many other
   platforms, should consider making a generic solution in sysdep.h.  */
#define __RUNTIME_YYMAXDEPTH

extern int ypSkipIncludes;  /* in yinput.c */

/* ScanForFunc is a "pseudo-parser" which merely calls yylex until
   it finds a func, struct, or extern definition for fname.  It returns
   the location of the line on which the definition began, suitable
   for use with fseek.  */
extern long ScanForFunc(const char *fname, int notExtern);
extern long YpStandby(void);
extern char *YpLitName(Literal);

#ifdef DO_DEBUG
#include <stdio.h>
#define YYDEBUG 1
/* must also set yydebug non-0 */
#endif

#ifdef YY_PROTOTYPES
extern int yylex(void);
extern void yyerror(char *s);
#endif

/* The Yorick grammar requires two "lexical tie-ins"-- both alter the
   token returned by yylex when an end-of-line (EOL) is encountered:
   (1) If parentheses () and square brackets [] are balanced, then
       EOL returns a ';' token, unless the previous token was ';'.
       If the token immediately before EOL was an operator which needs
       a right operand, yylex treats it as if there were an open
       parenthesis-- no ';' token is generated.
   (2) If there are no open control structures (func or struct definitions,
       if, while, do, for or compound statements), then EOL returns the
       YACC end-of-file (EOF) token.  This is applied after rule (1).
 */
static int nOpenCS;     /* The openCS rule increments nOpenCS, while the
			   action when the control structure is complete
			   decrements it.  */
static int parenDepth;  /* The openP rule increments parenDepth; the
			   closeP rule decrements it.  */
static int braceDepth;  /* The openC rule increments braceDepth; the
			   closeC rule decrements it.  */
static int needOperand; /* set if previous token needs right operand */
static int supressSemi; /* set to prevent yylex from returning ';' at EOL
			   -- used for if, while, do, and for */
static int needBody;    /* set if looking for open curly brace */
static int keyCount;    /* key_expr state information */

/* A bizzare lexical tie-in is required for graphics routines which
   need the textual form of their arguments to generate default
   legend strings...  */
static int nQuines, quining;
static void BeginQuine(void);
static void GrabQuine(char *stop);
static int DoQuine(void);

static int EndOfLine(void);

%}
/* End C declarations required for actions */
/*--------------------------------------------------------------------------*/



/*--------------------------------------------------------------------------*/
/* Begin YACC symbol declarations */

%union {
  long l;
  double d;
  Quote q;
  int i;            /* counter or index */
  CodeBlock cb;     /* code block index (see parse.c) */
  Literal lit;      /* literal table index (see parse.c) */
  CBorLit def;      /* CodeBlock or Literal for assign_predicate */
}

%token <l> CHAR SHORT INT LONG CHAR_ERR
%token <d> FLOAT DOUBLE IMAGINARY
%token <q> STRING STRING_ERR SYSCALL
%token <lit> NAME
%token <i> RFNAME
%token FUNC STRUCT
%token IF ELSE WHILE DO FOR GOTO CONTINUE BREAK RETURN EXTERN LOCAL

%token DOTDOT NEXT_ARG MORE_ARGS

/* operators from lowest to highest precedence */
%right '=' PEQ MEQ TMSEQ DIVEQ MODEQ SHLEQ SHREQ ANDEQ XOREQ OREQ
%left OR
%left AND
%left '|'
%left '~'
%left '&'
%left EQ NE
%left '<' '>' LE GE
%left SHL SHR
%left '+' '-'
%left '*' '/' '%'
%right '^'
%right UNARY   /* fictitious terminal for unary operator precedence */
%right '!'
%left '.' ARROW
%nonassoc PLUSPLUS MINUSMINUS

%token NOINPUT

%type <i> assign_op pointer range_func
%type <i> arg_list func_arg_list lj_arg_list rj_arg_list expr_list
%type <def> assign_predicate

%type <cb> compound statement_list statement nil_statement get_pc
%type <cb> assign_expr range_expr simple_range cond_expr expr
%type <cb> for_expr assign_expr_list unary_expr term pm_expr basic_expr
%type <cb> constant variable struct_name member rfname
%type <cb> nil_expr key_expr

/* End YACC symbol declarations */
/*--------------------------------------------------------------------------*/



/*--------------------------------------------------------------------------*/
/* Begin YACC grammar rules */
%%

program_unit:                       /* function, structure, or main program */
    NOINPUT
  | main_program
           { YpFunc(1, 1); }

  | openCS FUNC func_name compound
           { nOpenCS--; YpFunc(0, EndOfLine()); }
  | openCS FUNC func_name openP param_list closeP_wait compound
           { nOpenCS--; YpFunc(0, EndOfLine()); }
  | openCS STRUCT struct_name openC declar_list closeC
           { nOpenCS--; YpStruct($3, EndOfLine()); }

  | openCS FUNC func_name openP error closeP compound
           { nOpenCS--; YpFunc(0, EndOfLine()); }
  | openCS FUNC func_name error
           { nOpenCS--; needBody=0; YpFunc(0, EndOfLine()); }
  | openCS STRUCT struct_name openC error closeC
           { nOpenCS--; YpStruct($3, EndOfLine()); }
  | openCS STRUCT error
           { nOpenCS--; needBody=0; YpStruct(NONE, EndOfLine()); }
  ;

main_program:
    statement
  | EXTERN RFNAME ';'            { YpSpecial($2); }
  | main_program statement
  | error ';'
  ;

/* Every rule in which openCS occurs MUST do nOpenCS--; in its action.
   Should come BEFORE the token which introduces the control structure,
   so that when the introductory token becomes the look-ahead token,
   the lexical tie-in is activated.
   IMPORTANT-- every rule containing openCS must have a corresponding
   error rule so that nOpenCS is adjusted correctly when a syntax
   error is detected.  */
openCS: /* empty */ { nOpenCS++; } ;

openP: '('       { parenDepth++; } ;
closeP: ')'      { parenDepth--; } ;
openB: '['       { parenDepth++; } ;
closeB: ']'      { parenDepth--; } ;

/* Open and close curly braces can be optionally followed by a semicolon
   in order to allow for automatically generated semicolons if the line
   ends at that point.  The close curly brace should allow for an
   optional actual semicolon it any case.  */
openC: '{' optional_semi       { braceDepth++; } ;
closeC: '}' optional_semi      { braceDepth--; } ;

optional_semi:
    /* empty */
  | ';'
  ;

/* ------------------------------ */

statement:
    EXTERN extern_vars ';'                   { $$= YpNoop(); }
  | LOCAL local_vars ';'                     { $$= YpNoop(); }

  | SYSCALL ';'                              { $$= YpSyscall($1); }

  | compound

  | assign_expr ';'                          { $$= YpAssignOrPrint($1); }

  | variable { if ((nQuines= YpCallInit($1))) quining= 1; } arg_sep
             lj_arg_list ';'  { nQuines= quining= 0; $$= YpCall($1, $4); }

  | if_test IF openP assign_expr brfalse closeP_wait statement if_done
                              { $$= YpIfElse($4, $7, NONE); }
  | if_test IF openP assign_expr brfalse closeP_wait statement if_done
            ELSE { nOpenCS++; } branch statement
                              { nOpenCS--; $$= YpIfElse($4, $7, $12); }
    /* Note-  yylex sets supressSemi when it detects ELSE */

  | wloop WHILE openP assign_expr brfalse closeP_wait nil_statement
                              { nOpenCS--; $$= YpWhile($4, $7); }
  | dloop DO statement WHILE openP assign_expr closeP ';'
                              { nOpenCS--; $$= YpDo($3, $6); }
  | floop FOR openP for_expr ';' assign_expr brfalse ';' inc_expr closeP_wait
          nil_statement
                              { nOpenCS--; $$= YpFor($4, $6, $11); }
  | floop FOR openP for_expr ';' ';' inc_expr closeP_wait nil_statement
                              { nOpenCS--; $$= YpFor($4, NONE, $9); }

  | CONTINUE ';'              { $$= YpContinue(); }
  | BREAK ';'                 { $$= YpBreak(); }
  | RETURN ';'                { $$= YpReturn(NONE); }
  | RETURN assign_expr ';'    { $$= YpReturn($2); }
  | RETURN range_expr ';'     { $$= YpReturn($2); }
  | GOTO NAME ';'             { $$= YpGoto($2); }
  | NAME lab_delim            { $$= YpLabel($1); }

  | if_test IF openP error closeP            { nOpenCS--; $$= YpReset(); }
  | if_test IF error                         { nOpenCS--; $$= YpReset(); }
  | wloop WHILE openP error closeP           { nOpenCS--; $$= YpReset(); }
  | wloop WHILE error                        { nOpenCS--; $$= YpReset(); }
  | dloop DO statement WHILE openP error closeP
                                             { nOpenCS--; $$= YpReset(); }
  | dloop DO statement WHILE error           { nOpenCS--; $$= YpReset(); }
  | dloop DO error ';'                       { nOpenCS--; $$= YpReset(); }
          /* DO: note that an error is also a statement */
  | floop FOR openP error closeP             { nOpenCS--; $$= YpReset(); }
  | floop FOR error                          { nOpenCS--; $$= YpReset(); }

  /*  | error ';'                                { $$= YpReset(); }
     If an error is a statement, there is no graceful way to recover
     from an error after DO but before WHILE.  Instead, better error
     recovery within a compound statement allows the parse to continue.  */
  ;

nil_statement:
    ';'                                      { $$= YpNoop(); }
  | statement
  ;

compound:                                             /* compound statement */
    openC closeC                             { $$= YpNoop(); }
  | openC statement_list closeC              { $$= $2; }
  | openC error closeC                       { $$= YpReset(); }
  | openC error statement_list closeC        { $$= YpReset(); }
  ;

statement_list:                               /* list of several statements */
    statement
  | statement_list statement
  ;

branch:
    /* empty */     { YpBranch(0); }
  ;

brfalse:
    /* empty */     { YpBranch(1); }
  ;

brtrue:
    /* empty */     { YpBranch(2); }
  ;

branch_pop:
    /* empty */     { YpBranch(3); }
  ;

closeP_wait:
    { supressSemi= 1; } closeP
  ;

if_test:
    /* openCS */    { supressSemi= 1; nOpenCS++; }
  ;

if_done:
    /* openCS */    { nOpenCS--; }
  ;

wloop:
    /* openCS */    { supressSemi= 1; nOpenCS++; YpLoop(0); }
  ;

dloop:
    /* openCS */    { supressSemi= 1; nOpenCS++; YpLoop(1); }
  ;

floop:
    /* openCS */    { supressSemi= 1; nOpenCS++; YpLoop(2); }
  ;

lab_delim:  { supressSemi= 1; } ':' ;

/* ------------------------------ */

declar_list:        /* list of member declarations for structure definition */
    data_type declarators ';'
  | declar_list data_type declarators ';'
  ;

data_type:
    NAME             { YpDataType($1); }
  ;

declarators:                         /* list of member names and dimensions */
    declarator
  | declarators ',' declarator
  ;

declarator:                           /* one member name and its dimensions */
    pointer member                           { YpDeclarator($1, $2, 0); }
  | pointer member openP expr_list closeP    { YpDeclarator($1, $2, $4); }

  | pointer member openP error closeP        { YpReset(); }
  ;

pointer:              /* all levels of indirection are equivalent in Yorick */
    /* empty */   { $$= 0; }
  | pointer '*'   { $$= $1+1; }
  ;

expr_list:  /* list of non-nil expressions for declarator dimensions, [] op */
    expr                  { $$= 1; }
  | simple_range          { $$= 1; }
  | expr_list ',' expr    { $$= $1 + 1; }
  ;

member:  NAME     { $$= YpQuoted($1); }
  ;

func_name:  NAME         { needBody= 1; YpInitFunc($1); }
  ;

struct_name:  { needBody= 1; } variable   { $$= $2; }
  ;

/* ------------------------------ */

param_list:                 /* dummy parameter list for function definition */
    positionals
  | positionals ',' keywords
  | keywords
  | dot_dot
  | positionals ',' dot_dot
  | positionals ',' dot_dot ',' keywords
  | dot_dot ',' keywords 
  ;

positionals:                           /* positional part of parameter list */
    NAME                          { YpPosParam($1, 0); }
  | positionals ',' NAME          { YpPosParam($3, 0); }
  | '&' NAME                      { YpPosParam($2, 1); }
  | positionals ',' '&' NAME      { YpPosParam($4, 1); }
  ;

keywords:                                 /* keyword part of parameter list */
    NAME '='                      { YpKeyParam($1); }
  | keywords ',' NAME '='         { YpKeyParam($3); }
  ;

dot_dot:
    DOTDOT                        { YpHasPosList(); }
  ;

/* ------------------------------ */

arg_list: /* turn off argument quining for function calls or array indexing */
    { if (nQuines) quining++; } func_arg_list
                                      { $$= $2; if (nQuines) quining--; }
  ;

arg_sep: { if (quining==1) BeginQuine(); } ',' ;

func_arg_list:                            /* actual parameter or index list */
    lj_arg_list
  | rj_arg_list
  | lj_arg_list arg_sep rj_arg_list   { $$= $1 + $3; }
  ;

lj_arg_list:                    /* left justified part of actual index list */
    key_expr                          { $$= keyCount; YpCheckRef($1); }
  | lj_arg_list arg_sep key_expr      { $$= $1 + keyCount; YpCheckRef($3); }
  ;

rj_arg_list:                   /* right justified part of actual index list */
    dotdot                            { $$= 1; }
  | dotdot ',' lj_arg_list            { $$= 1 + $3; }
  ;

dotdot:
    DOTDOT      { YpDotDot(0); }
  | '*'         { YpDotDot(1); }
  ;

key_expr:                     /* one actual positional or keyword parameter */
    nil_expr                     { $$= $1; keyCount= 1 + DoQuine(); }
  | range_expr                   { $$= $1; keyCount= 1 + DoQuine(); }
  | NAME '=' nil_expr            { $$= $3; keyCount= 2; YpKeyword($1, $3); }
  | NAME '=' range_expr          { $$= $3; keyCount= 2; YpKeyword($1, $3); }
  ;

nil_expr:            /* non-assignment/range/ternary expression, or nothing */
    /* empty */                       { $$= YpNil(); }
  | expr
  ;

range_expr:
    simple_range
  | range_func                          { $$= YpRangeFunc($1, NONE); }
  | range_func ':'                      { $$= YpRangeFunc($1, NONE); }
  | range_func ':' simple_range         { $$= YpRangeFunc($1, $3); }
  ;

simple_range:
    nil_expr ':' nil_expr               { $$= YpRange($1, 0); }
  | nil_expr ':' nil_expr ':' nil_expr  { $$= YpRange($1, 1); }
  ;

range_func:
    RFNAME
  | '+'        { $<i>$= 14; }
  | '-'        { $<i>$= 15; }
  ;

extern_vars:                               /* arguments to EXTERN statement */
    NAME                          { YpExtern($1); }
  | extern_vars ',' NAME          { YpExtern($3); }
  ;

local_vars:                                 /* arguments to LOCAL statement */
    NAME                          { YpLocal($1); }
  | local_vars ',' NAME           { YpLocal($3); }
  ;

/* ------------------------------ */

assign_expr:                          /* expression or assignment statement */
    cond_expr
  | assign_predicate assign_expr      { $$= YpAssign($1, $2); }
  | assign_predicate range_expr       { $$= YpAssign($1, $2); }
  | unary_expr assign_op assign_expr  { $$= YpIncrement($1, $2, $3); }
  ;

assign_predicate:
    unary_expr '='                    { $$= YpCheckDefine($1); }
  ;

assign_op:            /* increment operator += -= *= /= %= <<= >>= &= ~= |= */
    PEQ       { $$= 0; }
  | MEQ       { $$= 1; }
  | TMSEQ     { $$= 2; }
  | DIVEQ     { $$= 3; }
  | MODEQ     { $$= 4; }
  | SHLEQ     { $$= 5; }
  | SHREQ     { $$= 6; }
  | ANDEQ     { $$= 7; }
  | XOREQ     { $$= 8; }
  | OREQ      { $$= 9; }
  ;

/* ------------------------------ */

cond_expr:               /* ternary operator -- not necessarily implemented */
    expr
  | expr openCS '?' brfalse expr ':' branch_pop cond_expr
                              { nOpenCS--; $$= YpTernop($1, $5, $8); }
  | expr openCS '?' error
                              { nOpenCS--; $$= YpReset(); }
  ;

/* ------------------------------ */

expr:   /* expression not involving assignment, index range, or ?: operator */
    pm_expr

  | expr '*' expr      { $$= YpMultop($1); }
  | expr '/' expr      { $$= YpBinop($1, 2); }
  | expr '%' expr      { $$= YpBinop($1, 3); }

  | expr '+' expr      { $$= YpBinop($1, 4); }
  | expr '-' expr      { $$= YpBinop($1, 5); }

  | expr SHL expr      { $$= YpBinop($1, 6); }
  | expr SHR expr      { $$= YpBinop($1, 7); }

  | expr '<' expr      { $$= YpBinop($1, 8); }
  | expr '>' expr      { $$= YpBinop($1, 9); }
  | expr LE expr       { $$= YpBinop($1, 10); }
  | expr GE expr       { $$= YpBinop($1, 11); }
  | expr EQ expr       { $$= YpBinop($1, 12); }
  | expr NE expr       { $$= YpBinop($1, 13); }

  | expr '&' expr      { $$= YpBinop($1, 14); }
  | expr '~' expr      { $$= YpBinop($1, 15); }
  | expr '|' expr      { $$= YpBinop($1, 16); }

  | expr AND brfalse expr     { $$= YpLogop($1, 0, $4); }
  | expr OR brtrue expr       { $$= YpLogop($1, 1, $4); }
  ;

pm_expr:
    term
  | '+' term           { $$= YpUnop(2, $2); }
  | '-' term           { $$= YpUnop(3, $2); }
  ;

term:
    unary_expr
  | unary_expr '^' expr      { $$= YpBinop($1, 0); }
  ;

unary_expr:                /* one term, possibly preceded by unary operator */
    basic_expr

  | '*' unary_expr  %prec UNARY  { $$= YpUnop(0, $2); }
  | '&' basic_expr  %prec UNARY  { YpCheckRef($2); $$= YpUnop(1, $2); }

  | '~' basic_expr  %prec UNARY  { $$= YpUnop(4, $2); }
  | '!' basic_expr  %prec UNARY  { $$= YpUnop(5, $2); }

  | PLUSPLUS basic_expr   %prec UNARY  { $$= YpUnop(6, YpCheckDefine($2)); }
  | MINUSMINUS basic_expr %prec UNARY  { $$= YpUnop(7, YpCheckDefine($2)); }
  ;

basic_expr:                     /* one term, including any postfix operator */
    constant
  | variable

  | basic_expr openP { YpEvalInit($1); } arg_list closeP
                                          { $$= YpEval($1, $4); }
  | rfname openP arg_list closeP          { $$= YpEval($1, $3); }
  | NEXT_ARG openP closeP                 { $$= YpNextArg(0); }
  | MORE_ARGS openP closeP                { $$= YpNextArg(1); }
  | openB get_pc expr_list closeB         { $$= YpBuild($2, $3); }
  | openB closeB                          { $$= YpNil(); }
    /* Note-- cant use curly braces similarly, because, e.g.-
       { variable, arg1, arg2, arg3, arg4 }     and
       { variable, arg1, arg2, arg3, arg4; }
       could not be distinguished until the final semicolon (the second
       is a compound statement with a single subroutine call, the first
       would be some kind of list composed of 5 objects)  */

  | basic_expr '.' NAME            { $$= YpMember($1, 0, $3); }
  | basic_expr ARROW NAME          { $$= YpMember($1, 1, $3); }

  | basic_expr PLUSPLUS            { $$= YpPostfix(YpCheckDefine($1), 0); }
  | basic_expr MINUSMINUS          { $$= YpPostfix(YpCheckDefine($1), 1); }

  | openP assign_expr closeP       { $$= $2; }

  | basic_expr openP error closeP     { $$= YpReset(); }
  | rfname openP error closeP         { $$= YpReset(); }
  | openB error closeB                { $$= YpReset(); }
  | openP error closeP                { $$= YpReset(); }
  ;

/* ------------------------------ */

get_pc:  /* return current pc */    { $$= YpNoop(); }
  ;

for_expr:                            /* initialize section of FOR statement */
    /* empty */                         { $$= YpNoop(); }
  | assign_expr_list
  ;

inc_expr:                             /* increment section of FOR statement */
    /* empty */
  | { YpBeginInc(); } assign_expr_list  { YpEndInc($2); }
  ;

assign_expr_list:
    assign_expr                         { $$= $1; YpDrop(); }
  | assign_expr_list ',' assign_expr    { $$= $1; YpDrop(); }
  ;

/* ------------------------------ */

variable:  NAME   { $$= YpVariable($1); }
  ;

rfname:  RFNAME   { $$= YpPushRF($1); }
  ;

constant:
    CHAR          { $$= YpChar($1); }
  | SHORT         { $$= YpShort($1); }
  | INT           { $$= YpInt($1); }
  | LONG          { $$= YpLong($1); }
  | FLOAT         { $$= YpFloat($1); }
  | DOUBLE        { $$= YpDouble($1); }
  | STRING        { $$= YpString($1); }
  | IMAGINARY     { $$= YpImaginary($1); }
  ;

%%
/* End YACC grammar rules */
/*--------------------------------------------------------------------------*/



/*--------------------------------------------------------------------------*/
/* Begin C code */

static char *curLine, *nextChar= 0;
static int curChar;
static char *prevToken;

static int prevWasSemi, prevWasComma, noInputYet;
static char *quineBegin, *quineEnd, *quineText;

static int EndOfLine(void);

/* CheckMulti checks for multi-character special symbols.
   In the "multis" list, CheckMulti assumes that any 3 character
   symbol comes immediately after a matching 2 character symbol, AND
   no other symbols with the same FIRST character follow.
   More than one 3-symbol may not match a 2-symbol, and symbols of
   more than 3 characters are not allowed.  */
static int CheckMulti(void);

/* CheckName checks for reserved words and range function names.
   The "reserved" list must be alphabetized.  */
static int CheckName(char *name);

static char *AppendString(char *s, long ns, char *t, long nt);
static int StringValue(void);
static int CharValue(void);
static int NumberValue(void);

/* CheckSyscall checks for $ system escape sequence */
static int CheckSyscall(void);

/* ------------------------------ */

int YpParse(void *func)
{
  YpParseInit(func);
  nOpenCS= 0;    /* initialize number of open control structures */
  parenDepth= 0; /* initialize number of open parentheses/square brackets */
  braceDepth= 0; /* initialize number of open curly braces */
  needOperand= prevWasSemi= prevWasComma= supressSemi= 0; /* more EOL flags */
  nQuines= quining= 0;
  ypSkipIncludes= 0;  /* see ScanForFunc */
  curLine= nextChar= YpNextLine(NL_MAIN);
  prevToken= 0;
  if (!nextChar) return 0;
  noInputYet= 1;

  /* Note: If YACC does not generate a prototype for yyparse, it may
     be slightly dangerous to do so here (e.g.- Could it be a macro?
     Is it "int yyparse(void)" or "int yyparse()"?)... */
  return yyparse();            /* invoke YACC generated parser */
}

/* ------------------------------ */

static char *multis[]= { "!=", "%=", "&&", "&=", "*=", "++", "+=",
  "--", "-=", "->", "..", "/=", "<<", "<<=", "<=", "==", ">=", ">>", ">>=",
  "|=", "||", "~=", "" };

static int multiTypes[]= { NE, MODEQ, AND, ANDEQ, TMSEQ, PLUSPLUS, PEQ,
  MINUSMINUS, MEQ, ARROW, DOTDOT, DIVEQ, SHL, SHLEQ, LE, EQ, GE, SHR, SHREQ,
  OREQ, OR, XOREQ };

static char *reserved[]= { "avg", "break", "continue", "cum", "dif", "do",
  "else", "extern", "for", "func", "goto", "if", "local", "max", "min", "mnx",
  "more_args", "mxx", "next_arg", "pcen", "psum", "ptp", "return", "rms",
  "struct", "sum", "uncp", "while", "zcen", "" };

static int reservedTypes[]= { RFNAME, BREAK, CONTINUE, RFNAME, RFNAME, DO,
  ELSE, EXTERN, FOR, FUNC, GOTO, IF, LOCAL, RFNAME, RFNAME, RFNAME,
  MORE_ARGS, RFNAME, NEXT_ARG, RFNAME, RFNAME, RFNAME, RETURN, RFNAME,
  STRUCT, RFNAME, RFNAME, WHILE, RFNAME };

static int reservedRFs[]= { 0, -1, -1, 1, 2, -1,
  -1, -1, -1, -1, -1, -1, -1, 3, 4, 5,
  -1, 6, -1, 7, 8, 9, -1, 10, -1, 11,
  12, -1, 13 };

/* ------------------------------ */

static int EndOfLine(void)
{
  if (!nextChar) return 1;
  while ((curChar= *nextChar) && (curChar==' ' || curChar=='\t'))
    nextChar++;        /* skip over tabs and blanks */
  return !curChar;
}

static int CheckMulti(void)
{
  char **now= multis;
  char *cur= *now;
  int token;

  token= curChar;
  nextChar++;
  while (cur[0] && curChar!=cur[0]) cur= *(++now);

  if (*cur++) {
    /* first character of symbol matches at least one multi-symbol */
    int second= *nextChar;

    do {
      if (second == *cur) {
	/* second character of symbol matches at least one multi-symbol */
	nextChar++;
	now++;
	if (curChar==(*now)[0] && *nextChar && *nextChar==(*now)[2]) {
	  nextChar++;        /* found 3 character multi-symbol */
	} else {
	  now--;             /* found 2 character multi-symbol */
	}
	token= multiTypes[now-multis];
	break;
      }
      cur= *(++now);
    } while (curChar == (cur++)[0]);

    if (token!=DOTDOT && token!=PLUSPLUS && token!=MINUSMINUS)
      needOperand= 1;

  } else if (curChar=='^') {
    /* Caret is the only operator which is NOT the first character of
       at least one multi-symbol.  */
    needOperand= 1;

  } else if (curChar==',') {
    if (quining==1) quineEnd= nextChar-1;
    prevWasComma= 1;
  } else if (needBody && curChar=='{') {
    needBody= 0;  /* no need to supress semicolon on EOL any more */
  }

  return token;
}

static int CheckName(char *name)
{
  char *now= name;
  int c= *now++;

  if (c>='a' && c<='z') { /* from "avg" to "zcen" NOT a to z! */
    char *match, **lst, **fst= reserved;
    int i= 1;

    /* find position of 1st character of name in reserved list */
    while (**fst<c) fst++;
    lst= fst;
    while (**lst==c) lst++;
    lst--;

    /* if first character matches, procede with following characters */
    while (fst <= lst) {
      if (fst==lst) {
	match= &(*fst)[i];   /* only one possible match */
	while (now<nextChar && match[0]==now[0]) { match++; now++; }
	if (*match || now!=nextChar) break;

	yylval.i= reservedRFs[fst-reserved];
	if (reservedTypes[fst-reserved]==ELSE) supressSemi= 1;
	return reservedTypes[fst-reserved];     /* found a reserved word */

      }
      c= *now++;
      while (fst<=lst && (*fst)[i]<c) fst++;
      while (fst<=lst && (*lst)[i]>c) lst--;
      i++;
    }
  }

  yylval.lit= YpName(name, nextChar-name);
  return NAME;             /* NOT a reserved word, just an ordinary name */
}

/* ------------------------------ */

static char *AppendString(char *s, long ns, char *t, long nt)
{
  char *new= StrAlloc(ns+nt);
  if (ns) strcpy(new, s);
  StrFree(s);
  if (nt) strncpy(new+ns, t, nt);
  new[ns+nt]= '\0';
  return new;
}

static char *quote= 0;  /* for StringValue and CheckSyscall */

static int StringValue(void)
{
  char c, *begin, *next;
  long n, len= 0;

  begin= quote;
  quote= 0;
  StrFree(begin);

  begin= ++nextChar;  /* skip open quote */
  while ((c=*nextChar++)) {
    if (c=='\\') {
      if (*nextChar) {
	nextChar[-1]= YpEscapeSeq(nextChar, &next);
	quote= AppendString(quote, len, begin, nextChar-begin);
	len+= nextChar-begin;
	begin= nextChar= next;

      } else {
	/* escaped newline */
	n= nextChar-begin-1;
	quote= AppendString(quote, len, begin, n);
	len+= n;
	if (quining==1) GrabQuine(nextChar-1);
	curLine= nextChar= begin= prevToken= YpNextLine(NL_QUOTE);
	if (quining==1) quineBegin= quineEnd= nextChar;
	if (!curLine) {
	  if (len) StrFree(quote);
	  YpError("open \" at end-of-file");
	  return 0;                    /* EOF reached (inside string) */
	}
      }

    } else if (c=='\"') {
      /* only legitimate way out is close quote */
      quote= AppendString(quote, len, begin, nextChar-begin-1);
      yylval.q= YpQuoteConst(quote);
      return STRING;
    }
  }

  if (len) {
    begin= quote;
    quote= 0;
    StrFree(begin);
  }
  YpError("missing close \"");
  yylval.q= 0;
  return STRING_ERR;  /* unmatched open quote */
}

static int CharValue(void)
{
  int c= *(++nextChar);
  if (*nextChar) nextChar++;
  if (c=='\\') c= YpEscapeSeq(nextChar, &nextChar);
  if (*nextChar && (*nextChar++ != '\'')) {
    /* skip past matching close quote, if one on current line */
    while (*nextChar && (*nextChar++ != '\''));
    YpError("missing close \'");
    yylval.l= 0;
    return CHAR_ERR;
  }
  yylval.l= c;
  return CHAR;
}

static int NumberValue(void)
{
  char *begin= nextChar;
  int isReal= (curChar=='.');
  long trialValue= 0;
  char testc;

  if (!isReal) {
    /* strtoul does not exist on Suns, but the Sun strtol works
       properly there, hence this switch */
#ifndef NO_STRTOUL
    trialValue= strtoul(begin, &nextChar, 0);
#else
    trialValue= strtol(begin, &nextChar, 0);
#endif
    testc= nextChar[0];

    /* Mac MetroWerks C library strtoul does not advance nextChar
       past all digits if there are too many to represent a number;
       instead, it leaves nextChar==begin, which is a disaster.  Try
       to avert the disaster here. */
    if (begin[0]=='0') {
      if (begin[1]=='x') {
	while ((testc>='0' && testc<='9') ||
	       (testc>='A' && testc<='F') ||
	       (testc>='a' && testc<='f')) testc= *(++nextChar);
      } else {
	while (testc>='0' && testc<='7') testc= *(++nextChar);
      }
    } else {
      while (testc>='0' && testc<='9') testc= *(++nextChar);
    }

    if (testc=='.') {
      isReal= 1;
    } else if (testc=='e' || testc=='E') {
      int i= 1;
      if (nextChar[1]=='+' || nextChar[1]=='-') i= 2;
      if (nextChar[i]>='0' && nextChar[i]<='9') isReal= 1;
    }
  }

  if (isReal) {
    yylval.d= strtod(begin, &nextChar);

    if (nextChar==begin) {
      /* avert MetroWerks disaster described above */
      if (begin[0]=='.') nextChar++;
      testc= nextChar[0];
      while (testc>='0' && testc<='9') testc= *(++nextChar);
    }

    if (*nextChar=='f' || *nextChar=='F') {
      nextChar++;
      return FLOAT;
    } else if (*nextChar=='i' || *nextChar=='I') {
      /* alternative suggestion: 1j for imaginary, 1i for int */
      nextChar++;
      return IMAGINARY;
    } else {
      return DOUBLE;
    }

  } else {
    yylval.l= trialValue;
    if (*nextChar=='l' || *nextChar=='L') {
      nextChar++;
      return LONG;
    } else if (*nextChar=='i' || *nextChar=='I') {
      nextChar++;
      yylval.d= (double)trialValue;
      return IMAGINARY;
    } else if (*nextChar=='s' || *nextChar=='S') {
      nextChar++;
      return SHORT;
    } else if (*nextChar=='n' || *nextChar=='N') {
      /* alternative suggestion: 1j for imaginary, 1i for int */
      nextChar++;
      return INT;
    } else {
      return LONG;
    }
  }
}

/* ------------------------------ */

static int CheckSyscall(void)
{
  long n, len;
  int more;

  if (EndOfLine() || curChar!='$') return 0;
  nextChar++;
  for (n=0 ; nextChar[n] ; n++)
    if (nextChar[n]!=' ' && nextChar[n]!='\t') break;
  if (!nextChar[n]) return 0;

  nextChar+= n;               /* skip past leading whitespace */
  len= 0;

  if (quote) {
    char *prev= quote;
    quote= 0;
    StrFree(prev);
  }
  for (;;) {
    n= strlen(nextChar);
    more= (n && nextChar[n-1]=='\\');
    if (more) n--;

    quote= AppendString(quote, len, nextChar, n);
    len+= n;
    nextChar+= n;

    if (!more) break;
    curLine= nextChar= prevToken= YpNextLine(NL_QUOTE);
    if (!curLine) break;               /* EOF reached */
  }

  yylval.q= YpQuoteConst(quote);
  return 1;
}

/* ------------------------------ */

static void BeginQuine(void)
{
  /* nextChar is just past comma for argument to be quined */
  EndOfLine();   /* skip whitespace */
  if (quineText) StrFree(quineText);
  quineText= 0;
  quineBegin= quineEnd= nextChar;
  if (curChar=='\\' && !nextChar[1]) quineBegin++;
}

static void GrabQuine(char *stop)
{
  long n= stop>quineBegin? stop-quineBegin : 0;
  char *text= StrNCat(quineText, quineBegin, n);
  StrFree(quineText);
  quineText= text;
}

static int DoQuine(void)
{
  if (quining!=1) return 0;
  while (quineEnd>curLine && (quineEnd[-1]==' ' || quineEnd[-1]=='\t'))
    quineEnd--;
  GrabQuine(quineEnd);
  YpString(YpQuoteConst(quineText));
  StrFree(quineText);
  quineText= quineBegin= quineEnd= 0;
  nQuines--;
  if (nQuines==0) quining= 0;
  return 1;
}

/* ------------------------------ */

int yylex(void)
{
  int eol, continuation, bol;
  int supress= parenDepth || needOperand || supressSemi;
  if (noInputYet==2) return 0;

  for (;;) { /* loop past all whitespace, comments, and escaped newlines */

    for (;;) {  /* loop past blank lines */
      bol= (nextChar && nextChar==curLine);
      eol= EndOfLine();
      continuation= (curChar=='\\' && !nextChar[1]);
      bol= (bol && !continuation && !supress);
      if (!eol && !continuation) break;

      if (!continuation && !supress && !noInputYet &&
	  !prevWasComma && !needBody) {
	if (!prevWasSemi) {
	  /* if there are no open parentheses or operators, and the
	     previous token was NOT ';', EOL is same as ';' */
	  if (quining==1) quineEnd= nextChar;
	  prevWasSemi= 1;
	  return ';';
	}
	/* EOL (after ';') is same as EOF if no reason to continue */
	if (!nOpenCS && !braceDepth) return 0;
      }

      if (quining==1) GrabQuine(nextChar);
      curLine= nextChar= prevToken=
	YpNextLine(noInputYet? NL_NOINPUT : NL_CONTINUE);
      if (quining==1) quineBegin= quineEnd= nextChar;

      if (!curLine) {                        /* hit EOF */
	if (noInputYet) {
	  noInputYet= 2;
	  return NOINPUT;
	} else {
	  return 0;
	}
      }
    }

    if (curChar=='/' && nextChar[1]=='/') {
      while (*nextChar) nextChar++;
      continue;
    }

    if (curChar!='/' || nextChar[1]!='*') break;

    nextChar+= 2;                        /* skip past open comment */
    curChar= *nextChar++;
    while (curChar!='*' || *nextChar!='/') {  /* skip comment body */
      if (!curChar) {
	if (quining==1) GrabQuine(nextChar-1);
	curLine= nextChar= prevToken= YpNextLine(NL_COMMENT);
	if (quining==1) quineBegin= quineEnd= nextChar;
	if (!curLine) {
	  YpError("open comment at end-of-file");
	  return 0;           /* EOF reached (in comment) */
	}
      }
      curChar= *nextChar++;
    }
    nextChar++;
  }

  /* At last!  A real token has arrived! */

  needOperand= supressSemi= prevWasSemi= prevWasComma= 0;

  prevToken= nextChar;
  noInputYet= 0;

  if (curChar=='\"') {
    /* token is quoted string */
    return StringValue();         /* may include escaped newlines */

  } else if (curChar=='\'') {
    /* token is character constant */
    return CharValue();

  } else if ((curChar>='0' && curChar<='9') ||
	     (curChar=='.' && nextChar[1]>='0' && nextChar[1]<='9')) {
    /* token is some sort of number */
    return NumberValue();

  } else {
    /* token is not a number, check for a variable name */
    while ((curChar>='a'&&curChar<='z') || (curChar>='A'&&curChar<='Z') ||
	   (curChar>='0'&&curChar<='9') || curChar=='_')
      curChar= *(++nextChar);

    if (nextChar > prevToken) {
      /* token is a name */
      return CheckName(prevToken);

    } else if (curChar!=';') {
      /* token must be a symbol */
      if (bol && CheckSyscall()) return SYSCALL;
      return CheckMulti();
    } else {
      if (quining==1) quineEnd= nextChar;
      prevWasSemi= 1;
      nextChar++;
      return ';';
    }
  }
}

/* ------------------------------ */

void yyerror(char *msg)
{
  char errorMsg[81];
  long n;

  if (yychar==CHAR_ERR || yychar==STRING_ERR) return;

  n= strlen(msg);
  strncpy(errorMsg, msg, 80);
  if (n>=80) {
    errorMsg[80]= '\0';
  } else if (n>=70) {
    errorMsg[n]= '\0';
  } else {
    msg= errorMsg+n;
    strcpy(msg, " near ");
    msg+= 6;
    n= 80-n;
    if (n>16) n= 16;
    if (yychar==0) {
      strcpy(msg, "<EOF>");
    } else if (!prevToken || *prevToken==0) {
      strcpy(msg, "<EOL>");
    } else if (yychar==STRING && *prevToken!='\"') {
      strcpy(msg, "\"...\"");
    } else {
      strncpy(msg, prevToken, n);
      msg[n]= '\0';
    }
  }

  YpError(errorMsg);
}

/* ------------------------------ */

long ScanForFunc(const char *fname, int notExtern)
{
  int type, counter= 0;
  long position= -1;
  int token;
  YpParseInit((void *)0); /* necessary to receive literals and quotes */
  nOpenCS= 0;    /* initialize number of open control structures */
  parenDepth= 0; /* initialize number of open parentheses/square brackets */
  braceDepth= 0; /* initialize number of open curly braces */
  needOperand= prevWasSemi= prevWasComma= supressSemi= 0; /* more EOL flags */
  nQuines= quining= 0;
  /* this time, want to skip any include files and remember address of
     each line before it is read */
  ypSkipIncludes= 1;
  /* Get 1st line in NL_NOINPUT mode.  This is not really too important
     here, but if input were from keyboard, this would force YpNextLine
     to wait for input instead of possibly returning eof to handle
     input from another source.  */
  curLine= nextChar= YpNextLine(NL_NOINPUT);
  prevToken= 0;
  if (!nextChar) return 0;

  for (;;) {
    noInputYet= 1;     /* keep calling YpNextLine(NL_NOINPUT) --
			  assures that YpNextLine will keep track of
			  beginning line number */
    token= yylex();
    if (!token || token==NOINPUT) break;
    if (token=='{') braceDepth++;
    else if (token=='}') braceDepth--;
    else if (!braceDepth) {
      if (token==FUNC) type= 1;
      else if (token==STRUCT) type= 2;
      else if (!notExtern && (token==EXTERN||token==LOCAL)) type= 3;
      else type= 0;
      if (type) {
	position= YpStandby();
	noInputYet= 0;     /* if necessary, YpNextLine(NL_CONTINUE) */
	supressSemi= 1;    /* dont get confused by newline */
	token= yylex();
	supressSemi= 0;
	if (token==NAME && strcmp(fname, YpLitName(yylval.lit))==0) break;
	if (type==3) {
	  while ((token= yylex())==',') {
	    token= yylex();
	    if (token==NAME && strcmp(fname, YpLitName(yylval.lit))==0)
	      goto found;
	  }
	}
	position= -1;
	if (!token || token==NOINPUT) break;
      }
    } else if (token==NAME || token==STRING || token==SYSCALL) {
      /* try to keep the literal and constant tables reasonably small */
      counter++;
      if (counter>=32) { YpParseInit((void *)0); counter= 0; }
    }
  }
 found:

  return position;
}

/* End C code */
/*--------------------------------------------------------------------------*/