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.
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
24 * Will Cohen and Terence Parr
25 * Parr Research Corporation
26 * with Purdue University and AHPCRC, University of Minnesota
33 zzchar_t *zzlextext; /* text of most recently matched token */
34 zzchar_t *zzbegexpr; /* beginning of last reg expr recogn. */
35 zzchar_t *zzendexpr; /* beginning of last reg expr recogn. */
36 int zzbufsize; /* number of characters in zzlextext */
37 int zzbegcol = 0; /* column that first character of token is in*/
38 int zzendcol = 0; /* column that last character of token is in */
39 int zzline = 1; /* line current token is on */
40 int zzreal_line=1; /* line of 1st portion of token that is not skipped */
41 int zzchar; /* character to determine next state */
42 int zzbufovf; /* indicates that buffer too small for text */
44 static zzchar_t *zznextpos;/* points to next available position in zzlextext*/
48 void zzerrstd(const char *);
49 void (*zzerr)(const char *)=zzerrstd;/* pointer to error reporting function */
50 extern int zzerr_in(void);
53 void (*zzerr)()=zzerrstd; /* pointer to error reporting function */
54 extern int zzerr_in();
57 static FILE *zzstream_in=0;
58 static int (*zzfunc_in)() = zzerr_in;
59 static zzchar_t *zzstr_in=0;
61 #ifdef USER_ZZMODE_STACK
64 static int zzauto = 0;
66 static int zzadd_erase;
67 static char zzebuf[70];
70 #define ZZINC (++zzendcol)
76 #define ZZGETC_STREAM {zzchar = getc(zzstream_in); zzclass = ZZSHIFT(zzchar);}
77 #define ZZGETC_FUNC {zzchar = (*zzfunc_in)(); zzclass = ZZSHIFT(zzchar);}
78 #define ZZGETC_STR { \
85 zzclass = ZZSHIFT(zzchar); \
88 #define ZZNEWSTATE (newstate = dfa[state][zzclass])
92 /* Truncate matching buffer to size (not an error) */ \
93 if (zznextpos < lastpos){ \
94 *(zznextpos++) = zzchar; \
102 zzrdstream( FILE *f )
108 /* make sure that it is really set to something, otherwise just
112 /* make sure that there is always someplace to get input
113 before closing zzstream_in
116 if (zzstream_in && zzstream_in!=stdin) fclose( zzstream_in );
128 zzrdfunc( int (*f)() )
134 /* make sure that it is really set to something, otherwise just
138 /* make sure that there is always someplace to get input
139 before closing zzstream_in
142 if (zzstream_in && zzstream_in!=stdin) fclose( zzstream_in );
155 zzrdstr( zzchar_t *s )
161 /* make sure that it is really set to something, otherwise just
165 /* make sure that there is always someplace to get input
166 before closing zzstream_in
169 if (zzstream_in && zzstream_in!=stdin) fclose( zzstream_in );
184 fclose( zzstream_in );
190 /* saves dlg state, but not what feeds dlg (such as file position) */
193 zzsave_dlg_state(struct zzdlg_state *state)
195 zzsave_dlg_state(state)
196 struct zzdlg_state *state;
199 state->stream = zzstream_in;
200 state->func_ptr = zzfunc_in;
201 state->str = zzstr_in;
202 state->auto_num = zzauto;
203 state->add_erase = zzadd_erase;
204 state->lookc = zzchar;
205 state->char_full = zzcharfull;
206 state->begcol = zzbegcol;
207 state->endcol = zzendcol;
208 state->line = zzline;
209 state->lextext = zzlextext;
210 state->begexpr = zzbegexpr;
211 state->endexpr = zzendexpr;
212 state->bufsize = zzbufsize;
213 state->bufovf = zzbufovf;
214 state->nextpos = zznextpos;
215 state->class_num = zzclass;
220 zzrestore_dlg_state(struct zzdlg_state *state)
222 zzrestore_dlg_state(state)
223 struct zzdlg_state *state;
226 zzstream_in = state->stream;
227 zzfunc_in = state->func_ptr;
228 zzstr_in = state->str;
229 zzauto = state->auto_num;
230 zzadd_erase = state->add_erase;
231 zzchar = state->lookc;
232 zzcharfull = state->char_full;
233 zzbegcol = state->begcol;
234 zzendcol = state->endcol;
235 zzline = state->line;
236 zzlextext = state->lextext;
237 zzbegexpr = state->begexpr;
238 zzendexpr = state->endexpr;
239 zzbufsize = state->bufsize;
240 zzbufovf = state->bufovf;
241 zznextpos = state->nextpos;
242 zzclass = state->class_num;
253 /* points to base of dfa table */
256 /* have to redo class since using different compression */
257 zzclass = ZZSHIFT(zzchar);
259 sprintf(zzebuf,"Invalid automaton mode = %d ",m);
264 /* erase what is currently in the buffer, and get a new reg. expr */
271 /* don't erase what is in the zzlextext buffer, add on to it */
278 /* substitute c for the reg. expr last matched and is in the buffer */
281 zzreplchar(zzchar_t c)
288 /* can't allow overwriting null at end of string */
289 if (zzbegexpr < &zzlextext[zzbufsize-1]){
291 *(zzbegexpr+1) = '\0';
293 zzendexpr = zzbegexpr;
294 zznextpos = zzbegexpr + 1;
297 /* replace the string s for the reg. expr last matched and in the buffer */
300 zzreplstr(register zzchar_t *s)
303 register zzchar_t *s;
306 register zzchar_t *l= &zzlextext[zzbufsize -1];
308 zznextpos = zzbegexpr;
310 while ((zznextpos <= l) && (*(zznextpos++) = *(s++))!=0){
313 /* correct for NULL at end of string */
316 if ((zznextpos <= l) && (*(--s) == 0)){
322 zzendexpr = zznextpos - 1;
328 register int state, newstate;
329 /* last space reserved for the null char */
330 register zzchar_t *lastpos;
333 zzreal_line = zzline;
335 lastpos = &zzlextext[zzbufsize-1];
336 zznextpos = zzlextext;
337 zzbegcol = zzendcol+1;
339 zzbegexpr = zznextpos;
341 /* interactive version of automaton */
342 /* if there is something in zzchar, process it */
343 state = newstate = dfa_base[zzauto];
350 while (zzalternatives[newstate]){
357 else if (zzstream_in)
358 while (zzalternatives[newstate]){
366 while (zzalternatives[newstate]){
373 /* figure out if last character really part of token */
374 if ((state != dfa_base[zzauto]) && (newstate == DfaStates)){
382 /* Able to transition out of start state to some non err state?*/
383 if ( state == dfa_base[zzauto] ){
384 /* make sure doesn't get stuck */
388 /* non-interactive version of automaton */
393 state = dfa_base[zzauto];
395 while (ZZNEWSTATE != DfaStates){
401 else if (zzstream_in)
402 while (ZZNEWSTATE != DfaStates){
409 while (ZZNEWSTATE != DfaStates){
416 if ( state == dfa_base[zzauto] ){
417 if (zznextpos < lastpos){
418 *(zznextpos++) = zzchar;
423 /* make sure doesn't get stuck */
430 zzendcol -= zzcharfull;
432 zzendexpr = zznextpos -1;
434 (*actions[accepts[state]])();
435 switch (zzadd_erase) {
444 if (zzstream_in) { ZZGETC_STREAM; zzcharfull = 1; ZZINC;}
445 if (zzfunc_in) { ZZGETC_FUNC; zzcharfull = 1; ZZINC;}
446 if (zzstr_in) { ZZGETC_STR; zzcharfull = 1; ZZINC;}
447 if (!(zzstream_in || zzfunc_in || zzstr_in)){
454 zzerrstd(const char *s)
461 "%s near line %d (text was '%s')\n",
462 ((s == NULL) ? "Lexical error" : s),
469 fprintf(stderr,"No input stream, function, or string\n");
470 /* return eof to get out gracefully */