1 /* dlgauto.h automaton
2  *
3  * SOFTWARE RIGHTS
4  *
5  * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
6  * Set (PCCTS) -- PCCTS is in the public domain.  An individual or
7  * company may do whatever they wish with source code distributed with
8  * PCCTS or the code generated by PCCTS, including the incorporation of
9  * PCCTS, or its output, into commerical software.
10  *
11  * We encourage users to develop software with PCCTS.  However, we do ask
12  * that credit is given to us for developing PCCTS.  By "credit",
13  * we mean that if you incorporate our source code into one of your
14  * programs (commercial product, research project, or otherwise) that you
15  * acknowledge this fact somewhere in the documentation, research report,
16  * etc...  If you like PCCTS and have developed a nice tool with the
17  * output, please mention that you developed it using PCCTS.  In
18  * addition, we ask that this header remain intact in our source code.
19  * As long as these guidelines are kept, we expect to continue enhancing
20  * this system and expect to make other tools available as they are
21  * completed.
22  *
23  * ANTLR 1.33
24  * Will Cohen and Terence Parr
25  * Parr Research Corporation
26  * with Purdue University and AHPCRC, University of Minnesota
27  * 1989-2000
28  */
29 
30 #ifndef ZZDEFAUTO_H
31 #define ZZDEFAUTO_H
32 
33 /*  10-Apr-97 133MR1	Uses __USE_PROTOS show should #include pcctscfg.h */
34 
35 #include "pcctscfg.h"
36 
37 zzchar_t	*zzlextext;	/* text of most recently matched token */
38 zzchar_t	*zzbegexpr;	/* beginning of last reg expr recogn. */
39 zzchar_t	*zzendexpr;	/* beginning of last reg expr recogn. */
40 int	zzbufsize = 0;	/* number of characters in zzlextext */          /* MR7 */
41 int	zzbegcol = 0;	/* column that first character of token is in*/
42 int	zzendcol = 0;	/* column that last character of token is in */
43 int	zzline = 1;	/* line current token is on */
44 int	zzreal_line=1;	/* line of 1st portion of token that is not skipped */
45 int	zzchar;		/* character to determine next state */
46 int	zzbufovf;	/* indicates that buffer too small for text */
47 int	zzcharfull = 0;
48 static zzchar_t	*zznextpos;/* points to next available position in zzlextext*/
49 static int 	zzclass;
50 
51 #ifdef __USE_PROTOS
52 void	zzerrstd(const char *);
53 void	(*zzerr)(const char *)=zzerrstd;/* pointer to error reporting function */
54 extern int	zzerr_in(void);
55 static int	(*zzfunc_in)(void) = zzerr_in;  /* MR20 */
56 #else
57 void	zzerrstd();
58 void	(*zzerr)()=zzerrstd;	/* pointer to error reporting function */
59 extern int	zzerr_in();
60 static int	(*zzfunc_in)() = zzerr_in;      /* MR20 */
61 #endif
62 
63 static FILE	*zzstream_in=0;
64 static zzchar_t	*zzstr_in=0;
65 
66 #ifdef USER_ZZMODE_STACK
67 int 	          zzauto = 0;
68 #else
69 static int     zzauto = 0;
70 #endif
71 static int	zzadd_erase;
72 static char 	zzebuf[70];
73 
74 #ifdef ZZCOL
75 #define ZZINC (++zzendcol)
76 #else
77 #define ZZINC
78 #endif
79 
80 
81 #define ZZGETC_STREAM {zzchar = getc(zzstream_in); zzclass = ZZSHIFT(zzchar);}
82 #define ZZGETC_FUNC {zzchar = (*zzfunc_in)(); zzclass = ZZSHIFT(zzchar);}
83 #define ZZGETC_STR { 			\
84 	if (*zzstr_in){				\
85 		zzchar = *zzstr_in;		\
86 		++zzstr_in;				\
87 	}else{						\
88 		zzchar = EOF;			\
89 	}							\
90 	zzclass = ZZSHIFT(zzchar);	\
91 }
92 
93 #define ZZNEWSTATE	(newstate = dfa[state][zzclass])
94 
95 #ifndef ZZCOPY
96 #define ZZCOPY	\
97 	/* Truncate matching buffer to size (not an error) */	\
98 	if (zznextpos < lastpos){				\
99 		*(zznextpos++) = zzchar;			\
100 	}else{							\
101 		zzbufovf = 1;					\
102 	}
103 #endif
104 
105 void
106 #ifdef __USE_PROTOS
zzrdstream(FILE * f)107 zzrdstream( FILE *f )
108 #else
109 zzrdstream( f )
110 FILE *f;
111 #endif
112 {
113 	/* make sure that it is really set to something, otherwise just
114 	   leave it be.
115 	*/
116 	if (f){
117 		/* make sure that there is always someplace to get input
118 		   before closing zzstream_in
119 		*/
120 #if 0
121 		if (zzstream_in && zzstream_in!=stdin) fclose( zzstream_in );
122 #endif
123 		zzline = 1;
124 		zzstream_in = f;
125 		zzfunc_in = NULL;
126 		zzstr_in = 0;
127 		zzcharfull = 0;
128 	}
129 }
130 
131 void
132 #ifdef __USE_PROTOS
zzrdfunc(int (* f)(void))133 zzrdfunc( int (*f)(void) )
134 #else
135 zzrdfunc( f )
136 int (*f)();
137 #endif
138 {
139 	/* make sure that it is really set to something, otherwise just
140 	   leave it be.
141 	*/
142 	if (f){
143 		/* make sure that there is always someplace to get input
144 		   before closing zzstream_in
145 		*/
146 #if 0
147 		if (zzstream_in && zzstream_in!=stdin) fclose( zzstream_in );
148 #endif
149 		zzline = 1;
150 		zzstream_in = NULL;
151 		zzfunc_in = f;
152 		zzstr_in = 0;
153 		zzcharfull = 0;
154 	}
155 }
156 
157 
158 void
159 #ifdef __USE_PROTOS
zzrdstr(zzchar_t * s)160 zzrdstr( zzchar_t *s )
161 #else
162 zzrdstr( s )
163 zzchar_t *s;
164 #endif
165 {
166 	/* make sure that it is really set to something, otherwise just
167 	   leave it be.
168 	*/
169 	if (s){
170 		/* make sure that there is always someplace to get input
171 		   before closing zzstream_in
172 		*/
173 #if 0
174 		if (zzstream_in && zzstream_in!=stdin) fclose( zzstream_in );
175 #endif
176 		zzline = 1;
177 		zzstream_in = NULL;
178 		zzfunc_in = 0;
179 		zzstr_in = s;
180 		zzcharfull = 0;
181 	}
182 }
183 
184 
185 #ifdef __USE_PROTOS
zzclose_stream(void)186 void zzclose_stream(void)
187 #else
188 void zzclose_stream()
189 #endif
190 {
191 #if 0
192 	fclose( zzstream_in );
193 	zzstream_in = NULL;
194 	zzfunc_in = NULL;
195 #endif
196 }
197 
198 /* saves dlg state, but not what feeds dlg (such as file position) */
199 void
200 #ifdef __USE_PROTOS
zzsave_dlg_state(struct zzdlg_state * state)201 zzsave_dlg_state(struct zzdlg_state *state)
202 #else
203 zzsave_dlg_state(state)
204 struct zzdlg_state *state;
205 #endif
206 {
207 	state->stream = zzstream_in;
208 	state->func_ptr = zzfunc_in;
209 	state->str = zzstr_in;
210 	state->auto_num = zzauto;
211 	state->add_erase = zzadd_erase;
212 	state->lookc = zzchar;
213 	state->char_full = zzcharfull;
214 	state->begcol = zzbegcol;
215 	state->endcol = zzendcol;
216 	state->line = zzline;
217 	state->lextext = zzlextext;
218 	state->begexpr = zzbegexpr;
219 	state->endexpr = zzendexpr;
220 	state->bufsize = zzbufsize;
221 	state->bufovf = zzbufovf;
222 	state->nextpos = zznextpos;
223 	state->class_num = zzclass;
224 }
225 
226 void
227 #ifdef __USE_PROTOS
zzrestore_dlg_state(struct zzdlg_state * state)228 zzrestore_dlg_state(struct zzdlg_state *state)
229 #else
230 zzrestore_dlg_state(state)
231 struct zzdlg_state *state;
232 #endif
233 {
234 	zzstream_in = state->stream;
235 	zzfunc_in = state->func_ptr;
236 	zzstr_in = state->str;
237 	zzauto = state->auto_num;
238 	zzadd_erase = state->add_erase;
239 	zzchar = state->lookc;
240 	zzcharfull = state->char_full;
241 	zzbegcol = state->begcol;
242 	zzendcol = state->endcol;
243 	zzline = state->line;
244 	zzlextext = state->lextext;
245 	zzbegexpr = state->begexpr;
246 	zzendexpr = state->endexpr;
247 	zzbufsize = state->bufsize;
248 	zzbufovf = state->bufovf;
249 	zznextpos = state->nextpos;
250 	zzclass = state->class_num;
251 }
252 
253 void
254 #ifdef __USE_PROTOS
zzmode(int m)255 zzmode( int m )
256 #else
257 zzmode( m )
258 int m;
259 #endif
260 {
261 	/* points to base of dfa table */
262 	if (m<MAX_MODE){
263 		zzauto = m;
264 		/* have to redo class since using different compression */
265 		zzclass = ZZSHIFT(zzchar);
266 	}else{
267 		sprintf(zzebuf,"Invalid automaton mode = %d ",m);
268 		zzerr(zzebuf);
269 	}
270 }
271 
272 /* erase what is currently in the buffer, and get a new reg. expr */
273 
274 #ifdef __USE_PROTOS
zzskip(void)275 void zzskip(void)
276 #else
277 void zzskip()
278 #endif
279 {
280 	zzadd_erase = 1;
281 }
282 
283 /* don't erase what is in the zzlextext buffer, add on to it */
284 #ifdef __USE_PROTOS
zzmore()285 void zzmore()
286 #else
287 void zzmore()
288 #endif
289 {
290 	zzadd_erase = 2;
291 }
292 
293 /* substitute c for the reg. expr last matched and is in the buffer */
294 #ifdef __USE_PROTOS
295 void
zzreplchar(zzchar_t c)296 zzreplchar(zzchar_t c)
297 #else
298 void
299 zzreplchar(c)
300 zzchar_t c;
301 #endif
302 {
303 	/* can't allow overwriting null at end of string */
304 	if (zzbegexpr < &zzlextext[zzbufsize-1]){
305 		*zzbegexpr = c;
306 		*(zzbegexpr+1) = '\0';
307 	}
308 	zzendexpr = zzbegexpr;
309 	if (c != '\0') {
310 		zznextpos = zzbegexpr + 1;
311 	}
312 	else {
313 		zznextpos = zzbegexpr;	/* MR30 Zero terminates string. */
314 	}
315 }
316 
317 /* replace the string s for the reg. expr last matched and in the buffer */
318 void
319 #ifdef __USE_PROTOS
zzreplstr(register zzchar_t * s)320 zzreplstr(register zzchar_t *s)
321 #else
322 zzreplstr(s)
323 register zzchar_t *s;
324 #endif
325 {
326 	register zzchar_t *l= &zzlextext[zzbufsize -1];
327 
328 	zznextpos = zzbegexpr;
329 	if (s){
330 	 	while ((zznextpos <= l) && (*(zznextpos++) = *(s++))!=0){
331 			/* empty */
332 		}
333 		/* correct for NULL at end of string */
334 		zznextpos--;
335 	}
336 	if ((zznextpos <= l) && (*(--s) == 0)){
337 		zzbufovf = 0;
338 	}else{
339 		zzbufovf = 1;
340 	}
341 	*(zznextpos) = '\0';
342 	zzendexpr = zznextpos - 1;
343 }
344 
345 #ifdef __USE_PROTOS
zzgettok(void)346 void zzgettok(void)
347 #else
348 void zzgettok()
349 #endif
350 {
351 	register int state, newstate;
352 	/* last space reserved for the null char */
353 	zzchar_t *lastpos;  /* MR27 Remove register since address operator used. */
354 
355 skip:
356 	zzreal_line = zzline;
357 	zzbufovf = 0;
358 	lastpos = &zzlextext[zzbufsize-1];
359 	zznextpos = zzlextext;
360 	zzbegcol = zzendcol+1;
361 more:
362 	zzbegexpr = zznextpos;
363 #ifdef ZZINTERACTIVE
364 	/* interactive version of automaton */
365 	/* if there is something in zzchar, process it */
366 	state = newstate = dfa_base[zzauto];
367 	if (zzcharfull){
368 		ZZINC;
369 		ZZCOPY;
370 		ZZNEWSTATE;
371 	}
372 	if (zzstr_in)
373 		while (zzalternatives[newstate]){
374 			state = newstate;
375 			ZZGETC_STR;
376 			ZZINC;
377 			ZZCOPY;
378 			ZZNEWSTATE;
379 		}
380 	else if (zzstream_in)
381 		while (zzalternatives[newstate]){
382 			state = newstate;
383 			ZZGETC_STREAM;
384 			ZZINC;
385 			ZZCOPY;
386 			ZZNEWSTATE;
387 		}
388 	else if (zzfunc_in)
389 		while (zzalternatives[newstate]){
390 			state = newstate;
391 			ZZGETC_FUNC;
392 			ZZINC;
393 			ZZCOPY;
394 			ZZNEWSTATE;
395 		}
396 	/* figure out if last character really part of token */
397 	if ((state != dfa_base[zzauto]) && (newstate == DfaStates)){
398 		zzcharfull = 1;
399 		--zznextpos;
400 	}else{
401 		zzcharfull = 0;
402 		state = newstate;
403 	}
404 	*(zznextpos) = '\0';
405 	/* Able to transition out of start state to some non err state?*/
406 	if ( state == dfa_base[zzauto] ){
407 		/* make sure doesn't get stuck */
408 		zzadvance();
409 	}
410 #else
411 	/* non-interactive version of automaton */
412 	if (!zzcharfull)
413 		zzadvance();
414 	else
415 		ZZINC;
416 	state = dfa_base[zzauto];
417 	if (zzstr_in)
418 		while (ZZNEWSTATE != DfaStates){
419 			state = newstate;
420 			ZZCOPY;
421 			ZZGETC_STR;
422 			ZZINC;
423 		}
424 	else if (zzstream_in)
425 		while (ZZNEWSTATE != DfaStates){
426 			state = newstate;
427 			ZZCOPY;
428 			ZZGETC_STREAM;
429 			ZZINC;
430 		}
431 	else if (zzfunc_in)
432 		while (ZZNEWSTATE != DfaStates){
433 			state = newstate;
434 			ZZCOPY;
435 			ZZGETC_FUNC;
436 			ZZINC;
437 		}
438 	zzcharfull = 1;
439 	if ( state == dfa_base[zzauto] ){
440 		if (zznextpos < lastpos){
441 			*(zznextpos++) = zzchar;
442 		}else{
443 			zzbufovf = 1;
444 		}
445 		*zznextpos = '\0';
446 		/* make sure doesn't get stuck */
447 		zzadvance();
448 	}else{
449 		*zznextpos = '\0';
450 	}
451 #endif
452 #ifdef ZZCOL
453 	zzendcol -= zzcharfull;
454 #endif
455 	zzendexpr = zznextpos -1;
456 	zzadd_erase = 0;
457 	(*actions[accepts[state]])();
458 	switch (zzadd_erase) {
459 		case 1: goto skip;
460 		case 2: goto more;
461 	}
462 }
463 
464 #ifdef __USE_PROTOS
zzadvance(void)465 void zzadvance(void)
466 #else
467 void zzadvance()
468 #endif
469 {
470 	if (zzstream_in) { ZZGETC_STREAM; zzcharfull = 1; ZZINC;}
471 	if (zzfunc_in) { ZZGETC_FUNC; zzcharfull = 1; ZZINC;}
472 	if (zzstr_in) { ZZGETC_STR; zzcharfull = 1; ZZINC;}
473 	if (!(zzstream_in || zzfunc_in || zzstr_in)){
474 		zzerr_in();
475 	}
476 }
477 
478 void
479 #ifdef __USE_PROTOS
zzerrstd(const char * s)480 zzerrstd(const char *s)
481 #else
482 zzerrstd(s)
483 char *s;
484 #endif
485 {
486         zzLexErrCount++;                /* MR11 */
487         fprintf(stderr,
488                 "%s near line %d (text was '%s')\n",
489                 ((s == NULL) ? "Lexical error" : s),
490                 zzline,zzlextext);
491 }
492 
493 #ifdef __USE_PROTOS
zzerr_in(void)494 int zzerr_in(void)
495 #else
496 int zzerr_in()
497 #endif
498 {
499 	fprintf(stderr,"No input stream, function, or string\n");
500 	/* return eof to get out gracefully */
501 	return EOF;
502 }
503 
504 #endif
505