[go: up one dir, main page]

File: main.c

package info (click to toggle)
sorcerer 1.0
  • links: PTS
  • area: main
  • in suites: slink
  • size: 736 kB
  • ctags: 1,524
  • sloc: ansic: 11,308; cpp: 1,388; makefile: 300
file content (1059 lines) | stat: -rw-r--r-- 23,056 bytes parent folder | download
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
/*
 * main.c
 *
 * SOFTWARE RIGHTS
 *
 * We reserve no LEGAL rights to SORCERER -- SORCERER is in the public
 * domain.  An individual or company may do whatever they wish with
 * source code distributed with SORCERER or the code generated by
 * SORCERER, including the incorporation of SORCERER, or its output, into
 * commerical software.
 * 
 * We encourage users to develop software with SORCERER.  However, we do
 * ask that credit is given to us for developing SORCERER.  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 SORCERER and have developed a nice tool with the
 * output, please mention that you developed it using SORCERER.  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.
 *
 * SORCERER 1.00B
 * Terence Parr
 * AHPCRC, University of Minnesota
 * 1992-1994
 */
#include "stdpccts.h"
#include "sym.h"
#include "proto.h"
#include "ctype.h"

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

static int DontAcceptStdin = 0;
static int DontAcceptFiles = 0;
static TokenDef *token_defs = NULL;

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

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

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
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;
}

static void
#ifdef __USE_PROTOS
pDToks( char *s, char *t )
#else
pDToks( s, t )
char *s, *t;
#endif
{
	if ( !GenCPP )
	{
		warnNoFL("-def-tokens valid with C++ interface only; ignored");
		return;
	}
	else {
		def_tokens = 1;
	}
}

static void
#ifdef __USE_PROTOS
pDToksFile( char *s, char *t )
#else
pDToksFile( s, t )
char *s, *t;
#endif
{
	if ( GenCPP )
	{
		warnNoFL("-def-tokens-file not valid with C++ interface; ignored");
		return;
	}
	if ( Inline )
	{
		warnNoFL("-def-tokens-file conflicts with -inline; ignored");
	}
	else {
		def_token_file = t;
		if ( def_token_file == NULL )
		{
			warnNoFL("don't you want filename with that -def-tokens-file?; ignored");
		}
	}
}

static void
#ifdef __USE_PROTOS
pPrefix( char *s, char *t )
#else
pPrefix( s, t )
char *s, *t;
#endif
{
	Prefix = t;
	if ( GenCPP ) {
		warnNoFL("-prefix conflicts with C++ interface; ignored");
		Prefix = "";
		return;
	}

	if ( Prefix == NULL )
	{
		warnNoFL("don't you want string with that -prefix?; ignored");
		Prefix = "";
	}
}

static void
#ifdef __USE_PROTOS
pFuncs( char *s, char *t )
#else
pFuncs( s, t )
char *s, *t;
#endif
{
	if ( GenCPP ) { errNoFL("-funcs option makes no sense in C++ mode; ignored"); return; }
	GenKRProtos = GenAnsiProtos = 0;
	if ( strcmp(t, "KR")==0 ) GenKRProtos = 1;
	else if ( strcmp(t, "ANSI")==0 ) GenAnsiProtos = 1;
	else if ( strcmp(t, "both")==0 ) {GenAnsiProtos = 1; GenKRProtos = 1;}
	else {
		warnNoFL(eMsg1("don't know how to generate '%s' style functions; assuming ANSI",t));
		GenAnsiProtos = 1;
	}
}

static void
#ifdef __USE_PROTOS
pProtoFile( char *s, char *t )
#else
pProtoFile( s, t )
char *s, *t;
#endif
{
	if ( GenCPP )
		{warnNoFL("-proto-file not valid with C++ interface; ignored");}
	if ( t==NULL )
		{warnNoFL("don't you want filename with that -proto-file?; ignored");}
	else {
		GenProtoFile = t;
	}
}

static void
pstdin()
{
	if ( DontAcceptStdin )
	{
		warnNoFL("'-' (stdin) ignored as files were specified first");
		return;
	}
	if ( GenCPP )
		{warnNoFL("'-' (stdin) cannot be used with C++ interface; ignored");}

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

static void
pGuts()
{
	print_guts = 1;
}

static void
pTrans()
{
	transform = 1;
}

static void
pInline()
{
	if ( def_token_file!=NULL )
	{
		warnNoFL("-inline conflicts with -def-tokens; ignored");
	}
	else if ( GenCPP )
	{
		warnNoFL("-inline conflicts with C++ interface; ignored");
	}
	else Inline = 1;
}

static void
pCPP()
{
	GenCPP = 1;
}

Opt options[] = {
#ifdef __cplusplus
    { "-CPP",		 0, (void (*)(...)) pCPP,"Generate C++ output"},
    { "-def-tokens", 0, (void (*)(...)) pDToks,"Define ref'd tokens w/unique integer (C++ interface)"},
    { "-def-tokens-file", 1, (void (*)(...)) pDToksFile,"Define ref'd tokens w/unique integer (put in file)"},
    { "-funcs", 1,		(void (*)(...)) pFuncs,"Gen 'ANSI', 'KR', or 'both' style function headers"},
    { "-guts", 0,		(void (*)(...)) pGuts,"Print out a bunch of internal data structures"},
    { "-inline", 0,		(void (*)(...)) pInline, "Gen only actions and functions for given rules"},
    { "-prefix", 1,		(void (*)(...)) pPrefix, "Prefix all globally visible symbols"},
    { "-proto-file", 1, (void (*)(...)) pProtoFile, "Put all prototypes for rule functions in this file"},
    { "-out-dir", 1,	(void (*)(...)) pOut,	"Directory where all output files go (default=\".\")"},
    { "-transform", 0,	(void (*)(...)) pTrans, "Transformation mode; read/write to different pointers"},
    { "-",	0,			(void (*)(...)) pstdin,	"take input from stdin"},
	{ "*",  0,			(void (*)(...)) pFile, 	"" },	/* anything else is a file */
#else
    { "-CPP",		 0, pCPP,  "Generate C++ output"},
    { "-def-tokens", 0, pDToks,"Define ref'd tokens w/unique integer (C++ interface)"},
    { "-def-tokens-file", 1, pDToksFile,"Define ref'd tokens w/unique integer (put in file)"},
    { "-funcs", 1,		pFuncs,"Gen 'ANSI', 'KR', or 'both' style function headers"},
    { "-guts", 0,		pGuts,"Print out a bunch of internal data structures"},
    { "-inline", 0,		pInline, "Gen only actions and functions for given rules"},
    { "-prefix", 1,		pPrefix, "Prefix all globally visible symbols"},
    { "-proto-file", 1, pProtoFile, "Put all prototypes for rule functions in this file"},
    { "-out-dir", 1,	pOut,	"Directory where all output files go (default=\".\")"},
    { "-transform", 0,	pTrans, "Transformation mode; read/write to different pointers"},
    { "-",	0,			pstdin,	"take input from stdin"},
	{ "*",  0,			pFile, 	"" },	/* anything else is a file */
#endif
	{ NULL, 0, NULL, NULL }
};

#ifdef __USE_PROTOS
main(int argc, char *argv[])
#else
main(argc, argv)
int argc;
char *argv[];
#endif
{
	AST *root;
#ifdef THINK_C
#include <console.h>
	argc = ccommand (&argv);
#endif

	fprintf(stderr,
		"Sorcerer  a simple tree-parser generator  Version %s  1992-1995\n",
		Version);

	if ( argc == 1 ) { help(); zzDONE; }
	ProcessArgs(argc-1, &(argv[1]), options);

	symbols = newHashTable();
	token_dict = (char **) calloc(token_table_size, sizeof(char *));
	require(token_dict != NULL, "main: can't create token_dict");

	output = stdout;
	input = NextFile();
	require(input!=NULL, "No grammar description found (exiting...)");
	root = read_sor_desc(input);

	if ( root!=NULL ) gen(root);
		
	if ( found_error )
	{
		fprintf(stderr, "no parser generated due to parsing errors; exiting...\n");
		zzDIE;
	}

	if ( GenCPP ) {
		GenCPPClassHeader();
		GenCPPClassCode();
	}

	if ( !UserDefdTokens ) gen_tokens_file();
	else if ( def_token_file!=NULL ) {
		warnNoFL("tokens file not generated; it conflicts with use of #tokdefs");
	}

	if ( GenProtoFile!=NULL )
	{
		FILE *ProtoFILE;
		ProtoFILE = fopen(OutMetaName(GenProtoFile), "w");
		if ( ProtoFILE==NULL ) {warnNoFL(eMsg1("Can't open prototype file '%s'; ignored",GenProtoFile));}
		else {
			gen_info_hdr( ProtoFILE );
			if ( header_action!=NULL )
				dumpAction(header_action, ProtoFILE, 0, -1, 0, 1);
			GenRulePrototypes( ProtoFILE, 0 );
			fclose(ProtoFILE);
		}
	}
	if ( found_error ) {zzDIE;}
	else zzDONE;
}

void
help()
{
	Opt *p = options;
	static char buf[MaxRuleName+1];

	fprintf(stderr, "sor [options] f1.sor ... fn.sor\n");
	while ( p->option!=NULL && *(p->option) != '*' )
	{
		buf[0]='\0';
		if ( p->arg ) sprintf(buf, "%s ___", p->option);
		else strcpy(buf, p->option);
		fprintf(stderr, "    %-20s   %s\n", buf, p->descr);
		p++;
	}
}

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

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

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];
}

/* 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 );
}

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 );
}

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_);
    exit(1);
}

/*
 * add an element to a list.
 *
 * Any non-empty list has a sentinel node whose 'elem' pointer is really
 * a pointer to the last element.  (i.e. length(list) = #elemIn(list)+1).
 * Elements are appended to the list.
 */
void
#ifdef __USE_PROTOS
list_add( ListNode **list, void *e )
#else
list_add( list, e )
ListNode **list;
void *e;
#endif
{
	ListNode *p, *tail;
	require(e!=NULL, "list_add: attempting to add NULL list element");

	p = newListNode;
	require(p!=NULL, "list_add: cannot alloc new list node");
	p->elem = e;
	if ( *list == NULL )
	{
		ListNode *sentinel = newListNode;
		require(sentinel!=NULL, "list_add: cannot alloc sentinel node");
		*list=sentinel;
		sentinel->next = p;
		sentinel->elem = (char *)p;		/* set tail pointer */
	}
	else								/* find end of list */
	{
		tail = (ListNode *) (*list)->elem;	/* get tail pointer */
		tail->next = p;
		(*list)->elem = (char *) p;		/* reset tail */
	}
}

static 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 ||
				 strcmp(p->option, *argv) == 0 )
			{
				if ( p->arg )
				{
					(*p->process)( *argv, *(argv+1) );
					argv++;
					argc--;
				}
				else
					(*p->process)( *argv );
				break;
			}
			p++;
		}
		argv++;
	}
}

/*
 * Given a string, this function allocates and returns a pointer to a
 * hash table record of size 'sz' whose "str" pointer is reset to a position
 * in the string table.
 */
Entry *
#ifdef __USE_PROTOS
newEntry( char *text, int sz )
#else
newEntry( text, sz )
char *text;
int sz;
#endif
{
	Entry *p;
	require(text!=NULL, "new: NULL terminal");
	
	if ( (p = (Entry *) calloc(1,sz)) == 0 )
	{
		fatal("newEntry: out of memory for terminals\n");
		exit(1);
	}
	p->str = mystrdup(text);
	
	return(p);
}

void
#ifdef __USE_PROTOS
token_association(int token_type, char *text)
#else
token_association( token_type, text )
int token_type;
char *text;
#endif
{
	SymEntry *p;

	if ( token_type >= token_table_size )	/* overflow? */
	{
		char **p;
		int i, more;

		more = 100;
		token_table_size += more;
		token_dict = (char **) realloc(token_dict, token_table_size*sizeof(char *));
		require(token_dict != NULL, "token_association: can't extend token_dict");
		for (p= &token_dict[token_table_size-more],i=1; i<=more; i++) *p++ = NULL;
	}
	token_dict[token_type] = text;
}

/*
 * 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.t	--> f.e".c"
 *
 * Where f,e,t 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.
 *
 * Use malloc() for new string.
 */
char *
#ifdef __USE_PROTOS
outname( char *fs )
#else
outname( fs )
char *fs;
#endif
{
	static char buf[MaxFileName+1];
	char *p;

	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");
	if ( GenCPP ) strcat(buf, CPP_FILE_SUFFIX);
	else strcat(buf, ".c");
	return( buf );
}

void
#ifdef __STDC__
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 PC
		/* assume that file names are case insensitive */
		if ( strcasecmp(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])));
		}
	}
}

char *
#ifdef __USE_PROTOS
OutMetaName(char *n)
#else
OutMetaName(n)
char *n;
#endif
{
	static char buf[MaxFileName+1];

	strcpy(buf, OutputDirectory);
	strcat(buf, DirectorySymbol);
	strcat(buf, n);
	return buf;
}

void
#ifdef __USE_PROTOS
set_fprint( FILE *f, set e )
#else
set_fprint( f, e )
FILE *f;
set e;
#endif
{
	int t;

	if ( set_nil(e) ) return;
	fprintf(f, "{");
	while ( !set_nil(e) )
	{
		t = set_int(e);
		set_rm(t, e);
		fprintf(f, " %s", token_dict[t]);
	}
	fprintf(f, " }");
}

/* Given a list of ANSI-style parameter declarations, print out a
 * comma-separated list of the symbols (w/o types).
 * Basically, we look for a comma, then work backwards until start of
 * the symbol name.  Then print it out until 1st non-alnum char.  Now,
 * move on to next parameter.
 */
void
#ifdef __USE_PROTOS
DumpListOfParmNames( char *pdecl, FILE *output )
#else
DumpListOfParmNames( pdecl, output )
char *pdecl;
FILE *output;
#endif
{
	int firstTime = 1, done = 0;
	require(output!=NULL, "DumpListOfParmNames: NULL parm");

	if ( pdecl == NULL ) return;
	while ( !done )
	{
		if ( !firstTime ) putc(',', output);
		done = DumpNextNameInDef(&pdecl, output);
		firstTime = 0;
	}
}

/* given a list of parameters or return values, dump the next
 * name to output.  Return 1 if last one just printed, 0 if more to go.
 */
int
#ifdef __USE_PROTOS
DumpNextNameInDef( char **q, FILE *output )
#else
DumpNextNameInDef( q, output )
char **q;
FILE *output;
#endif
{
	char *p = *q;		/* where did we leave off? */
	int done=0;

	while ( *p!='\0' && *p!=',' ) p++;		/* find end of decl */
	if ( *p == '\0' ) done = 1;
	while ( !isalnum(*p) && *p!='_' ) --p;	/* scan back until valid var character */
	while ( isalnum(*p) || *p=='_' ) --p;	/* scan back until beginning of variable */
	p++;						/* move to start of variable */
	while ( isalnum(*p) || *p=='_'  ) {putc(*p, output); p++;}
	while ( *p!='\0' && *p!=',' ) p++;		/* find end of decl */
	p++;				/* move past this parameter */

	*q = p;				/* record where we left off */
	return done;
}

/* Given a list of ANSI-style parameter declarations, dump K&R-style
 * declarations, one per line for each parameter.  Basically, convert
 * comma to semi-colon, newline.
 */
void
#ifdef __USE_PROTOS
DumpOldStyleParms( char *pdecl, FILE *output )
#else
DumpOldStyleParms( pdecl, output )
char *pdecl;
FILE *output;
#endif
{
	require(output!=NULL, "DumpOldStyleParms: NULL parm");

	if ( pdecl == NULL ) return;
	while ( *pdecl != '\0' )
	{
		if ( *pdecl == ',' )
		{
			pdecl++;
			putc(';', output); putc('\n', output);
			while ( *pdecl==' ' || *pdecl=='\t' || *pdecl=='\n' ) pdecl++;
		}
		else {putc(*pdecl, output); pdecl++;}
	}
	putc(';', output);
	putc('\n', output);
}

/* Take in a type definition (type + symbol) and print out type only */
void
#ifdef __USE_PROTOS
DumpType( char *s, FILE *f, char *file, int line )
#else
DumpType( s, f, file, line )
char *s;
FILE *f;
char *file;
int line;
#endif
{
	char *p, *end;
	require(s!=NULL, "DumpType: invalid type string");

	p = &s[strlen(s)-1];		/* start at end of string and work back */
	/* scan back until valid variable character */
	while ( !isalnum(*p) && *p!='_' ) --p;
	/* scan back until beginning of variable */
	while ( isalnum(*p) || *p=='_' ) --p;
	if ( p<=s )
	{
		warnFL(eMsg1("invalid parameter/return value: '%s'",s), file, line);
		return;
	}
	end = p;					/* here is where we stop printing alnum */
	p = s;
	while ( p!=end ) {putc(*p, f); p++;} /* dump until just before variable */
	while ( *p!='\0' )					 /* dump rest w/o variable */
	{
		if ( !isalnum(*p) && *p!='_' ) putc(*p, f);
		p++;
	}
}

char *
#ifdef __USE_PROTOS
actiondup(char *s)
#else
actiondup(s)
char *s;
#endif
{
	char *p = (char *) malloc(strlen(s)+1);
	require(p!=NULL, "actiondup: no memory");
	strcpy(p, s);
	return p;
}

#ifdef OLD
RefVarRec *
#ifdef __USE_PROTOS
newRefVarRec(char *t,char *lab, char *init)
#else
newRefVarRec(t,lab,init)
char *t,*lab,*init;
#endif
{
	RefVarRec *p;
	SymEntry *q;

	q = (SymEntry *) hash_get(symbols, lab);
	if ( q==NULL )
	{
		q = (SymEntry *) hash_add(symbols, lab, (Entry *) newSymEntry(lab));
		q->token = REFVAR;
		q->defined = 1;
	}
	else err(eMsg2("Reference variable clashes with %s: '%s'", zztokens[q->token], lab));

	p = (RefVarRec *) calloc(1, sizeof(RefVarRec));
	require(p!=NULL, "newRefVarRec: no memory");
	strcpy(p->type, t);
	strcpy(p->label, lab);
	strcpy(p->init, init);
	return p;
}
#endif

/* From one of the following ref var defs, create a record to track
 * the info:
 *
 *	    @(int *p);
 *	    @(int *p=NULL);		-- initialized
 *	    @(static int *p);	-- global
 *
 * The 'def' pointer should point to the first char after the '('
 */
RefVarRec *
#ifdef __USE_PROTOS
refVarRec(char *def)
#else
refVarRec(def)
char *def;
#endif
{
	RefVarRec *p;
	char *s,*decl;
	static char buf[MaxAtom+1];

	p = (RefVarRec *) calloc(1, sizeof(RefVarRec));
	require(p!=NULL, "newRefVarRec: no memory");

	/* find first word; must be type or "static" */
	s = def;
	while ( isalpha(*s) ) s++;
	if ( strncmp(def, "static", s-def)==0 ) p->global = 1;
	else s = def;

	/* now get type from s position until '=' or ')' */
	decl = s;
	while ( *s!='=' && *s!='\0' ) s++;
	if ( *s=='=' ) {
		/* there is an initializer */
		strcpy(p->init, s+1);
		*s='\0';
	}
	strcpy(p->decl, decl);
	strcpy(p->var, id_in_decl(decl));
	return p;
}

/* given a decl (type + id) return a ptr to the id; nondestructive to 'decl'.
 * Just scan backwards from the end looking for the start of the first id.
 */
char *
#ifdef __USE_PROTOS
id_in_decl( char *decl )
#else
id_in_decl( decl )
char *decl;
#endif
{
	static char id[MaxAtom+1];
	char *p = &(decl[strlen(decl)-1]);
	char *end, *start;
	require(decl!=NULL, "id_in_decl: NULL decl");
	require(strlen(decl)>(size_t)0, "id_in_decl: empty decl");

	/* scan back until valid var character */
    while ( !isalnum(*p) || *p=='_' ) --p;
	end = p+1;
	/* scan back until beginning of variable */
    while ( isalnum(*p) || *p=='_' ) --p;
    p++;                        /* move to start of variable */
	start = p;
	strncpy(id, start, end-start);
	id[end-start] = '\0';
	return id;
}

/* Take in a decl (type + id) and print it out with a prefix on id */
void
#ifdef __STDC__
dump_prefixed_decl( char *prefix, char *s, FILE *f )
#else
dump_prefixed_decl( prefix, s, f )
char *prefix;
char *s;
FILE *f;
#endif
{
    char *p, *id;
    require(s!=NULL, "invalid type string");

    p = &s[strlen(s)-1];        /* start at end of string and work back */
    /* scan back until valid variable character */
    while ( !isalnum(*p) && *p!='_' ) --p;
    /* scan back until beginning of variable */
    while ( isalnum(*p) || *p=='_' ) --p;
	require(p>s, "invalid decl");
	p++;
    id = p;                    /* here is where id is */
    p = s;
    while ( p!=id ) {fputc(*p, f); p++;} /* dump until just before variable */
	fprintf(f, prefix);
    while ( isspace(*p) ) p++;
    while ( *p!='\0' )                   /* dump rest */
    {
        fputc(*p, f);
        p++;
	}
}

/*
 * Convert
 *
 *  0, "#( RangeOp #(Minus %1:IConst %2:Var) #(Plus %3:Var %4:Var) )",
 *           t, &w, &x, &y, &z);
 *
 * to
 *
 *	0, "#( 6 #(5 %1:4 %2:3) #(1 %3:3 %4:3) )",
 *           t, &w, &x, &y, &z);
 */
char *
#ifdef __USE_PROTOS
cvt_token_str(char *prefix, char *s)
#else
cvt_token_str(prefix, s)
char *prefix, *s;
#endif
{
	SymEntry *q;
	char *b, *e, *p;
	char tname[MaxRuleName+1];
	char duh[MaxRuleName+1];
	static char call[MaxAtom+1];
	require(prefix!=NULL&&s!=NULL, "cvt_token_str: NULL string");
	require(s[0]!='\0', "cvt_token_str: empty string");

/*	printf("cvt incoming: '%s'\n", s);*/

	strcpy(call, prefix);
	p = &call[strlen(prefix)];

	while ( *s!='"' ) *p++ = *s++;	/* ignore until string start */
	*p++ = *s++;

	do
	{
		while ( !isalpha(*s) )
		{
			if ( *s=='"' ) { *p='\0'; goto done; }
			*p++ = *s++;	/* ignore until a token name */
		}
		*p='\0';
		b = s;
		while ( isalpha(*s) ) s++;	/* ignore until end of token name */
		e = s-1;
		strncpy(tname, b, e-b+1);
		tname[e-b+1] = '\0';

/*		printf("looking up %s\n", tname);*/
		q = (SymEntry *) hash_get(symbols, tname);
		if ( q==NULL )
		{
			warnNoFL(eMsg1("call to ast_scan() has reference to unknown token: '%s'",tname));
		}
		else
		{
			sprintf(duh, "%d", q->token_type);
			strcpy(p, duh);
			p += strlen(duh);
			*p='\0';
		}
	} while ( *s!='\0' && *s!='"' );
done:
	strcat(call, s);
/*	printf("cvt: result is '%s'\n", call);*/
	return call;
}