[go: up one dir, main page]

File: astlib.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 (830 lines) | stat: -rw-r--r-- 18,471 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
/*
 * astlib.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 <stdio.h>

#define SORCERER_TRANSFORM

#include "CASTBase.h"
#include "astlib.h"

#ifdef __USE_PROTOS
#include <stdarg.h>
#else
#include <varargs.h>
#endif

               /* String Scanning/Parsing Stuff */

#define StringScanMaxText	50

typedef struct stringlexer {
#ifdef __USE_PROTOS
			signed int c;
#else
			int c;
#endif
			char *input;
			char *p;
			char text[StringScanMaxText];
		} StringLexer;

#define LPAREN			1
#define RPAREN			2
#define PERCENT			3
#define INT				4
#define COLON			5
#define POUND			6
#define PERIOD			7
#define StringScanEOF	-1
#define VALID_SCAN_TOKEN(t)		(t>=LPAREN && t<=PERIOD)

static char *scan_token_tbl[] = {
	"invalid",	/*	0 */
	"LPAREN",	/*	1 */
	"RPAREN",	/*	2 */
	"PERCENT",	/*	3 */
	"INT",		/*	4 */
	"COLON",	/*	5 */
	"POUND",	/*	6 */
	"PERIOD",	/*	7 */
};

char *
#ifdef __USE_PROTOS
scan_token_str(int t)
#else
scan_token_str(t)
int t;
#endif
{
	if ( VALID_SCAN_TOKEN(t) ) return scan_token_tbl[t];
	else if ( t==StringScanEOF ) return "<end-of-string>";
	else return "<invalid-token>";
}

typedef struct stringparser {
			int token;
			StringLexer *lexer;
			int num_labels;
		} StringParser;

          /* This type ONLY USED by ast_scan() */

typedef struct _scanast {
            struct _scanast *right, *down;
            int token;
			int label_num;
        } ScanAST;

#ifdef __USE_PROTOS
static void stringlexer_init(StringLexer *scanner, char *input);
static void stringparser_init(StringParser *, StringLexer *);
static ScanAST *stringparser_parse_scanast(char *templ, int *n);
static ScanAST *stringparser_parse_tree(StringParser *parser);
static ScanAST *stringparser_parse_element(StringParser *parser);
static void stringscan_advance(StringLexer *scanner);
static int stringscan_gettok(StringLexer *scanner);
#else
static void stringlexer_init();
static void stringparser_init();
static ScanAST *stringparser_parse_scanast();
static ScanAST *stringparser_parse_tree();
static ScanAST *stringparser_parse_element();
static void stringscan_advance();
static int stringscan_gettok();
#endif

/* build a tree (root child1 child2 ... NULL)
 * If root is NULL, simply make the children siblings and return ptr
 * to 1st sibling (child1).  If root is not single node, return NULL.
 *
 * Siblings that are actually sibling lists themselves are handled
 * correctly.  For example #( NULL, #( NULL, A, B, C), D) results
 * in the tree ( NULL A B C D ).
 *
 * Requires at least two parameters with the last one being NULL.  If
 * both are NULL, return NULL.
 *
 * The ast_down and ast_right down/right pointers are used to make the tree.
 */
SORAST *
#ifdef __USE_PROTOS
ast_make(SORAST *rt, ...)
#else
ast_make(va_alist)
va_dcl
#endif
{
	va_list ap;
	register SORAST *child, *sibling=NULL, *tail, *w;
	SORAST *root;

#ifdef __USE_PROTOS
	va_start(ap, rt);
	root = rt;
#else
	va_start(ap);
	root = va_arg(ap, SORAST *);
#endif

	if ( root != NULL )
		if ( root->ast_down != NULL ) return NULL;
	child = va_arg(ap, SORAST *);
	while ( child != NULL )
	{
		/* find end of child */
		for (w=child; w->ast_right!=NULL; w=w->ast_right) {;}
		if ( sibling == NULL ) {sibling = child; tail = w;}
		else {tail->ast_right = child; tail = w;}
		child = va_arg(ap, SORAST *);
	}
	if ( root==NULL ) root = sibling;
	else root->ast_down = sibling;
	va_end(ap);
	return root;
}

/* The following push and pop routines are only used by ast_find_all() */

static void
#ifdef __USE_PROTOS
_push(SORAST **st, int *sp, SORAST *e)
#else
_push(st, sp, e)
SORAST **st;
int *sp;
SORAST *e;
#endif
{
	(*sp)--;
	require((*sp)>=0, "stack overflow");
	st[(*sp)] = e;
}

static SORAST *
#ifdef __USE_PROTOS
_pop(SORAST **st, int *sp)
#else
_pop(st, sp)
SORAST **st;
int *sp;
#endif
{
	SORAST *e = st[*sp];
	(*sp)++;
	require((*sp)<=MaxTreeStackDepth, "stack underflow");
	return e;
}

/* Find all occurrences of u in t.
 * 'cursor' must be initialized to 't'.  It eventually
 * returns NULL when no more occurrences of 'u' are found.
 */
SORAST *
#ifdef __USE_PROTOS
ast_find_all(SORAST *t, SORAST *u, SORAST **cursor)
#else
ast_find_all(t, u, cursor)
SORAST *t, *u, **cursor;
#endif
{
	SORAST *sib;
	static SORAST *template_stack[MaxTreeStackDepth];
	static int tsp = MaxTreeStackDepth;
	static int nesting = 0;

	if ( *cursor == NULL ) return NULL;
	if ( *cursor!=t ) sib = *cursor;
	else {
		/* else, first time--start at top of template 't' */
		tsp = MaxTreeStackDepth;
		sib = t;
		/* bottom of stack is always a NULL--"cookie" indicates "done" */
		_push(template_stack, &tsp, NULL);
	}

keep_looking:
	if ( sib==NULL )	/* hit end of sibling list */
	{
		sib = _pop(template_stack, &tsp);
		if ( sib == NULL ) { *cursor = NULL; return NULL; }
	}

	if ( sib->token != u->token )
	{
		/* look for another match */
		if ( sib->ast_down!=NULL )
		{
			if ( sib->ast_right!=NULL ) _push(template_stack, &tsp, sib->ast_right);
			sib=sib->ast_down;
			goto keep_looking;
		}
		/* nothing below to try, try next sibling */
		sib=sib->ast_right;
		goto keep_looking;
	}

	/* found a matching root node, try to match what's below */
	if ( ast_match_partial(sib, u) )
	{
		/* record sibling cursor so we can pick up next from there */
		if ( sib->ast_down!=NULL )
		{
			if ( sib->ast_right!=NULL ) _push(template_stack, &tsp, sib->ast_right);
			*cursor = sib->ast_down;
		}
		else if ( sib->ast_right!=NULL ) *cursor = sib->ast_right;
		else *cursor = _pop(template_stack, &tsp);
		return sib;
	}

	/* no match, keep searching */
	if ( sib->ast_down!=NULL )
	{
		if ( sib->ast_right!=NULL ) _push(template_stack, &tsp, sib->ast_right);
		sib=sib->ast_down;
	}
	else sib = sib->ast_right;	/* else, try to right if zip below */
	goto keep_looking;
}

/* are two trees exactly alike? */
int
#ifdef __USE_PROTOS
ast_match(SORAST *t, SORAST *u)
#else
ast_match(t, u)
SORAST *t, *u;
#endif
{
	SORAST *sib;

	if ( t==NULL ) if ( u!=NULL ) return 0; else return 1;
	if ( u==NULL ) return 0;

	for (sib=t; sib!=NULL&&u!=NULL; sib=sib->ast_right, u=u->ast_right)
	{
		if ( sib->token != u->token ) return 0;
		if ( sib->ast_down!=NULL )
			if ( !ast_match(sib->ast_down, u->ast_down) ) return 0;
	}
	return 1;
}

/* Is 'u' a subtree of 't' beginning at the root? */
int
#ifdef __USE_PROTOS
ast_match_partial(SORAST *t, SORAST *u)
#else
ast_match_partial(t, u)
SORAST *t, *u;
#endif
{
	SORAST *sib;

	if ( u==NULL ) return 1;
	if ( t==NULL ) if ( u!=NULL ) return 0; else return 1;

	for (sib=t; sib!=NULL&&u!=NULL; sib=sib->ast_right, u=u->ast_right)
	{
		if ( sib->token != u->token ) return 0;
		if ( sib->ast_down!=NULL )
			if ( !ast_match_partial(sib->ast_down, u->ast_down) ) return 0;
	}
	return 1;
}

static int
#ifdef __USE_PROTOS
ast_scanmatch(ScanAST *t, SORAST *u, SORAST **labels[], int *n)
#else
ast_scanmatch(t, u, labels, n)
ScanAST *t;
SORAST *u;
SORAST **labels[];
int *n;
#endif
{
	ScanAST *sib;

	if ( t==NULL ) if ( u!=NULL ) return 0; else return 1;
	if ( u==NULL ) return 0;

	for (sib=t; sib!=NULL&&u!=NULL; sib=sib->right, u=u->ast_right)
	{
		/* make sure tokens match; token of '0' means wildcard match */
		if ( sib->token != u->token && sib->token!=0 ) return 0;
		/* we have a matched token here; set label pointers if exists */
		if ( sib->label_num>0 )
		{
			require(labels!=NULL, "label found in template, but no array of labels");
			(*n)++;
			*(labels[sib->label_num-1]) = u;
		}
		/* match what's below if something there and current node is not wildcard */
		if ( sib->down!=NULL && sib->token!=0 )
			if ( !ast_scanmatch(sib->down, u->ast_down, labels, n) ) return 0;
	}
	return 1;
}

void
#ifdef __USE_PROTOS
ast_insert_after(SORAST *a, SORAST *b)
#else
ast_insert_after(a, b)
SORAST *a,*b;
#endif
{
	SORAST *end;
	require(a!=NULL, "ast_insert_after: NULL input tree");
	if ( b==NULL ) return;
	/* find end of b's child list */
	for (end=b; end->ast_right!=NULL; end=end->ast_right) {;}
	end->ast_right = a->ast_right;
	a->ast_right = b;
}

void
#ifdef __USE_PROTOS
ast_append(SORAST *a, SORAST *b)
#else
ast_append(a, b)
SORAST *a,*b;
#endif
{
	SORAST *end;
	require(a!=NULL&&b!=NULL, "ast_append: NULL input tree");
	/* find end of child list */
	for (end=a; end->ast_right!=NULL; end=end->ast_right) {;}
	end->ast_right = b;
}

SORAST *
#ifdef __USE_PROTOS
ast_tail(SORAST *a)
#else
ast_tail(a)
SORAST *a;
#endif
{
	SORAST *end;
	require(a!=NULL, "ast_tail: NULL input tree");
	/* find end of child list */
	for (end=a; end->ast_right!=NULL; end=end->ast_right) {;}
	return end;
}

SORAST *
#ifdef __USE_PROTOS
ast_bottom(SORAST *a)
#else
ast_bottom(a)
SORAST *a;
#endif
{
	SORAST *end;
	require(a!=NULL, "ast_bottom: NULL input tree");
	/* find end of child list */
	for (end=a; end->ast_down!=NULL; end=end->ast_down) {;}
	return end;
}

SORAST *
#ifdef __USE_PROTOS
ast_cut_between(SORAST *a, SORAST *b)
#else
ast_cut_between(a, b)
SORAST *a,*b;
#endif
{
	SORAST *end, *ret;
	require(a!=NULL&&b!=NULL, "ast_cut_between: NULL input tree");
	/* find node pointing to b */
	for (end=a; end->ast_right!=NULL&&end->ast_right!=b; end=end->ast_right)
		{;}
	require(end->ast_right!=NULL, "ast_cut_between: a,b not connected");
	end->ast_right = NULL;	/* don't want it point to 'b' anymore */
	ret = a->ast_right;
	a->ast_right = b;
	return ret;
}

SList *
#ifdef __USE_PROTOS
ast_to_slist(SORAST *t)
#else
ast_to_slist(t)
SORAST *t;
#endif
{
	SList *list=NULL;
	SORAST *p;

	for (p=t; p!=NULL; p=p->ast_right)
	{
		slist_add(&list, p);
	}
	return list;
}

SORAST *
#ifdef __USE_PROTOS
slist_to_ast(SList *list)
#else
slist_to_ast(list)
SList *list;
#endif
{
	SORAST *t=NULL, *last=NULL;
	SList *p;

	for (p = list->next; p!=NULL; p=p->next)
	{
		SORAST *u = (SORAST *)p->elem;
		if ( last==NULL ) last = t = u;
		else { last->ast_right = u; last = u; }
	}
	return t;
}

void
#ifdef __USE_PROTOS
ast_free(SORAST *t)
#else
ast_free(t)
SORAST *t;
#endif
{
    if ( t == NULL ) return;
    ast_free( t->ast_down );
    ast_free( t->ast_right );
    free( t );
}

int
#ifdef __USE_PROTOS
ast_nsiblings(SORAST *t)
#else
ast_nsiblings(t)
SORAST *t;
#endif
{
	int n=0;

	while ( t!=NULL )
	{
		n++;
		t = t->ast_right;
	}
	return n;
}

SORAST *
#ifdef __USE_PROTOS
ast_sibling_index(SORAST *t, int i)
#else
ast_sibling_index(t,i)
SORAST *t;
int i;
#endif
{
	int j=1;
	require(i>0, "ast_sibling_index: i<=0");

	while ( t!=NULL )
	{
		if ( j==i ) return t;
		j++;
		t = t->ast_right;
	}
	return NULL;
}

static void
#ifdef __USE_PROTOS
scanast_free(ScanAST *t)
#else
scanast_free(t)
ScanAST *t;
#endif
{
    if ( t == NULL ) return;
    scanast_free( t->down );
    scanast_free( t->right );
    free( t );
}

/*
 * ast_scan
 *
 * This function is like scanf(): it attempts to match a template
 * against an input tree.  A variable number of tree pointers
 * may be set according to the '%i' labels in the template string.
 * For example:
 *
 *   ast_scan("#( 6 #(5 %1:4 %2:3) #(1 %3:3 %4:3) )",
 *            t, &w, &x, &y, &z);
 *
 * Naturally, you'd want this converted from
 *
 *	 ast_scan("#( RangeOp #(Minus %1:IConst %2:Var) #(Plus %3:Var %4Var) )",
 *			  t, &w, &x, &y, &z);
 *
 * by SORCERER.
 *
 * This function call must be done withing a SORCERER file because SORCERER
 * must convert the token references to the associated token number.
 *
 * This functions parses the template and creates trees which are then
 * matched against the input tree.  The labels are set as they are
 * encountered; hence, partial matches may leave some pointers set
 * and some NULL.  This routines initializes all argument pointers to NULL
 * at the beginning.
 *
 * This function returns the number of labels matched.
 */
int
#ifdef __USE_PROTOS
ast_scan(char *templ, SORAST *tree, ...)
#else
ast_scan(va_alist)
va_dcl
#endif
{
	va_list ap;
	ScanAST *t;
	int n, i, found=0;
	SORAST ***label_ptrs=NULL;

#ifdef __USE_PROTOS
	va_start(ap, tree);
#else
	char *templ;
	SORAST *tree;

	va_start(ap);
	templ = va_arg(ap, char *);
	tree = va_arg(ap, SORAST *);
#endif

	/* make a ScanAST tree out of the template */
	t = stringparser_parse_scanast(templ, &n);

	/* make an array out of the labels */
	if ( n>0 )
	{
		label_ptrs = (SORAST ***) calloc(n, sizeof(SORAST **));
		require(label_ptrs!=NULL, "ast_scan: out of memory");
		for (i=1; i<=n; i++)
		{
			label_ptrs[i-1] = va_arg(ap, SORAST **);
			*(label_ptrs[i-1]) = NULL;
		}
	}

	/* match the input tree against the template */
	ast_scanmatch(t, tree, label_ptrs, &found);

	scanast_free(t);
	free(label_ptrs);

	return found;
}

static ScanAST *
#ifdef __USE_PROTOS
new_scanast(int tok)
#else
new_scanast(tok)
int tok;
#endif
{
    ScanAST *p = (ScanAST *) calloc(1, sizeof(ScanAST));
    if ( p == NULL ) {fprintf(stderr, "out of mem\n"); exit(-1);}
	p->token = tok;
	return p;
}

static ScanAST *
#ifdef __USE_PROTOS
stringparser_parse_scanast(char *templ, int *num_labels)
#else
stringparser_parse_scanast(templ, num_labels)
char *templ;
int *num_labels;
#endif
{
	StringLexer lex;
	StringParser parser;
	ScanAST *t;

	stringlexer_init(&lex, templ);
	stringparser_init(&parser, &lex);
	t = stringparser_parse_tree(&parser);
	*num_labels = parser.num_labels;
	return t;
}

static void
#ifdef __USE_PROTOS
stringparser_match(StringParser *parser, int token)
#else
stringparser_match(parser, token)
StringParser *parser;
int token;
#endif
{
	if ( parser->token != token ) sorcerer_panic("bad tree in ast_scan()");
}

/*
 * Match a tree of the form:
 *		(root child1 child2 ... childn)
 * or,
 *		node
 *
 * where the elements are integers or labeled integers.
 */
static ScanAST *
#ifdef __USE_PROTOS
stringparser_parse_tree(StringParser *parser)
#else
stringparser_parse_tree(parser)
StringParser *parser;
#endif
{
	ScanAST *t=NULL, *root, *child, *last;

	if ( parser->token != POUND )
	{
		return stringparser_parse_element(parser);
	}
	stringparser_match(parser,POUND);
	parser->token = stringscan_gettok(parser->lexer);
	stringparser_match(parser,LPAREN);
	parser->token = stringscan_gettok(parser->lexer);
	root = stringparser_parse_element(parser);
	while ( parser->token != RPAREN )
	{
		child = stringparser_parse_element(parser);
		if ( t==NULL ) { t = child; last = t; }
		else { last->right = child; last = child; }
	}
	stringparser_match(parser,RPAREN);
	parser->token = stringscan_gettok(parser->lexer);
	root->down = t;
	return root;
}

static ScanAST *
#ifdef __USE_PROTOS
stringparser_parse_element(StringParser *parser)
#else
stringparser_parse_element(parser)
StringParser *parser;
#endif
{
	static char ebuf[100];
	int label = 0;

	if ( parser->token == POUND )
	{
		return stringparser_parse_tree(parser);
	}
	if ( parser->token == PERCENT )
	{
		parser->token = stringscan_gettok(parser->lexer);
		stringparser_match(parser,INT);
		label = atoi(parser->lexer->text);
		parser->num_labels++;
		if ( label==0 ) sorcerer_panic("%%0 is an invalid label");
		parser->token = stringscan_gettok(parser->lexer);
		stringparser_match(parser,COLON);
		parser->token = stringscan_gettok(parser->lexer);
		/* can label tokens and wildcards */
		if ( parser->token != INT && parser->token != PERIOD )
			sorcerer_panic("can only label tokens");
	}
	if ( parser->token == INT )
	{
		ScanAST *p = new_scanast(atoi(parser->lexer->text));
		parser->token = stringscan_gettok(parser->lexer);
		p->label_num = label;
		return p;
	}
	if ( parser->token == PERIOD )
	{
		ScanAST *p = new_scanast(0);	/* token of 0 is wildcard */
		parser->token = stringscan_gettok(parser->lexer);
		p->label_num = label;
		return p;
	}
	sprintf(ebuf, "mismatch token in ast_scan(): %s", scan_token_str(parser->token));
	sorcerer_panic(ebuf);
}

static void
#ifdef __USE_PROTOS
stringparser_init(StringParser *parser, StringLexer *input)
#else
stringparser_init(parser, input)
StringParser *parser;
StringLexer *input;
#endif
{
	parser->lexer = input;
	parser->token = stringscan_gettok(parser->lexer);
	parser->num_labels = 0;
}

static void
#ifdef __USE_PROTOS
stringlexer_init(StringLexer *scanner, char *input)
#else
stringlexer_init(scanner, input)
StringLexer *scanner;
char *input;
#endif
{
	scanner->text[0]='\0';
	scanner->input = input;
	scanner->p = input;
	stringscan_advance(scanner);
}

static void
#ifdef __USE_PROTOS
stringscan_advance(StringLexer *scanner)
#else
stringscan_advance(scanner)
StringLexer *scanner;
#endif
{
	if ( *(scanner->p) == '\0' ) scanner->c = StringScanEOF;
	scanner->c = *(scanner->p)++;
}

static int
#ifdef __USE_PROTOS
stringscan_gettok(StringLexer *scanner)
#else
stringscan_gettok(scanner)
StringLexer *scanner;
#endif
{
	char *index = &scanner->text[0];
	static char ebuf[100];

	while ( isspace(scanner->c) ) { stringscan_advance(scanner); }
	if ( isdigit(scanner->c) )
	{
		int tok = INT;
		while ( isdigit(scanner->c) ) {
			*index++ = scanner->c;
			stringscan_advance(scanner);
		}
		*index = '\0';
		return tok;
	}
	switch ( scanner->c )
	{
		case '#' : stringscan_advance(scanner); return POUND;
		case '(' : stringscan_advance(scanner); return LPAREN;
		case ')' : stringscan_advance(scanner); return RPAREN;
		case '%' : stringscan_advance(scanner); return PERCENT;
		case ':' : stringscan_advance(scanner); return COLON;
		case '.' : stringscan_advance(scanner); return PERIOD;
		case '\0' : return StringScanEOF;
		case StringScanEOF : return StringScanEOF;
		default  :
			sprintf(ebuf, "invalid char in ast_scan: '%c'", scanner->c);
			sorcerer_panic(ebuf);
	}
}