summaryrefslogtreecommitdiff
path: root/BaseTools/Source/C/VfrCompile/Pccts/antlr/main.c
blob: 051ee4ec5d28b7b499f8f03aac2fee5b84fb528f (plain)
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
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
/*
 * main.c -- main program for PCCTS ANTLR.
 *
 * SOFTWARE RIGHTS
 *
 * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
 * Set (PCCTS) -- PCCTS is in the public domain.  An individual or
 * company may do whatever they wish with source code distributed with
 * PCCTS or the code generated by PCCTS, including the incorporation of
 * PCCTS, or its output, into commerical software.
 *
 * We encourage users to develop software with PCCTS.  However, we do ask
 * that credit is given to us for developing PCCTS.  By "credit",
 * we mean that if you incorporate our source code into one of your
 * programs (commercial product, research project, or otherwise) that you
 * acknowledge this fact somewhere in the documentation, research report,
 * etc...  If you like PCCTS and have developed a nice tool with the
 * output, please mention that you developed it using PCCTS.  In
 * addition, we ask that this header remain intact in our source code.
 * As long as these guidelines are kept, we expect to continue enhancing
 * this system and expect to make other tools available as they are
 * completed.
 *
 * ANTLR 1.33
 * Terence Parr
 * Parr Research Corporation
 * with Purdue University and AHPCRC, University of Minnesota
 * 1989-2001
 */

/* To set a breakpoint just before exit look for "cleanUp".    */
/* To set a breakpoint for fatal error look for "fatal_intern" */

#include <stdio.h>

#include "pcctscfg.h"
#include "stdpccts.h"

#define MAX_INT_STACK 50
static int istack[MAX_INT_STACK];		/* Int stack */
static int isp = MAX_INT_STACK;

static int DontAcceptFiles = 0;			/* if stdin, don't read files */
static int DontAcceptStdin = 0;			/* if files seen first, don't accept stdin */

static int tnodes_used_in_guard_predicates_etc;     /* MR10 */

		/* C m d - L i n e  O p t i o n  S t r u c t  &  F u n c s */

typedef struct _Opt {
			char *option;
			int  arg;
#ifdef __cplusplus
			void (*process)(...);
#else
			void (*process)();
#endif
			char *descr;
		} Opt;

#ifdef __USE_PROTOS
extern void ProcessArgs(int, char **, Opt *);
#else
extern void ProcessArgs();
#endif

#ifdef __USE_PROTOS
int ci_strequ(char *a,char *b)
#else
int ci_strequ(a,b)
  char  *a;
  char  *b;
#endif
{
  for ( ;*a != 0 && *b != 0; a++, b++) {
    if (toupper(*a) != toupper(*b)) return 0;
  }
  return (*a == *b);
}

static void
#ifdef __USE_PROTOS
pStdin( void )
#else
pStdin( )
#endif
{
	if ( DontAcceptStdin )
	{
		warnNoFL("'-' (stdin) ignored as files were specified first");
		return;
	}

	require(NumFiles<MaxNumFiles,"exceeded max # of input files");
	FileStr[NumFiles++] = "stdin";
	DontAcceptFiles = 1;
}

static void
#ifdef __USE_PROTOS
pFile( char *s )
#else
pFile( s )
char *s;
#endif
{
	if ( *s=='-' ) { warnNoFL( eMsg1("invalid option: '%s'",s) ); return; }
	if ( DontAcceptFiles )
	{
		warnNoFL(eMsg1("file '%s' ignored as '-' (stdin option) was specified first",s));
		return;
	}

	require(NumFiles<MaxNumFiles,"exceeded max # of input files");
	FileStr[NumFiles++] = s;
	DontAcceptStdin = 1;
}

/* MR14
        Allow input to be a file containing a list of files
        Bernard Giroud (b_giroud@decus.ch)
*/

static void
#ifdef __USE_PROTOS
pFileList( char *s, char *t )
#else
pFileList( s, t )
char *s;
char *t;
#endif
{
#define MaxFLArea 1024
	FILE *fl;
	static char Fn_in_Fl[MaxFLArea] = "";
        char one_fn[MaxFileName];
	char *flp = &Fn_in_Fl[0];
	int fnl, left = MaxFLArea, i;

	if ( *t=='-' ) { warnNoFL( eMsg1("invalid option: '%s'",t) ); return; }
	if ( DontAcceptFiles )
	{
		warnNoFL(eMsg1("file '%s' ignored as '-' (stdin option) was specified first",t));
		return;
	}

        if ((fl = fopen(t, "r")) == NULL)
	{
		warnNoFL(eMsg1("file '%s' can't be opened", t));
		return;
	}
        for (;;)
	{
		if (fgets(one_fn, 128 - 1, fl) == NULL)
			break;
		fnl = strlen(one_fn);
		require(fnl<=left, "no more room in File List Area");
		/* drop the trailing LF */
		if (one_fn[fnl - 1] == 0x0a) one_fn[fnl - 1] = ' ';
		strcat(Fn_in_Fl, one_fn);
		left = left - fnl;
		require(NumFiles<MaxNumFiles,"exceeded max # of input files");
		FileStr[NumFiles++] = flp;
		flp = flp + fnl;
	}
	fclose(fl);
	for (i=0;i < MaxFLArea;i++) if (Fn_in_Fl[i] == ' ') Fn_in_Fl[i] = '\0';
	DontAcceptStdin = 1;
}

static void
#ifdef __USE_PROTOS
pLLK( char *s, char *t )
#else
pLLK( s, t )
char *s;
char *t;
#endif
{
	LL_k = atoi(t);
	if ( LL_k <= 0 ) {
		warnNoFL("must have at least one token of lookahead (setting to 1)");
		LL_k = 1;
	}
}

static void
#ifdef __USE_PROTOS
pCk( char *s, char *t )
#else
pCk( s, t )
char *s;
char *t;
#endif
{
	CLL_k = atoi(t);
	if ( CLL_k <= 0 ) {
		warnNoFL("must have at least one token of look-ahead (setting to 1)");
		CLL_k = 1;
	}
}

static void						                                     /* MR6 */
#ifdef __USE_PROTOS
pTab( char *s, char *t )			                        	     /* MR6 */
#else
pTab( s, t )				                    		             /* MR6 */
char *s;							                                 /* MR6 */
char *t;							                                 /* MR6 */
#endif
{								                                     /* MR6 */
	TabWidth = atoi(t);					                             /* MR6 */
	if ( TabWidth < 0 || TabWidth > 8 ) {	            		     /* MR6 */
		warnNoFL("tab width must be between 1 and 8");               /* MR6 */
	  TabWidth=0;						                             /* MR6 */
	}							                                     /* MR6 */
}							                                         /* MR6 */

static int ambAidDepthSpecified=0;                                   /* MR11 */

static void						                                     /* MR11 */
#ifdef __USE_PROTOS
pAAd( char *s, char *t )			                        	     /* MR11 */
#else
pAAd( s, t )				                    		             /* MR11 */
char *s;							                                 /* MR11 */
char *t;							                                 /* MR11 */
#endif
{								                                     /* MR11 */
    ambAidDepthSpecified=1;                                          /* MR11 */
	MR_AmbAidDepth = atoi(t);				                         /* MR11 */
}							                                         /* MR11 */

static void						                                     /* MR11 */
#ifdef __USE_PROTOS
pTreport( char *s, char *t )			                       	     /* MR11 */
#else
pTreport( s, t )				                  		             /* MR11 */
    char *s;							                             /* MR11 */
    char *t;							                             /* MR11 */
#endif
{								                                     /* MR11 */
	TnodesReportThreshold = atoi(t);				                 /* MR11 */
}							                                         /* MR11 */

#ifdef __USE_PROTOS
void chkGTFlag(void)	                                    /* 7-Apr-97 MR1 */
#else
void chkGTFlag()                                            /* 7-Apr-97 MR1 */
#endif
{
	if ( !GenAST )
		warn("#-variable or other AST item referenced w/o -gt option");
}


#ifdef __USE_PROTOS
static void pInfo(char *s, char *t)                         /* MR10 */
#else
static void pInfo(s,t)                                      /* MR10 */
  char  *s;
  char  *t;
#endif
{
  char  *p;
  int   q;
  for (p=t; *p != 0; p++) {
    q=tolower(*p);
    if (q=='t') {
      InfoT=1;
    } else if (q=='p') {
      InfoP=1;
    } else if (q=='m') {
      InfoM=1;
    } else if (q=='o') {
      InfoO=1;
    } else if (q=='0') {
      ; /* nothing */
    } else if (q=='f') {
      InfoF=1;
    } else {
      warnNoFL(eMsgd("unrecognized -info option \"%c\"",(int)*p));
    };
  };
}

#ifdef __USE_PROTOS
static void pCGen(void)	{ CodeGen = FALSE; LexGen = FALSE; }
static void pLGen(void)	{ LexGen = FALSE; }
static void pXTGen(void){ MR_Inhibit_Tokens_h_Gen = TRUE; }
static void pTGen(void)	{ TraceGen = TRUE; }
static void pSGen(void)	{ GenExprSetsOpt = FALSE; }
static void pPrt(void)	{ PrintOut = TRUE; pCGen(); pLGen(); }
static void pPrtA(void)	{ PrintOut = TRUE; PrintAnnotate = TRUE; pCGen(); pLGen(); }
static void pAst(void)	{ GenAST = TRUE; }
static void pANSI(void)	{ GenANSI = TRUE; }
static void pCr(void)	{ GenCR = TRUE; }
static void pNOPURIFY(void)	{ PURIFY = FALSE; }
/*static void pCt(void)	{ warnNoFL("-ct option is now the default"); }*/
static void pLI(void)	{ GenLineInfo = TRUE; GenLineInfoMS = FALSE; } /* MR14 */
static void pLIms(void)	{ GenLineInfo = TRUE; GenLineInfoMS = TRUE; }  /* MR14 */
static void pFr(char *s, char *t) {RemapFileName = t;}
static void pFe(char *s, char *t) {ErrFileName = t;}
static void pFl(char *s, char *t) {DlgFileName = t;}
static void pFm(char *s, char *t) {ModeFileName = t;}
static void pFt(char *s, char *t) {DefFileName = t;}

static void pE1(void)	{ elevel = 1; }
static void pE2(void)	{ elevel = 2; }
static void pE3(void)	{ elevel = 3; }
static void pEGen(void)	{ GenEClasseForRules = 1; }
static void pDL(void)
	{
	    DemandLookahead = 1;
	    if ( GenCC ) {
			warnNoFL("-gk does not work currently in C++ mode; -gk turned off");
			DemandLookahead = 0;
	    }
	}

static void pAA(char *s,char *t) {MR_AmbAidRule = t;}               /* MR11 */
static void pAAm(char *s){MR_AmbAidMultiple = 1;}                   /* MR11 */
static void pGHdr(void)	{ GenStdPccts = 1; }
static void pFHdr(char *s, char *t) { stdpccts = t; pGHdr(); }
static void pW1(void) { WarningLevel = 1; }
static void pNewAST(void) { NewAST = 1; }                           /* MR13 */
static void ptmakeInParser(void) { tmakeInParser = 1; }             /* MR23 */
static void pAlpha(void) { AlphaBetaTrace = 1; }                    /* MR14 */
static void pMR_BlkErr(void) { MR_BlkErr = 1; }                     /* MR21 */
static void pStdout(void) {UseStdout = 1; }		                    /* MR6 */
static void pW2(void) { WarningLevel = 2; }
static void pCC(void) { GenCC = TRUE; }
#else
static void pCGen()	{ CodeGen = FALSE; LexGen = FALSE; }
static void pLGen()	{ LexGen = FALSE; }
static void pXTGen(){ MR_Inhibit_Tokens_h_Gen = TRUE; }             /* MR14 */
static void pTGen()	{ TraceGen = TRUE; }
static void pSGen()	{ GenExprSetsOpt = FALSE; }
static void pPrt()		{ PrintOut = TRUE; pCGen(); pLGen(); }
static void pPrtA()	{ PrintOut = TRUE; PrintAnnotate = TRUE; pCGen(); pLGen(); }
static void pAst()		{ GenAST = TRUE; }
static void pANSI()	{ GenANSI = TRUE; }
static void pCr()		{ GenCR = TRUE; }
static void pNOPURIFY()	{ PURIFY = FALSE; }

/*static void pCt()		{ warnNoFL("-ct option is now the default"); }*/
static void pLI()	    { GenLineInfo = TRUE; GenLineInfoMS = FALSE; }  /* MR14 */
static void pLIms()	    { GenLineInfo = TRUE; GenLineInfoMS = TRUE; }   /* MR14 */
static void pFr(s,t) char *s, *t; {RemapFileName = t;}
static void pFe(s,t) char *s, *t; {ErrFileName = t;}
static void pFl(s,t) char *s, *t; {DlgFileName = t;}
static void pFm(s,t) char *s, *t; {ModeFileName = t;}
static void pFt(s,t) char *s, *t; {DefFileName = t;}

static void pE1()		{ elevel = 1; }
static void pE2()		{ elevel = 2; }
static void pE3()		{ elevel = 3; }
static void pEGen()	{ GenEClasseForRules = 1; }
static void pDL()
	{
	    DemandLookahead = 1;
	    if ( GenCC ) {
          warnNoFL("-gk does not work currently in C++ mode; -gk turned off");
	      DemandLookahead = 0;
	    }
	}

static void pAA(s,t) char *s; char *t; {MR_AmbAidRule = t;}          /* MR11 BJS 20-Mar-98 */
static void pAAm(s) char *s; {MR_AmbAidMultiple = 1;}                /* MR11 BJS 20-Mar-98 */
static void pGHdr()	{ GenStdPccts = 1; }
static void pFHdr(s,t) char *s, *t; { stdpccts = t; pGHdr(); }
static void pW1() { WarningLevel = 1; }
static void pNewAST() { NewAST = 1; }                                /* MR13 */
static void ptmakeInParser() { tmakeInParser = 1; }                  /* MR23 */
static void pAlpha() { AlphaBetaTrace = 1; }                         /* MR14 */
static void pMR_BlkErr() { MR_BlkErr = 1; }                          /* MR21 */
static void pStdout() {UseStdout = 1; }	                             /* MR6 */
static void pW2() { WarningLevel = 2; }
static void pCC() { GenCC = TRUE; }
#endif

static void
#ifdef __USE_PROTOS
pPre( char *s, char *t )
#else
pPre( s, t )
char *s;
char *t;
#endif
{
	RulePrefix = t;
}

static void
#ifdef __USE_PROTOS
pOut( char *s, char *t )
#else
pOut( s, t )
char *s;
char *t;
#endif
{
	OutputDirectory = t;
}

static void
#ifdef __USE_PROTOS
pPred( void )
#else
pPred( )
#endif
{
	warnNoFL("-pr is no longer used (predicates employed if present); see -prc, -mrhoist, -mrhoistk");
/*
**	if ( DemandLookahead )
**		warnNoFL("-gk conflicts with -pr; -gk turned off");
**	DemandLookahead = 0;
**	HoistPredicateContext = 0;
*/
}

static void
#ifdef __USE_PROTOS
pPredCtx( char *s, char *t )
#else
pPredCtx(s,t)
char *s;
char *t;
#endif
{
	if ( ci_strequ(t,"on")) HoistPredicateContext = 1;
	else if ( ci_strequ(t,"off")) HoistPredicateContext = 0;
	if ( DemandLookahead )
	{
		warnNoFL("-gk incompatible with semantic predicate usage; -gk turned off");
		DemandLookahead = 0;
	}
}

static void
#ifdef __USE_PROTOS
pMRhoist( char *s, char *t )
#else
pMRhoist(s,t)
char *s;
char *t;
#endif
{
	if ( ci_strequ(t,"on")) MRhoisting = 1;
	else if ( ci_strequ(t,"off")==0 ) MRhoisting = 0;
    if (MRhoisting) {
        fprintf(stderr,"Maintenance Release style hoisting enabled for predicates with lookahead depth = 1\n");
        fprintf(stderr,"  No longer considered experimental\n");
        fprintf(stderr,"  Can't consider suppression for predicates with lookahead depth > 1\n");
        fprintf(stderr,"  Implies -prc on but does *not* imply -mrhoistk for k>1 predicates\n");
        fprintf(stderr,"  This is a reminder, not a warning or error.\n");
    };
}

static void
#ifdef __USE_PROTOS
pMRhoistk( char *s, char *t )
#else
pMRhoistk(s,t)
char *s;
char *t;
#endif
{
	if ( ci_strequ(t,"on")) MRhoistingk = 1;
	else if ( ci_strequ(t,"off")==0 ) MRhoistingk = 0;
    if (MRhoistingk) {
        fprintf(stderr,"EXPERIMENTAL Maintenance Release style hoisting enabled\n");
        fprintf(stderr,"  Applies to predicates with lookahead depth > 1\n");
        fprintf(stderr,"  Implies -prc on and -mrhoist on\n");
    };
}

static void
#ifdef __USE_PROTOS
pTRes( char *s, char *t )
#else
pTRes( s, t )
char *s;
char *t;
#endif
{
	TreeResourceLimit = atoi(t);
	if ( TreeResourceLimit <= 0 )
	{
		warnNoFL("analysis resource limit (# of tree nodes) must be greater than 0");
		TreeResourceLimit = -1; /* set to no limit */
	}
}

Opt options[] = {
#ifdef __cplusplus
    { "-CC", 0, (void (*)(...)) pCC,	"Generate C++ output (default=FALSE)"},
    { "-ck", 1, (void (*)(...)) pCk,	"Set compressed lookahead depth; fast approximate lookahead"},
    { "-cr", 0, (void (*)(...)) pCr,	"Generate cross reference (default=FALSE)"},
    { "-e1", 0, (void (*)(...)) pE1,	"Ambiguities/errors shown in low detail (default)"},
    { "-e2", 0, (void (*)(...)) pE2,	"Ambiguities/errors shown in more detail"},
    { "-e3", 0, (void (*)(...)) pE3,
    	"Ambiguities for k>1 grammars shown with exact tuples (not lookahead sets)"},
    { "-f",  1, (void (*)(...)) pFileList,"Read names of grammar files from specified file"}, /* MR14 */
    { "-fe", 1, (void (*)(...)) pFe,	"Rename err.c"},
    { "-fh", 1, (void (*)(...)) pFHdr,	"Rename stdpccts.h header (turns on -gh)"},
    { "-fl", 1, (void (*)(...)) pFl,	"Rename lexical output--parser.dlg"},
    { "-fm", 1, (void (*)(...)) pFm,	"Rename mode.h"},
    { "-fr", 1, (void (*)(...)) pFr,	"Rename remap.h"},
    { "-ft", 1, (void (*)(...)) pFt,	"Rename tokens.h"},
    { "-ga", 0, (void (*)(...)) pANSI,	"Generate ANSI-compatible code (default=FALSE)"},
    { "-gc", 0, (void (*)(...)) pCGen,	"Do not generate output parser code (default=FALSE)"},
    { "-gd", 0, (void (*)(...)) pTGen,	"Generate code to trace rule invocation (default=FALSE)"},
    { "-ge", 0, (void (*)(...)) pEGen,	"Generate an error class for each non-terminal (default=FALSE)"},
    { "-gh", 0, (void (*)(...)) pGHdr,	"Generate stdpccts.h for non-ANTLR-generated-files to include"},
    { "-gk", 0, (void (*)(...)) pDL,	"Generate parsers that delay lookahead fetches until needed"},
    { "-gl", 0, (void (*)(...)) pLI,	"Generate line info about grammar actions in parser"},
    { "-glms", 0, (void (*)(...)) pLIms,"Like -gl but replace '\\' with '/' in #line filenames for MS C/C++ systems"},
    { "-gp", 1, (void (*)(...)) pPre,	"Prefix all generated rule functions with a string"},
    { "-gs", 0, (void (*)(...)) pSGen,	"Do not generate sets for token expression lists (default=FALSE)"},
    { "-gt", 0, (void (*)(...)) pAst,	"Generate code for Abstract-Syntax-Trees (default=FALSE)"},
    { "-gx", 0, (void (*)(...)) pLGen,	"Do not generate lexical (dlg-related) files (default=FALSE)"},
    { "-gxt",0, (void (*)(...)) pXTGen,	"Do not generate tokens.h (default=FALSE)"},
    { "-k",  1, (void (*)(...)) pLLK,	"Set full LL(k) lookahead depth (default==1)"},
    { "-o",  1, (void (*)(...)) pOut,	OutputDirectoryOption},
    { "-p",  0, (void (*)(...)) pPrt,	"Print out the grammar w/o actions (default=no)"},
    { "-pa", 0, (void (*)(...)) pPrtA,	"Print out the grammar w/o actions & w/FIRST sets (default=no)"},
    { "-pr",0, (void (*)(...)) pPred,	"no longer used; predicates employed if present"},
    { "-prc", 1, (void (*)(...)) pPredCtx,"Turn on/off computation of context for hoisted predicates"},
   	{ "-rl", 1, (void (*)(...)) pTRes,	"Limit max # of tree nodes used by grammar analysis"},
    { "-stdout",0,  (void (*)(...)) pStdout,"Send grammar.c/grammar.cpp to stdout"},           /* MR6 */
	{ "-tab", 1, (void (*)(...)) pTab,	"Width of tabs (1 to 8) for grammar.c/grammar.cpp files"}, /* MR6 */
	{ "-w1", 0, (void (*)(...)) pW1,	"Set the warning level to 1 (default)"},
	{ "-w2", 0, (void (*)(...)) pW2,	"Ambiguities yield warnings even if predicates or (...)? block"},
	{ "-",   0, (void (*)(...)) pStdin,	"Read grammar from stdin" },
    { "-mrhoist",1, (void (*)(...)) pMRhoist,                                                  /* MR9 */
                                        "Turn on/off k=1 Maintenance Release style hoisting"},  /* MR9 */
    { "-mrhoistk",1, (void (*)(...)) pMRhoistk,                                                  /* MR9 */
                                        "Turn on/off EXPERIMENTAL k>1 Maintenance Release style hoisting"},  /* MR13 */
    { "-aa"  , 1, (void (*)(...)) pAA,  "Ambiguity aid for a rule (rule name or line number)"},          /* MR11 */
    { "-aam" , 0, (void (*)(...)) pAAm,
                                         "Lookahead token may appear multiple times in -aa listing"},    /* MR11 */
    { "-aad" , 1, (void (*)(...)) pAAd,
                                         "Limits exp growth of -aa listing - default=1 (max=ck value)"}, /* MR11 */
	{ "-info", 1, (void (*)(...)) pInfo,
      "Extra info: p=pred t=tnodes f=first/follow m=monitor o=orphans 0=noop"},                          /* MR12 */
    { "-treport",1,(void (*)(...)) pTreport,
                        "Report when tnode usage exceeds value during ambiguity resolution"},            /* MR11 */
	{ "-newAST", 0, (void (*)(...)) pNewAST,
                 "In C++ mode use \"newAST(...)\" rather than \"new AST(...)\""},                        /* MR13 */
	{ "-tmake", 0, (void (*)(...)) ptmakeInParser,
                 "In C++ mode use parser's tmake method rather than \"ASTBase::tmake(...)\""},			 /* MR23 */
    { "-alpha",0,(void (*)(...)) pAlpha,
                 "Provide additional information for \"(alpha)? beta\" error messages"},                 /* MR14 */
    { "-mrblkerr",0,(void (*)(...)) pMR_BlkErr,                                        /* MR21 */
                 "EXPERIMENTAL change to (...)* and (...)+ syntax error sets"},        /* MR21 */
	{ "-nopurify",0,(void (*)(...)) pNOPURIFY,
		"Don't use the notorious PURIFY macro (replaced by MR23 initial value syntax) to zero return arguments of rules"},   /* MR23 */
    { "*",   0, (void (*)(...)) pFile, 	"" },	/* anything else is a file */
#else
    { "-CC", 0, pCC,	"Generate C++ output (default=FALSE)"},
    { "-cr", 0, pCr,	"Generate cross reference (default=FALSE)"},
    { "-ck", 1, pCk,	"Set compressed lookahead depth; fast approximate lookahead"},
    { "-e1", 0, pE1,	"Ambiguities/errors shown in low detail (default)"},
    { "-e2", 0, pE2,	"Ambiguities/errors shown in more detail"},
    { "-e3", 0, pE3,   	"Ambiguities for k>1 grammars shown with exact tuples (not lookahead sets)"},
    { "-f",  1, pFileList,"Read names of grammar files from specified file"},   /* MR14 */
    { "-fe", 1, pFe,	"Rename err.c"},
    { "-fh", 1, pFHdr,	"Rename stdpccts.h header (turns on -gh)"},
    { "-fl", 1, pFl,	"Rename lexical output--parser.dlg"},
    { "-fm", 1, pFm,	"Rename mode.h"},
    { "-fr", 1, pFr,	"Rename remap.h"},
    { "-ft", 1, pFt,	"Rename tokens.h"},
    { "-ga", 0, pANSI,	"Generate ANSI-compatible code (default=FALSE)"},
    { "-gc", 0, pCGen,	"Do not generate output parser code (default=FALSE)"},
    { "-gd", 0, pTGen,	"Generate code to trace rule invocation (default=FALSE)"},
    { "-ge", 0, pEGen,	"Generate an error class for each non-terminal (default=FALSE)"},
    { "-gh", 0, pGHdr,	"Generate stdpccts.h for non-ANTLR-generated-files to include"},
    { "-gk", 0, pDL,	"Generate parsers that delay lookahead fetches until needed"},
    { "-gl", 0, pLI,	"Generate line info about grammar actions in C parser"},
    { "-glms", 0, pLIms,"Like -gl but replace '\\' with '/' in #line filenames for MS C/C++ systems"},
    { "-gp", 1, pPre,	"Prefix all generated rule functions with a string"},
    { "-gs", 0, pSGen,	"Do not generate sets for token expression lists (default=FALSE)"},
    { "-gt", 0, pAst,	"Generate code for Abstract-Syntax-Trees (default=FALSE)"},
    { "-gx", 0, pLGen,	"Do not generate lexical (dlg-related) files (default=FALSE)"},
    { "-gxt",0, pXTGen,	"Do not generate tokens.h (default=FALSE)"},
    { "-k",  1, pLLK,	"Set full LL(k) lookahead depth (default==1)"},
    { "-o",  1, pOut,	OutputDirectoryOption},
    { "-p",  0, pPrt,	"Print out the grammar w/o actions (default=no)"},
    { "-pa", 0, pPrtA,	"Print out the grammar w/o actions & w/FIRST sets (default=no)"},
    { "-pr",0, pPred,	"no longer used; predicates employed if present"},
    { "-prc", 1, pPredCtx,"Turn on/off computation of context for hoisted predicates"},
    { "-rl", 1, pTRes,	"Limit max # of tree nodes used by grammar analysis"},
    { "-stdout",0, pStdout,	"Send grammar.c/grammar.cpp to stdout"},               /* MR6 */
    { "-tab", 1, pTab,	"Width of tabs (1 to 8) for grammar.c/grammar.cpp files"}, /* MR6 */
	{ "-w1", 0, pW1,	"Set the warning level to 1 (default)"},
	{ "-w2", 0, pW2,	"Ambiguities yield warnings even if predicates or (...)? block"},
    { "-mrhoist",1,pMRhoist,                                                       /* MR9 */
                         "Turn on/off k=1 Maintenance Release style hoisting"},  /* MR9 */
    { "-mrhoistk",1,pMRhoistk,                                                       /* MR13 */
                         "Turn on/off k>1 EXPERIMENTAL Maintenance Release style hoisting"},  /* MR13 */
    { "-aa"  ,1,pAA,     "Ambiguity aid for a rule (rule name or line number)"},          /* MR11 */
    { "-aam" ,0,pAAm,
                         "Lookahead token may appear multiple times in -aa listing"},     /* MR11 */
    { "-aad" ,1,pAAd,
                         "Limits exp growth of -aa listing - default=1 (max=ck value)"},  /* MR11 */
	{ "-info",1,pInfo,
      "Extra info: p=pred t=tnodes f=first/follow m=monitor o=orphans 0=noop"},           /* MR11 */
    { "-treport",1,pTreport,
                        "Report when tnode usage exceeds value during ambiguity resolution"},   /* MR11 */
	{ "-newAST", 0, pNewAST,
                 "In C++ mode use \"newAST(...)\" rather than \"new AST(...)\""},         /* MR13 */
	{ "-tmake", 0, ptmakeInParser,
                 "In C++ mode use parser's tmake method rather than \"ASTBase::tmake(...)\""},   /* MR23 */
    { "-alpha",0, pAlpha,
                 "Provide additional information for \"(alpha)? beta\" error messages"},  /* MR14 */
    { "-mrblkerr",0,pMR_BlkErr,                                                           /* MR21 */
                 "EXPERIMENTAL change to (...)* and (...)+ syntax error sets"},           /* MR21 */
	{ "-nopurify",0,pNOPURIFY,
		"Don't use the notorious PURIFY macro (replaced by MR23 initial value syntax) to zero return arguments of rules"},   /* MR23 */
	{ "-",   0, pStdin,	"Read grammar from stdin" },
	{ "*",   0, pFile, 	"" },	/* anything else is a file */
#endif
	{ NULL,  0, NULL }
 };

void readDescr();
void cleanUp();

#ifdef __USE_PROTOS
static void buildRulePtr( void );
static void help( void );
static void init( void );
static void CompleteTokenSetRefs( void );
static void ensure_no_C_file_collisions(char *);
static void CompleteContextGuards(void);
#else
static void buildRulePtr( );
static void help( );
static void init( );
static void CompleteTokenSetRefs( );
static void ensure_no_C_file_collisions();
static void CompleteContextGuards();
#endif

static void
#ifdef __USE_PROTOS  /* <BJS> */
report_numericPredLabels(ActionNode *a)
#else
report_numericPredLabels(a)
ActionNode *a;
#endif
{                                                                           /* MR10 */
  warnFL("numeric references to attributes (e.g. $i or $i.j) in semantic pred will be null during guess mode",  /* MR10 */
            FileStr[a->file],a->line);                                      /* MR10 */
}                                                                           /* MR10 */

								/* M a i n */

int
#ifdef __USE_PROTOS
main( int argc, char *argv[] )
#else
main( argc, argv )
int argc;
char *argv[];
#endif
{
	int i;
	static char EPSTR[] = "[Ep]";

    Save_argc=argc;                                                  /* MR10 */
    Save_argv=argv;                                                  /* MR10 */

/*	malloc_debug(8);*/

#ifdef SPECIAL_INITS
    special_inits();                                                 /* MR1 */
#endif
	fprintf(stderr, "Antlr parser generator   Version %s   1989-2001\n", Version);
	if ( argc == 1 ) { help(); zzDIE; }
	ProcessArgs(argc-1, &(argv[1]), options);

/* MR14 */    if (MR_AmbAidRule && AlphaBetaTrace) {
/* MR14 */       fatal("Can't specify both -aa (ambiguity aid) and -alpha (\"(alpha)? beta\" aid)");
/* MR14 */    }

    if (MRhoistingk) {              /* MR13 */
      HoistPredicateContext=1;      /* MR13 */
      MRhoisting=1;                 /* MR13 */
    };                              /* MR13 */
    if (MRhoisting && ! HoistPredicateContext) {
/***      warnNoFL("Using \"-mrhoist\" forces \"-prc on\"");    ***/
      HoistPredicateContext=1;
    };
    if (HoistPredicateContext && ! MRhoisting) {
        warnNoFL("When using predicate context (-prc on) -mrhoist on is recommended");
    }
	/* Fix lookahead depth */
	/* Compressed lookahead must always be larger than or equal to full lookahead */
	if ( CLL_k < LL_k  && CLL_k>0 )
	{
		warnNoFL("must have compressed lookahead >= full LL(k) lookahead (setting -ck to -k)");
		CLL_k = LL_k;
	}
	if ( CLL_k == -1 ) CLL_k = LL_k;
	OutputLL_k = CLL_k;
	if ( ((CLL_k-1)&CLL_k)!=0 ) { /* output ll(k) must be power of 2 */
		int n;
		for(n=1; n<CLL_k; n<<=1) {;}
		OutputLL_k = n;
	};

	if (MR_BlkErr) {
		warnNoFL("The -mrblkerr option is EXPERIMENTAL");
        if (LL_k > 1) {
    		warnNoFL("The -mrblkerr option is designed only for k=1 ck=1 grammars");
        }
	};

    if ( ! ambAidDepthSpecified) {
      MR_AmbAidDepth=1;
    } else {
      if (MR_AmbAidDepth > CLL_k || MR_AmbAidDepth <= 0) {
        warnNoFL(eMsgd(
            "Ambiguity aid depth (\"-aad ...\") must be a number between 1 and max(k,ck)=%d",CLL_k));
        MR_AmbAidDepth=1;
      };
      if (MR_AmbAidDepth == 0) {
        MR_AmbAidDepth=2;
      };
    };

    if (MR_AmbAidRule != NULL) MR_AmbAidLine=atoi(MR_AmbAidRule);

	fpTrans = &(C_Trans[0]);		/* Translate to C Language */
	fpJTrans = &(C_JTrans[0]);
	init();
	lexclass(LexStartSymbol);

	readDescr();
	LastTokenCounted = TokenNum;
	RemapForcedTokens();
	if ( CannotContinue ) {cleanUp(); zzDIE;}
	if ( GenCC && no_classes_found ) fatal("required grammar class not found (exiting...)");
	if ( WarningLevel>1 && HdrAction == NULL )
	   warnNoFL("no #header action was found");
	if ( FoundAtOperator && ! FoundExceptionGroup) {
	   warnNoFL("found the exception operator '@' - but no exception group was found");
	};
	EpToken = addTname(EPSTR);		/* add imaginary token epsilon */
	set_orel(EpToken, &imag_tokens);

	/* this won't work for hand-built scanners since EofToken is not
	 * known.  Forces EOF to be token type 1.
	 */
	set_orel(EofToken, &imag_tokens);

	set_size(NumWords(TokenNum-1));

	/* compute the set of all known token types
	 * It represents the set of tokens from 1 to last_token_num + the
	 * reserved positions above that (if any).  Don't include the set of
	 * imaginary tokens such as the token/error classes or EOF.
	 */
	{
		set a;
		a = set_dup(reserved_positions);
		for (i=1; i<TokenNum; i++) { set_orel(i, &a); }
		all_tokens = set_dif(a, imag_tokens);
		set_free(a);
	}

	ComputeTokSets();				/* Compute #tokclass sets */
	CompleteTokenSetRefs();			/* Change complex nodes in syn diag */
    CompleteContextGuards();        /* MR13 */

	if ( CodeGen ) genDefFile();	/* create tokens.h */
	if ( LexGen ) genLexDescr();	/* create parser.dlg */

	if ( GenStdPccts )
	{
		FILE *f = fopen(OutMetaName(stdpccts), "w");
		if ( f==NULL ) {warnNoFL( eMsg1("can't create %s",OutMetaName(stdpccts)) );}
		else
		{
#ifdef SPECIAL_FOPEN
			special_fopen_actions(OutMetaName(stdpccts));            /* MR1 */
#endif
            if (strcmp(stdpccts,"stdpccts.h") == 0) {                /* MR10 */
 	  	      genStdPCCTSIncludeFile(f,NULL);                        /* MR10 */
            } else {                                                 /* MR10 */
 	  	      genStdPCCTSIncludeFile(f,pcctsBaseName(stdpccts));     /* MR32 */
            };
			fclose(f);
		}
	}

	buildRulePtr();					/* create mapping from rule # to RuleBlk junction */
	ComputeErrorSets();
	FoLink( (Node *)SynDiag );		/* add follow links to end of all rules */
	
	if ( GenCR ) GenCrossRef( SynDiag );

	if ( CodeGen )
	{
		if ( SynDiag == NULL )
		{
			warnNoFL("no grammar description recognized");
			cleanUp();
			zzDIE;
		}
		else if ( !GenCC ) {
			ErrFile = fopen(OutMetaName(ErrFileName), "w");
			require(ErrFile != NULL, "main: can't open err file");
#ifdef SPECIAL_FOPEN
	              special_fopen_actions(OutMetaName(ErrFileName));   /* MR1 */
#endif
			NewSetWd();
			GenErrHdr();
			TRANS(SynDiag);			/* Translate to the target language */
			DumpSetWd();
			DumpRemainingTokSets();
			fclose( ErrFile );
		}
		else {
			strcpy(Parser_h_Name, CurrentClassName);
			strcat(Parser_h_Name, ".h");
			strcpy(Parser_c_Name, CurrentClassName);
			strcat(Parser_c_Name, CPP_FILE_SUFFIX);
			ensure_no_C_file_collisions(Parser_c_Name);
			Parser_h = fopen(OutMetaName(Parser_h_Name), "w");
			require(Parser_h != NULL, "main: can't open class Parserx.h file");
#ifdef SPECIAL_FOPEN
		    special_fopen_actions(OutMetaName(Parser_h_Name));       /* MR1 */
#endif
			Parser_c = fopen(OutMetaName(Parser_c_Name), "w");
			require(Parser_c != NULL, "main: can't open class Parserx.c file");
#ifdef SPECIAL_FOPEN
		    special_fopen_actions(OutMetaName(Parser_c_Name));       /* MR1 */
#endif
			GenParser_h_Hdr();
			if ( class_before_actions != NULL )
			{
				ListNode *p;
				for (p = class_before_actions->next; p!=NULL; p=p->next)
				{
					UserAction *ua = (UserAction *)p->elem;
					dumpAction( ua->action, Parser_h, 0, ua->file, ua->line, 1);
				}
			}
			GenParser_c_Hdr();
			fprintf(Parser_h, "protected:\n");  /* MR20 */
			NewSetWd();
			TRANS(SynDiag);			/* Translate to the target language */
			DumpSetWd();
			GenRuleMemberDeclarationsForCC(Parser_h, SynDiag);
			if ( class_after_actions != NULL )
			{
				ListNode *p;
				for (p = class_after_actions->next; p!=NULL; p=p->next)
				{
					UserAction *ua = (UserAction *)p->elem;
					dumpAction( ua->action, Parser_h, 0, ua->file, ua->line, 1);
				}
			}
			DumpRemainingTokSets();
			fprintf(Parser_h, "};\n");
			fprintf(Parser_h, "\n#endif /* %s_h */\n", CurrentClassName);
			fclose( Parser_h );
			fclose( Parser_c );
		}
	}

    MR_orphanRules(stderr);
    if (LTinTokenAction && WarningLevel >= 2) {
		if (GenCC) {
			warnNoFL("At least one <<action>> following a token match contains a reference to LT(...)\n      this will reference the immediately preceding token,\n      not the one which follows as is the case with semantic predicates.");
		}
			warnNoFL("At least one <<action>> following a token match contains a reference to LA(...) or LATEXT(...)\n      this will reference the immediately preceding token,\n      not the one which follows as is the case with semantic predicates.");
	}

	if ( PrintOut )
	{
		if ( SynDiag == NULL ) {warnNoFL("no grammar description recognized");}
		else PRINT(SynDiag);
	}

#ifdef DBG_LL1
#endif
	GenRemapFile();					/* create remap.h */
/* MR10 */    if (FoundGuessBlk) {
#ifdef __cplusplus__
/* MR10 */      list_apply(NumericPredLabels, (void (*)(void *))report_numericPredLabels);
#else
#ifdef __USE_PROTOS
/* MR10 */      list_apply(NumericPredLabels, (void (*)(void *))report_numericPredLabels);
#else
/* MR10 */      list_apply(NumericPredLabels,report_numericPredLabels);
#endif
#endif
/* MR10 */    };

    if (InfoT && TnodesAllocated > 0) {
      if (TnodesPeak > 10000) {
        fprintf(stdout,"\nTree Nodes:  peak %dk  created %dk  lost %d\n",
                        (TnodesPeak/1000),
                        (TnodesAllocated/1000),
                        TnodesInUse-tnodes_used_in_guard_predicates_etc);
       } else {
         fprintf(stdout,"\nTree Nodes:  peak %d  created %d  lost %d\n",
                        TnodesPeak,
                        TnodesAllocated,
                        TnodesInUse-tnodes_used_in_guard_predicates_etc);
       };
    };
    if (InfoF) {
      DumpFcache();
    };
    if (MR_skipped_e3_report) {
      fprintf(stderr,"note: use -e3 to get exact information on ambiguous tuples\n");
    };
    if (MR_BadExprSets != 0) {
      fprintf(stderr,"note: Unreachable C or C++ code was generated for empty expression sets,\n");
      fprintf(stderr,"        probably due to undefined rules or infinite left recursion.\n");
      fprintf(stderr,"      To locate: search the generated code for \"empty set expression\"\n");
    };
    if (MR_AmbAidRule != NULL && MR_matched_AmbAidRule==0) {
      RuleEntry *q = (RuleEntry *) hash_get(Rname,MR_AmbAidRule);
      if (MR_AmbAidLine == 0 && q == NULL) {
         warnNoFL(eMsg2("there is no rule \"%s\" so \"-aa %s\" will never match",
                                                        MR_AmbAidRule,MR_AmbAidRule));
      } else {
        warnNoFL(eMsg1("there was no ambiguity that matched \"-aa %s\"",MR_AmbAidRule));
      };
    };
    if (AlphaBetaTrace) {

      if (MR_AlphaBetaMessageCount == 0) {
         fprintf(stderr,"note: there were no messages about \"(alpha)? beta\" blocks added to the generated code\n");
      } else {
         fprintf(stderr,"note: there were %d messages about \"(alpha)? beta\" blocks added to the generated code\n",
                    MR_AlphaBetaMessageCount);
      }

      if (set_null(MR_CompromisedRules)) {
         fprintf(stderr,"note: the list of rules with compromised follow sets is empty\n");
      } else {
         fprintf(stderr,"note: the following is a list of rules which *may* have incorrect\n");
         fprintf(stderr,"      follow sets computed as a result of an \"(alpha)? beta\" block\n");
         fprintf(stderr,"\n");
         MR_dumpRuleSet(MR_CompromisedRules);
         fprintf(stderr,"\n");
      }
    }
	cleanUp();
	exit(PCCTS_EXIT_SUCCESS);
    return 0;           /* MR11 make compilers happy */ 
}

static void 
#ifdef __USE_PROTOS 
init( void )
#else
init( )
#endif
{
	SignalEntry *q;

	Tname = newHashTable();
	Rname = newHashTable();
	Fcache = newHashTable();
	Tcache = newHashTable();
	Sname = newHashTable();
    Pname = newHashTable();     /* MR11 */

	/* Add default signal names */
	q = (SignalEntry *)hash_add(Sname,
							  "NoViableAlt",
							  (Entry *)newSignalEntry("NoViableAlt"));
	require(q!=NULL, "cannot alloc signal entry");
	q->signum = sigNoViableAlt;
	q = (SignalEntry *)hash_add(Sname,
							  "MismatchedToken",
							  (Entry *)newSignalEntry("MismatchedToken"));
	require(q!=NULL, "cannot alloc signal entry");
	q->signum = sigMismatchedToken;
	q = (SignalEntry *)hash_add(Sname,
							  "NoSemViableAlt",
							  (Entry *)newSignalEntry("NoSemViableAlt"));
	require(q!=NULL, "cannot alloc signal entry");
	q->signum = sigNoSemViableAlt;
	
	reserved_positions = empty;
	all_tokens = empty;
	imag_tokens = empty;
	tokclasses = empty;
	TokenStr = (char **) calloc(TSChunk, sizeof(char *));
	require(TokenStr!=NULL, "main: cannot allocate TokenStr");
	FoStack = (int **) calloc(CLL_k+1, sizeof(int *));
	require(FoStack!=NULL, "main: cannot allocate FoStack");
	FoTOS = (int **) calloc(CLL_k+1, sizeof(int *));
	require(FoTOS!=NULL, "main: cannot allocate FoTOS");
	Cycles = (ListNode **) calloc(CLL_k+1, sizeof(ListNode *));
	require(Cycles!=NULL, "main: cannot allocate Cycles List");
    MR_CompromisedRules=empty;  /* MR14 */
}

static void
#ifdef __USE_PROTOS
help( void )
#else
help( )
#endif
{
	Opt *p = options;
	fprintf(stderr, "antlr [options] f1 f2 ... fn\n");
	while ( *(p->option) != '*' )
	{
		fprintf(stderr, "    %-9s%s   %s\n",
						p->option,
						(p->arg)?"___":"   ",
						p->descr);
		p++;
	}
}

/* The RulePtr array is filled in here.  RulePtr exists primarily
 * so that sets of rules can be maintained for the FOLLOW caching
 * mechanism found in rJunc().  RulePtr maps a rule num from 1 to n
 * to a pointer to its RuleBlk junction where n is the number of rules.
 */
static void
#ifdef __USE_PROTOS
buildRulePtr( void )
#else
buildRulePtr( )
#endif
{
	int r=1;
	Junction *p  = SynDiag;
	RulePtr = (Junction **) calloc(NumRules+1, sizeof(Junction *));
	require(RulePtr!=NULL, "cannot allocate RulePtr array");
	
	while ( p!=NULL )
	{
		require(r<=NumRules, "too many rules???");
		RulePtr[r++] = p;
		p = (Junction *)p->p2;
	}
}

void
#ifdef __USE_PROTOS
dlgerror(const char *s)
#else
dlgerror(s)
char *s;
#endif
{
	fprintf(stderr, ErrHdr, FileStr[CurFile], zzline);
	fprintf(stderr, " lexical error: %s (text was '%s')\n",
					((s == NULL) ? "Lexical error" : s), zzlextext);
}

void
#ifdef __USE_PROTOS
readDescr( void )
#else
readDescr( )
#endif
{
	zzerr = dlgerror;
	input = NextFile();
	if ( input==NULL ) fatal("No grammar description found (exiting...)");
	ANTLR(grammar(), input);
    tnodes_used_in_guard_predicates_etc=TnodesInUse;    /* MR10 */
}

FILE *
#ifdef __USE_PROTOS
NextFile( void )
#else
NextFile( )
#endif
{
	FILE *f;

	for (;;)
	{
		CurFile++;
		if ( CurFile >= NumFiles ) return(NULL);
		if ( ci_strequ(FileStr[CurFile],"stdin")) return stdin;
		f = fopen(FileStr[CurFile], "r");
		if ( f == NULL )
		{
			warnNoFL( eMsg1("file %s doesn't exist; ignored", FileStr[CurFile]) );
		}
		else
		{
			return(f);
		}
	}
}

/*
 * Return a string corresponding to the output file name associated
 * with the input file name passed in.
 *
 * Observe the following rules:
 *
 *		f.e		--> f".c"
 *		f		--> f".c"
 *		f.		--> f".c"
 *		f.e.g	--> f.e".c"
 *
 * Where f,e,g are arbitrarily long sequences of characters in a file
 * name.
 *
 * In other words, if a ".x" appears on the end of a file name, make it
 * ".c".  If no ".x" appears, append ".c" to the end of the file name.
 *
 * C++ mode using .cpp not .c.
 *
 * Use malloc() for new string.
 */

char *
#ifdef __USE_PROTOS
outname( char *fs )
#else
outname( fs )
char *fs;
#endif
{
    if ( GenCC) {
      return outnameX(fs,CPP_FILE_SUFFIX);
    } else {
      return outnameX(fs,".c");
    };
}

char *
#ifdef __USE_PROTOS
outnameX( char *fs ,char *suffix)
#else
outnameX( fs , suffix )
char *fs;
char *suffix;
#endif
{
	static char buf[MaxFileName+1];
	char *p;
	require(fs!=NULL&&*fs!='\0', "outname: NULL filename");

	p = buf;
	strcpy(buf, fs);
	while ( *p != '\0' )  {p++;}			/* Stop on '\0' */
	while ( *p != '.' && p != buf ) {--p;}	/* Find '.' */
	if ( p != buf ) *p = '\0';				/* Found '.' */
	require(strlen(buf) + 2 < (size_t)MaxFileName, "outname: filename too big");
    strcat(buf,suffix);
	return( buf );
}

void
#ifdef __USE_PROTOS
fatalFL( char *err_, char *f, int l )
#else
fatalFL( err_, f, l )
char *err_;
char *f;
int l;
#endif
{
	fprintf(stderr, ErrHdr, f, l);
	fprintf(stderr,	" %s\n", err_);
	cleanUp();
	exit(PCCTS_EXIT_FAILURE);
}

void
#ifdef __USE_PROTOS
fatal_intern( char *err_, char *f, int l )
#else
fatal_intern( err_, f, l )
char *err_;
char *f;
int l;
#endif
{
	fprintf(stderr, ErrHdr, f, l);
	fprintf(stderr,	" #$%%*&@# internal error: %s\n", err_);
	fprintf(stderr, ErrHdr, f, l);
	fprintf(stderr, " [complain to nearest government official\n");
	fprintf(stderr, ErrHdr, f, l);
	fprintf(stderr, "  or send hate-mail to parrt@parr-research.com;\n");
	fprintf(stderr, ErrHdr, f, l);
	fprintf(stderr, "  please pray to the ``bug'' gods that there is a trival fix.]\n");
	cleanUp();
	exit(PCCTS_EXIT_FAILURE);
}

void
#ifdef __USE_PROTOS
cleanUp( void )
#else
cleanUp( )
#endif
{
	if ( DefFile != NULL) fclose( DefFile );
}

/* sprintf up to 3 strings */
char *
#ifdef __USE_PROTOS
eMsg3( char *s, char *a1, char *a2, char *a3 )
#else
eMsg3( s, a1, a2, a3 )
char *s;
char *a1;
char *a2;
char *a3;
#endif
{
	static char buf[250];			/* DANGEROUS as hell !!!!!! */
	
	sprintf(buf, s, a1, a2, a3);
	return( buf );
}

/* sprintf a decimal */
char *
#ifdef __USE_PROTOS
eMsgd( char *s, int d )
#else
eMsgd( s, d )
char *s;
int d;
#endif
{
	static char buf[250];			/* DANGEROUS as hell !!!!!! */
	
	sprintf(buf, s, d);
	return( buf );
}

char *
#ifdef __USE_PROTOS
eMsgd2( char *s, int d1,int d2)
#else
eMsgd2( s, d1, d2 )
char *s;
int d1;
int d2;
#endif
{
	static char buf[250];			/* DANGEROUS as hell !!!!!! */
	
	sprintf(buf, s, d1, d2);
	return( buf );
}

void
#ifdef __USE_PROTOS
s_fprT( FILE *f, set e )
#else
s_fprT( f, e )
FILE *f;
set e;
#endif
{
	register unsigned *p;
	unsigned *q;

	if ( set_nil(e) ) return;
	if ( (q=p=set_pdq(e)) == NULL ) fatal_internal("Can't alloc space for set_pdq");
	fprintf(f, "{");
	while ( *p != nil )
	{
		fprintf(f, " %s", TerminalString(*p));
		p++;
	}
	fprintf(f, " }");
	free((char *)q);
}

/* Return the token name or regular expression for a token number. */
char *
#ifdef __USE_PROTOS
TerminalString( int token )
#else
TerminalString( token )
int token;
#endif
{
	int     j;
        static    char    imag_name[20];

	/* look in all lexclasses for the token */
	if ( TokenString(token) != NULL ) return TokenString(token);
	for (j=0; j<NumLexClasses; j++)
	{
		lexmode(j);
		if ( ExprString(token) != NULL ) return ExprString(token);
	}

    if (1) {
      sprintf(imag_name,"UnknownToken#%d",token);           /* MR13 */
      return imag_name;                                     /* MR13 */
    }

	require(j<NumLexClasses, eMsgd("No label or expr for token %d",token));
	return "invalid";
}

                    /* S i m p l e  I n t  S t a c k */

void
#ifdef __USE_PROTOS
pushint( int i )
#else
pushint( i )
int i;
#endif
{
	require(isp>0, "pushint: stack overflow");
	istack[--isp] = i;
}

int
#ifdef __USE_PROTOS
popint( void )
#else
popint( )
#endif
{
	require(isp<MAX_INT_STACK, "popint: stack underflow");
	return istack[isp++];
}

int
#ifdef __USE_PROTOS
istacksize( void )
#else
istacksize( )
#endif
{
	return MAX_INT_STACK-isp;
}

void
#ifdef __USE_PROTOS
istackreset( void )
#else
istackreset( )
#endif
{
	isp = MAX_INT_STACK;
}

int
#ifdef __USE_PROTOS
istackempty( void )
#else
istackempty( )
#endif
{
	return isp==MAX_INT_STACK;
}

int
#ifdef __USE_PROTOS
topint( void )
#else
topint( )
#endif
{
	require(isp<MAX_INT_STACK, "topint: stack underflow");
	return istack[isp];
}

void
#ifdef __USE_PROTOS
ProcessArgs( int argc, char **argv, Opt *options )
#else
ProcessArgs( argc, argv, options )
int argc;
char **argv;
Opt *options;
#endif
{
	Opt *p;
	require(argv!=NULL, "ProcessArgs: command line NULL");

	while ( argc-- > 0 )
	{
		p = options;
		while ( p->option != NULL )
		{
			if ( strcmp(p->option, "*") == 0 ||
				 ci_strequ(p->option, *argv) == 1 )
			{
				if ( p->arg )
				{
/* MR9  26-Sep-97   Check for argv valid                */
                    if (argc-- > 0) {
  				     	(*p->process)( *argv, *(argv+1) );
					    argv++;
                    } else {
fprintf(stderr,"error: required argument for option %s omitted\n",*argv);
exit(PCCTS_EXIT_FAILURE);
                    };
				}
				else
					(*p->process)( *argv );
				break;
			}
			p++;
		}
		argv++;
	}
}

static void 
#ifdef __USE_PROTOS
CompleteContextGuards(void)
#else
CompleteContextGuards()
#endif
{
    ListNode *      p;
    Predicate *     pred;

    if (ContextGuardPredicateList == NULL) return;

    for (p=ContextGuardPredicateList->next; p != NULL; p=p->next) {
      pred=(Predicate *)p->elem;
      recomputeContextGuard(pred);
    }
}

/* Go back into the syntax diagram and compute all meta tokens; i.e.
 * turn all '.', ranges, token class refs etc... into actual token sets
 */
static void
#ifdef __USE_PROTOS
CompleteTokenSetRefs(void)
#else
CompleteTokenSetRefs()
#endif
{
	ListNode *p;

	if ( MetaTokenNodes==NULL ) return;
	for (p = MetaTokenNodes->next; p!=NULL; p=p->next)
	{
		set a,b;

		TokNode *q = (TokNode *)p->elem;
		if ( q->wild_card )
		{
			q->tset = all_tokens;
		}
		else if ( q->tclass!=NULL )
		{
			if ( q->complement ) q->tset = set_dif(all_tokens, q->tclass->tset);
			else q->tset = q->tclass->tset;
		}
		else if ( q->upper_range!=0 )
		{
			/* we have a range on our hands: make a set from q->token .. q->upper_range */
			int i;
			a = empty;
			for (i=q->token; i<=q->upper_range; i++) { set_orel(i, &a); }   /* MR13 */

/* MR13 */    if (q->complement) {
/* MR13 */      q->tset = set_dif(all_tokens, a);
/* MR13 */        set_free(a);
/* MR13 */      } else {
/* MR13 */	      q->tset = a;
/* MR13 */      }

        }

		/* at this point, it can only be a complemented single token */
		else if ( q->complement )
		{
			a = set_of(q->token);
			b = set_dif(all_tokens, a);
			set_free(a);
			q->tset=b;
		}
		else fatal("invalid meta token");
	}
}

/* MR10: Jeff Vincent
   MR10: Changed to remove directory information from n only if
   MR10: if OutputDirectory was changed by user (-o option)
*/

char *
#ifdef __USE_PROTOS
OutMetaName(char *n)
#else
OutMetaName(n)
char *n;
#endif
{	
    static char *dir_sym = DirectorySymbol;
    static char newname[MaxFileName+1];
    char *p;

	/* If OutputDirectory is same as TopDirectory (platform default) then leave n alone. */
    if (strcmp(OutputDirectory, TopDirectory) == 0)		/* TopDirectory is "." on Unix. */
		return n;

	/* p will point to filename without path information */
	if ((p = strrchr(n, *dir_sym)) != NULL)				/* Directory symbol is "/" on Unix. */
		p++;
	else
		p = n;

	/* Copy new output directory into newname[] */
	strcpy(newname, OutputDirectory);

	/* if new output directory does not have trailing dir_sym, add it! */
	if (newname[strlen(newname)-1] != *dir_sym) {
		strcat(newname, dir_sym);
	}
	strcat(newname, p);
	return newname;
}

char *
#ifdef __USE_PROTOS
pcctsBaseName(char *n) /* MR32 */
#else
pcctsBaseName(n)
char *n;
#endif
{
    static char newname[MaxFileName+1];
    static char* dir_sym = DirectorySymbol;
    int count = 0;
    char *p;

    p = n;

    while ( *p != '\0' )  {p++;}                    /* go to end of string */
    while ( (*p != *dir_sym) && (p != n) ) {--p;}   /* Find last DirectorySymbol */
    while ( *p == *dir_sym) p++;                    /* step forward if we're on a dir symbol */
    while ( *p != '\0' && *p != '.')
    {
        newname[count++] = *p;
        p++;
    }                                               /* create a new name */
    newname[count] = '\0';
    return newname;
}

static void
#ifdef __USE_PROTOS
ensure_no_C_file_collisions(char *class_c_file)
#else
ensure_no_C_file_collisions(class_c_file)
char *class_c_file;
#endif
{
	int i;

	for (i=0; i<NumFiles; i++)
	{

#ifdef PCCTS_CASE_INSENSITIVE_FILE_NAME
		/* assume that file names are case insensitive */
		if ( STRICMP(outname(FileStr[i]), class_c_file)==0 )
#else
		if ( strcmp(outname(FileStr[i]), class_c_file)==0 )
#endif
		{
			fatal(eMsg1("class def output file conflicts with parser output file: %s",
						outname(FileStr[i])));
		}
	}
}

void
#ifdef __USE_PROTOS
warnNoFL(char *err)
#else
warnNoFL(err)
char *err;
#endif
{
	fprintf(stderr, "warning: %s\n", err);
}

void
#ifdef __USE_PROTOS
warnFL(char *err,char *f,int l)
#else
warnFL(err,f,l)
char *f;
int l;
char *err;
#endif
{
	fprintf(stderr, ErrHdr, f, l);						
	fprintf(stderr, " warning: %s\n", err);
}

void
#ifdef __USE_PROTOS
warn(char *err)												
#else
warn(err)												
char *err;
#endif
{
	/* back up the file number if we hit an error at the end of the last file */
	if ( CurFile >= NumFiles && CurFile >= 1 ) CurFile--;
	fprintf(stderr, ErrHdr, FileStr[CurFile], zzline);
	fprintf(stderr, " warning: %s\n", err);
}

void
#ifdef __USE_PROTOS
warnNoCR( char *err )
#else
warnNoCR( err )											
char *err;
#endif
{
	/* back up the file number if we hit an error at the end of the last file */
	if ( CurFile >= NumFiles && CurFile >= 1 ) CurFile--;
	fprintf(stderr, ErrHdr, FileStr[CurFile], zzline);
	fprintf(stderr, " warning: %s", err);
}

void
#ifdef __USE_PROTOS
errNoFL(char *err)
#else
errNoFL(err)
char *err;
#endif
{
	fprintf(stderr, "error: %s\n", err);
}

void
#ifdef __USE_PROTOS
errFL(char *err,char *f,int l)
#else
errFL(err,f,l)
char *err;
char *f;
int l;
#endif
{
	fprintf(stderr, ErrHdr, f, l);						
	fprintf(stderr, " error: %s\n", err);
}

void
#ifdef __USE_PROTOS
err(char *err)												
#else
err(err)												
char *err;
#endif
{
	/* back up the file number if we hit an error at the end of the last file */
	if ( CurFile >= NumFiles && CurFile >= 1 ) CurFile--;
	fprintf(stderr, ErrHdr, FileStr[CurFile], zzline);
	fprintf(stderr, " error: %s\n", err);
}

void
#ifdef __USE_PROTOS
errNoCR( char *err )											
#else
errNoCR( err )											
char *err;
#endif
{
	/* back up the file number if we hit an error at the end of the last file */
	if ( CurFile >= NumFiles && CurFile >= 1 ) CurFile--;
	fprintf(stderr, ErrHdr, FileStr[CurFile], zzline);
	fprintf(stderr, " error: %s", err);
}

UserAction *
#ifdef __USE_PROTOS
newUserAction(char *s)
#else
newUserAction(s)
char *s;
#endif
{
	UserAction *ua = (UserAction *) calloc(1, sizeof(UserAction));
	require(ua!=NULL, "cannot allocate UserAction");

	ua->action = (char *) calloc(strlen(LATEXT(1))+1, sizeof(char));
	strcpy(ua->action, s);
	return ua;
}

/* Added by TJP September 1994 */
/* Take in file.h and return file_h; names w/o '.'s are left alone */
char *
#ifdef __USE_PROTOS
gate_symbol(char *name)
#else
gate_symbol(name)
char *name;
#endif
{
	static char buf[100];
	char *p;
	sprintf(buf, "%s", name);

	for (p=buf; *p!='\0'; p++)
	{
		if ( *p=='.' ) *p = '_';
	}
	return buf;
}

char *
#ifdef __USE_PROTOS
makeAltID(int blockid, int altnum)
#else
makeAltID(blockid, altnum)
int blockid;
int altnum;
#endif
{
	static char buf[100];
	char *p;
	sprintf(buf, "_blk%d_alt%d", blockid, altnum);
	p = (char *)malloc(strlen(buf)+1);
	strcpy(p, buf);
	return p;
}