1 /* This is the parser for the dlg
2 * This is a part of the Purdue Compiler Construction Tool Set
6 * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
7 * Set (PCCTS) -- PCCTS is in the public domain. An individual or
8 * company may do whatever they wish with source code distributed with
9 * PCCTS or the code generated by PCCTS, including the incorporation of
10 * PCCTS, or its output, into commerical software.
12 * We encourage users to develop software with PCCTS. However, we do ask
13 * that credit is given to us for developing PCCTS. By "credit",
14 * we mean that if you incorporate our source code into one of your
15 * programs (commercial product, research project, or otherwise) that you
16 * acknowledge this fact somewhere in the documentation, research report,
17 * etc... If you like PCCTS and have developed a nice tool with the
18 * output, please mention that you developed it using PCCTS. In
19 * addition, we ask that this header remain intact in our source code.
20 * As long as these guidelines are kept, we expect to continue enhancing
21 * this system and expect to make other tools available as they are
26 * With mods by Terence Parr; AHPCRC, University of Minnesota
38 Fix for Borland C++ 4.x & 5.x compiling with ALL warnings enabled
42 #pragma warn -aus /* unused assignment of 'xxx' */
45 #pragma clang diagnostic ignored "-Wparentheses-equality"
47 int action_no = 0; /* keep track of actions outputted */
48 int nfa_allocated = 0; /* keeps track of number of nfa nodes */
49 nfa_node **nfa_array = NULL;/* root of binary tree that stores nfa array */
50 nfa_node nfa_model_node; /* model to initialize new nodes */
51 set used_chars; /* used to label trans. arcs */
52 set used_classes; /* classes or chars used to label trans. arcs */
53 set normal_chars; /* mask to get rid elements that aren't used
55 int flag_paren = FALSE;
56 int flag_brace = FALSE;
57 int mode_counter = 0; /* keep track of number of %%names */
62 int func_action; /* should actions be turned into functions?*/
63 int lex_mode_counter = 0; /* keeps track of the number of %%names */
65 /* MR1 11-Apr-97 Provide mechanism for inserting code into DLG class */
66 /* MR1 via <<%%lexmember...>> */
68 int lexMember = 0; /* <<%%lexmemeber ...>> MR1 */
69 int lexAction = 0; /* <<%%lexaction ...>> MR1 */
70 int parserClass = 0; /* <<%%parserclass ...>> MR1 */
71 int lexPrefix = 0; /* <<%%lexprefix ...>> MR1 */
72 char theClassName[100]; /* MR11 */
73 char *pClassName=theClassName; /* MR11 */
74 int firstLexMember=1; /* MR1 */
77 void xxputc(int c) { /* MR1 */
79 void xxputc(c) /* MR1 */
83 if (parserClass) { /* MR1 */
84 *pClassName++=c; /* MR1 */
85 *pClassName=0; /* MR1 */
86 } else if (lexMember || lexPrefix) { /* MR1 */
87 if (class_stream != NULL) fputc(c,class_stream); /* MR1 */
89 fputc(c,OUT); /* MR1 */
94 void xxprintf(char *format,char *string) { /* MR1 */
96 void xxprintf(format,string) /* MR1 */
97 char *format; /* MR1 */
98 char *string; /* MR1 */
101 if (lexMember || lexPrefix || parserClass) { /* MR1 */
102 if (class_stream != NULL) /* MR1 */
103 fprintf(class_stream,format,string); /* MR1 */
105 fprintf(OUT,format,string); /* MR1 */
110 #token "[\r\t\ ]+" << zzskip(); >> /* Ignore white */
111 #token "\n" << zzline++; zzskip(); DAWDLE; >> /* Track Line # */
113 #token PER_PER "\%\%"
114 #token NAME_PER_PER "\%\%[a-zA-Z_][a-zA-Z0-9_]*"
115 << p_mode_def(&zzlextext[2],lex_mode_counter++); >>
117 #token LEXMEMBER "\<\<\%\%lexmember" /* MR1 */
118 <<lexMember=1; /* MR1 */
119 if (firstLexMember != 0) { /* MR1 */
120 firstLexMember=0; /* MR1 */
121 p_class_def1(); /* MR1 */
123 zzmode(ACT); /* MR1 */
125 #token LEXACTION "\<\<\%\%lexaction" /* MR1 */
126 <<lexAction=1;zzmode(ACT);>> /* MR1 */
127 #token PARSERCLASS "\<\<\%\%parserclass" /* MR1 */
128 <<parserClass=1; /* MR1 */
129 zzmode(ACT); /* MR1 */
131 #token LEXPREFIX "\<\<\%\%lexprefix" /* MR1 */
132 <<lexPrefix=1;zzmode(ACT);>> /* MR1 */
136 fprintf(OUT,"\n%s %sact%d()\n{ ",
137 gen_cpp?"ANTLRTokenType":"static void",
138 gen_cpp?ClassName("::"):"", ++action_no);
139 zzmode(ACT); zzskip();
141 #token GREAT_GREAT "\>\>"
148 #token ZERO_MORE "\*"
153 #token OCTAL_VALUE "\\0[0-7]*"
154 << {int t; sscanf(&zzlextext[1],"%o",&t); zzlextext[0] = t;}>>
155 #token HEX_VALUE "\\0[Xx][0-9a-fA-F]+"
156 << {int t; sscanf(&zzlextext[3],"%x",&t); zzlextext[0] = t;}>>
157 #token DEC_VALUE "\\[1-9][0-9]*"
158 << {int t; sscanf(&zzlextext[1],"%d",&t); zzlextext[0] = t;}>>
159 #token TAB "\\t" << zzlextext[0] = '\t';>>
160 #token NL "\\n" << zzlextext[0] = '\n';>>
161 #token CR "\\r" << zzlextext[0] = '\r';>>
162 #token BS "\\b" << zzlextext[0] = '\b';>>
165 /* MR1 10-Apr-97 MR1 Allow #token regular expressions to cross lines */
167 #token CONTINUATION "\\ \n" << zzline++; zzskip();>> /* MR1 */
169 /* NOTE: this takes ANYTHING after the \ */
170 #token LIT "\\~[tnrb]" << zzlextext[0] = zzlextext[1];>>
172 /* NOTE: this takes ANYTHING that doesn't match the other tokens */
173 #token REGCHAR "~[\\]"
176 grammar : << p_head(); p_class_hdr(); func_action = FALSE;>>
177 ( {LEXACTION | LEXMEMBER | LEXPREFIX | PARSERCLASS } ACTION)* /* MR1 */
178 <<if ( gen_cpp ) p_includes();>>
180 << func_action = FALSE; p_tables(); p_tail(); >>
182 << if (firstLexMember != 0) p_class_def1(); >> /* MR1 */
185 start_states : ( PER_PER do_conversion
186 | NAME_PER_PER do_conversion (NAME_PER_PER do_conversion)*)
190 do_conversion : <<new_automaton_mode(); func_action = TRUE;>>
193 dfa_class_nop[mode_counter] =
194 relabel($1.l,comp_level);
196 p_shift_table(mode_counter);
197 dfa_basep[mode_counter] = dfa_allocated+1;
198 make_dfa_model_node(dfa_class_nop[mode_counter]);
203 fprint_hash_stats(stderr);
208 rule_list : rule <<$$.l=$1.l; $$.r=$1.r;>>
214 /* all accept nodes "dead ends" */
220 <<$$.l = new_nfa_node(); $$.r = NULL;
221 warning("no regular expressions", zzline);
225 rule : reg_expr ACTION
226 /* MR23 */ << if ($1.r != NULL) {
227 $$.l=$1.l; $$.r=$1.r; ($1.r)->accept=action_no;
231 <<$$.l = NULL; $$.r = NULL;
232 error("no expression for action ", zzline);
236 reg_expr : and_expr <<$$.l=$1.l; $$.r=$1.r;>>
238 <<{nfa_node *t1, *t2;
239 t1 = new_nfa_node(); t2 = new_nfa_node();
242 /* MR23 */ if ($$.r != NULL) ($$.r)->trans[1]=t2;
244 ($2.r)->trans[1]=t2; /* MR20 */
252 and_expr : repeat_expr
254 $$.l=$1.l; $$.r=$1.r;
257 /* MR23 */ << if ($$.r != NULL) {
258 ($$.r)->trans[1]=$1.l;
265 repeat_expr : expr <<$$.l=$1.l; $$.r=$1.r;>>
267 <<{ nfa_node *t1,*t2;
268 /* MR23 */ if ($$.r != NULL) ($$.r)->trans[0] = $$.l;
269 t1 = new_nfa_node(); t2 = new_nfa_node();
272 /* MR23 */ if ($$.r != NULL) ($$.r)->trans[1]=t2;
277 /* MR23 */ <<if ($$.r != NULL) ($$.r)->trans[0] = $$.l;>>
280 << error("no expression for *", zzline);>>
282 << error("no expression for +", zzline);>>
285 expr : << $$.l = new_nfa_node();
286 $$.r = new_nfa_node();
288 L_BRACK atom_list R_BRACK
290 /* MR23 */ if ($$.l != NULL) {
291 ($$.l)->trans[0] = $$.r;
292 ($$.l)->label = set_dup($2.label);
293 set_orin(&used_chars,($$.l)->label);
296 | NOT L_BRACK atom_list R_BRACK
298 /* MR23 */ if ($$.l != NULL) {
299 ($$.l)->trans[0] = $$.r;
300 ($$.l)->label = set_dif(normal_chars,$3.label);
301 set_orin(&used_chars,($$.l)->label);
304 | L_PAR reg_expr R_PAR
306 /* MR23 */ if ($$.l != NULL) {
307 ($$.l)->trans[0] = $2.l;
309 ($2.r)->trans[1] = $$.r; /* MR20 */
313 | L_BRACE reg_expr R_BRACE
315 /* MR23 */ if ($$.l != NULL) {
316 ($$.l)->trans[0] = $2.l;
317 ($$.l)->trans[1] = $$.r;
319 ($2.r)->trans[1] = $$.r; /* MR20 */
325 /* MR23 */ if ($$.l != NULL) {
326 ($$.l)->trans[0] = $$.r;
327 ($$.l)->label = set_dup($1.label);
328 set_orin(&used_chars,($$.l)->label);
333 atom_list : << set_free($$.label); >>
334 (near_atom <<set_orin(&($$.label),$1.label);>>)*
337 near_atom : << register int i;
338 register int i_prime;
341 <<$$.letter=$1.letter; $$.label=set_of($1.letter);
342 i_prime = $1.letter + MIN_CHAR;
343 if (case_insensitive && islower(i_prime))
344 set_orel(toupper(i_prime)-MIN_CHAR,
346 if (case_insensitive && isupper(i_prime))
347 set_orel(tolower(i_prime)-MIN_CHAR,
351 << if (case_insensitive){
352 i_prime = $$.letter+MIN_CHAR;
353 $$.letter = (islower(i_prime) ?
354 toupper(i_prime) : i_prime)-MIN_CHAR;
355 i_prime = $2.letter+MIN_CHAR;
356 $2.letter = (islower(i_prime) ?
357 toupper(i_prime) : i_prime)-MIN_CHAR;
359 /* check to see if range okay */
361 int debugLetter1 = $$.letter;
362 int debugLetter2 = $2.letter;
364 if ($$.letter > $2.letter
365 && $2.letter != 0xff){ /* MR16 */
366 error("invalid range ", zzline);
368 for (i=$$.letter; i<= (int)$2.letter; ++i){
369 set_orel(i,&($$.label));
370 i_prime = i+MIN_CHAR;
371 if (case_insensitive && islower(i_prime))
372 set_orel(toupper(i_prime)-MIN_CHAR,
374 if (case_insensitive && isupper(i_prime))
375 set_orel(tolower(i_prime)-MIN_CHAR,
382 atom : << register int i_prime;>>
384 <<$$.label = set_of($1.letter);
385 i_prime = $1.letter + MIN_CHAR;
386 if (case_insensitive && islower(i_prime))
387 set_orel(toupper(i_prime)-MIN_CHAR,
389 if (case_insensitive && isupper(i_prime))
390 set_orel(tolower(i_prime)-MIN_CHAR,
395 anychar : REGCHAR <<$$.letter = $1.letter - MIN_CHAR;>>
396 | OCTAL_VALUE <<$$.letter = $1.letter - MIN_CHAR;>>
397 | HEX_VALUE <<$$.letter = $1.letter - MIN_CHAR;>>
398 | DEC_VALUE <<$$.letter = $1.letter - MIN_CHAR;>>
399 | TAB <<$$.letter = $1.letter - MIN_CHAR;>>
400 | NL <<$$.letter = $1.letter - MIN_CHAR;>>
401 | CR <<$$.letter = $1.letter - MIN_CHAR;>>
402 | BS <<$$.letter = $1.letter - MIN_CHAR;>>
403 | LIT <<$$.letter = $1.letter - MIN_CHAR;>>
404 /* NOTE: LEX_EOF is ALWAYS shifted to 0 = MIN_CHAR - MIN_CHAR*/
405 | L_EOF <<$$.letter = 0;>>
408 <</* empty action */>>
411 #token "@" << error("unterminated action", zzline); zzmode(START); >>
413 << if (func_action) fprintf(OUT,"}\n\n");
416 /* MR1 11-Apr-97 Provide mechanism for inserting code into DLG class */
417 /* MR1 via <<%%lexmember ...>> */
418 /* MR1 This is a consequence of not saving actions */
420 /* MR1 */ parserClass=0;
421 /* MR1 */ lexPrefix=0;
422 /* MR1 */ lexAction=0;
423 /* MR1 */ lexMember=0;
425 #token "\>" << xxputc(zzlextext[0]); zzskip(); >> /* MR1 */
426 #token "\\\>" << xxputc('>'); zzskip(); >> /* MR1 */
427 #token "\\" << xxputc('\\'); zzskip(); >> /* MR1 */
428 #token "\n" << xxputc(zzlextext[0]); ++zzline; zzskip(); >> /* MR1 */
429 #token "/\*" << zzmode(ACTION_COMMENTS); /* MR1 */
430 xxprintf("%s", &(zzlextext[0])); zzskip(); /* MR1 */
432 #token "//" << zzmode(ACTION_CPP_COMMENTS); /* MR1 */
433 xxprintf("%s", &(zzlextext[0])); zzskip(); /* MR1 */
435 #token "~[]" << xxputc(zzlextext[0]); zzskip(); >> /* MR1 */
437 #lexclass ACTION_COMMENTS /* MR1 */
438 #token "\*/" << zzmode(ACT); /* MR1 */
439 xxprintf("%s", &(zzlextext[0])); zzskip(); /* MR1 */
441 #token "[\n\r]" << zzline++; xxputc(zzlextext[0]); zzskip();>> /* MR1 */
442 #token "~[]" << xxputc(zzlextext[0]); zzskip();>> /* MR1 */
444 #lexclass ACTION_CPP_COMMENTS /* MR1 */
445 #token "[\n\r]" << zzmode(ACT); zzline++; /* MR1 */
446 xxprintf("%s", &(zzlextext[0])); zzskip(); /* MR1 */
448 #token "~[]" << xxputc(zzlextext[0]); zzskip();>> /* MR1 */
451 /* adds a new nfa to the binary tree and returns a pointer to it */
459 register nfa_node *t;
460 static int nfa_size=0; /* elements nfa_array[] can hold */
463 if (nfa_size<=nfa_allocated){
464 /* need to redo array */
466 /* need some to do inital allocation */
467 nfa_size=nfa_allocated+NFA_MIN;
468 nfa_array=(nfa_node **) malloc(sizeof(nfa_node*)*
471 /* need more space */
472 nfa_size=2*(nfa_allocated+1);
473 nfa_array=(nfa_node **) realloc(nfa_array,
474 sizeof(nfa_node*)*nfa_size);
477 /* fill out entry in array */
478 t = (nfa_node*) malloc(sizeof(nfa_node));
479 nfa_array[nfa_allocated] = t;
481 t->node_no = nfa_allocated;
486 /* initialize the model node used to fill in newly made nfa_nodes */
489 make_nfa_model_node(void)
491 make_nfa_model_node()
494 nfa_model_node.node_no = -1; /* impossible value for real nfa node */
495 nfa_model_node.nfa_set = 0;
496 nfa_model_node.accept = 0; /* error state default*/
497 nfa_model_node.trans[0] = NULL;
498 nfa_model_node.trans[1] = NULL;
499 nfa_model_node.label = empty;
504 #if defined(DEBUG) || defined(_DEBUG)
506 /* print out the pointer value and the node_number */
509 fprint_dfa_pair(FILE *f, nfa_node *p)
511 fprint_dfa_pair(f, p)
517 fprintf(f, "%x (%d)", p, p->node_no);
523 /* print out interest information on a set */
526 fprint_set(FILE *f, set s)
535 fprintf(f, "n = %d,", s.n);
537 fprintf(f, "setword = %x, ", s.setword);
538 /* print out all the elements in the set */
541 fprintf(f, "%d ", *x);
545 fprintf(f, "setword = (nil)");
549 /* code to be able to dump out the nfas
550 return 0 if okay dump
551 return 1 if screwed up
555 dump_nfas(int first_node, int last_node)
557 dump_nfas(first_node, last_node)
565 for (i=first_node; i<=last_node; ++i){
568 fprintf(stderr, "nfa_node %d {\n", t->node_no);
569 fprintf(stderr, "\n\tnfa_set = %d\n", t->nfa_set);
570 fprintf(stderr, "\taccept\t=\t%d\n", t->accept);
571 fprintf(stderr, "\ttrans\t=\t(");
572 fprint_dfa_pair(stderr, t->trans[0]);
573 fprintf(stderr, ",");
574 fprint_dfa_pair(stderr, t->trans[1]);
575 fprintf(stderr, ")\n");
576 fprintf(stderr, "\tlabel\t=\t{ ");
577 fprint_set(stderr, t->label);
578 fprintf(stderr, "\t}\n");
579 fprintf(stderr, "}\n\n");
587 /* DLG-specific syntax error message generator
588 * (define USER_ZZSYN when compiling so don't get 2 definitions)
592 zzsyn(char *text, int tok, char *egroup, SetWordType *eset, int etok, int k, char *bad_text)
594 zzsyn(text, tok, egroup, eset, etok, k, bad_text)
595 char *text, *egroup, *bad_text;
602 fprintf(stderr, ErrHdr, file_str[0]!=NULL?file_str[0]:"stdin", zzline);
603 fprintf(stderr, " syntax error at \"%s\"", (tok==zzEOF_TOKEN)?"EOF":text);
604 if ( !etok && !eset ) {fprintf(stderr, "\n"); return;}
605 if ( k==1 ) fprintf(stderr, " missing");
608 fprintf(stderr, "; \"%s\" not", bad_text);
609 if ( zzset_deg(eset)>1 ) fprintf(stderr, " in");
611 if ( zzset_deg(eset)>0 ) zzedecode(eset);
612 else fprintf(stderr, " %s", zztokens[etok]);
613 if ( strlen(egroup) > (size_t)0 ) fprintf(stderr, " in %s", egroup);
614 fprintf(stderr, "\n");