2 * antlr.g -- PCCTS Version 1.xx ANTLR
4 * Parse an antlr input grammar and build a syntax-diagram.
6 * Written in itself (needs at least 1.06 to work)
10 * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
11 * Set (PCCTS) -- PCCTS is in the public domain. An individual or
12 * company may do whatever they wish with source code distributed with
13 * PCCTS or the code generated by PCCTS, including the incorporation of
14 * PCCTS, or its output, into commerical software.
16 * We encourage users to develop software with PCCTS. However, we do ask
17 * that credit is given to us for developing PCCTS. By "credit",
18 * we mean that if you incorporate our source code into one of your
19 * programs (commercial product, research project, or otherwise) that you
20 * acknowledge this fact somewhere in the documentation, research report,
21 * etc... If you like PCCTS and have developed a nice tool with the
22 * output, please mention that you developed it using PCCTS. In
23 * addition, we ask that this header remain intact in our source code.
24 * As long as these guidelines are kept, we expect to continue enhancing
25 * this system and expect to make other tools available as they are
30 * Parr Research Corporation
31 * with Purdue University and AHPCRC, University of Minnesota
36 /* MR1 10-Apr-97 MR1 Replace #if logic with #include "pcctscfg.h" */
46 #define zzcr_attr(attr,tok,t)
51 /* MR20 G. Hobbelt For Borland C++ 4.x & 5.x compiling with ALL warnings enabled */
52 #if defined(__TURBOC__)
53 #pragma warn -aus /* unused assignment of 'xxx' */
56 #pragma clang diagnostic ignored "-Wparentheses-equality"
59 static void chkToken(char *, char *, char *, int);
61 static void chkToken();
65 static int isDLGmaxToken(char *Token); /* MR3 */
67 static int isDLGmaxToken(); /* MR3 */
70 static int class_nest_level = 0;
72 /* MR20 G. Hobbelt extern definitions moved to antlr.h */
77 /* maintained, but not used for now */
78 set AST_nodes_refd_in_actions = set_init;
80 set attribsRefdFromAction = set_init; /* MR20 */
81 int UsedOldStyleAttrib = 0;
82 int UsedNewStyleLabel = 0;
84 char *inline_set(char *);
89 /* MR1 10-Apr-97 MR1 Previously unable to put right shift operator */
90 /* MR1 in DLG action */
92 int tokenActionActive=0; /* MR1 */
97 #token QuotedTerm "\"" << zzmode(START); >>
98 #token "\n|\r|\r\n" <<
100 warn("eoln found in string");
103 #token "\\(\n|\r|\r\n)" << zzline++; zzmore(); >>
104 #token "\\~[]" << zzmore(); >>
105 #token "~[\n\r\"\\]+" << zzmore(); >>
107 #lexclass ACTION_STRINGS
108 #token "\"" << zzmode(ACTIONS); zzmore(); >>
109 #token "\n|\r|\r\n" <<
111 warn("eoln found in string (in user action)");
114 #token "\\(\n|\r|\r\n)" << zzline++; zzmore(); >>
115 #token "\\~[]" << zzmore(); >>
116 #token "~[\n\r\"\\]+" << zzmore(); >>
118 #lexclass ACTION_CHARS
119 #token "'" << zzmode(ACTIONS); zzmore(); >>
120 #token "\n|\r|\r\n" <<
122 warn("eoln found in char literal (in user action)");
125 #token "\\~[]" << zzmore(); >>
126 #token "~[\n\r'\\]+" << zzmore(); >>
128 #lexclass ACTION_COMMENTS
129 #token "\*/" << zzmode(ACTIONS); zzmore(); >>
130 #token "\*" << zzmore(); >>
131 #token "\n|\r|\r\n" << zzline++; zzmore(); DAWDLE; >>
132 #token "~[\n\r\*]+" << zzmore(); >>
134 #lexclass TOK_DEF_COMMENTS
135 #token "\*/" << zzmode(PARSE_ENUM_FILE);
137 #token "\*" << zzmore(); >>
138 #token "\n|\r|\r\n" << zzline++; zzmore(); DAWDLE; >>
139 #token "~[\n\r\*]+" << zzmore(); >>
141 #lexclass TOK_DEF_CPP_COMMENTS
142 #token "\n|\r|\r\n" << zzline++; zzmode(PARSE_ENUM_FILE); zzskip(); DAWDLE; >>
143 #token "~[\n\r]+" << zzskip(); >>
145 #lexclass ACTION_CPP_COMMENTS
146 #token "\n|\r|\r\n" << zzline++; zzmode(ACTIONS); zzmore(); DAWDLE; >>
147 #token "~[\n\r]+" << zzmore(); >>
149 #lexclass CPP_COMMENTS
150 #token "\n|\r|\r\n" << zzline++; zzmode(START); zzskip(); DAWDLE; >>
151 #token "~[\n\r]+" << zzskip(); >>
154 #token "\*/" << zzmode(START); zzskip(); >>
155 #token "\*" << zzskip(); >>
156 #token "\n|\r|\r\n" << zzline++; zzskip(); DAWDLE; >>
157 #token "~[\n\r\*]+" << zzskip(); >>
160 * This lexical class accepts actions of type [..] and <<..>>
162 * It translates the following special items for C:
164 * $j --> "zzaArg(current zztasp, j)"
165 * $i.j --> "zzaArg(zztaspi, j)"
166 * $i.nondigit> "zzaArg(current zztasp, i).nondigit"
168 * $alnum --> "alnum" (used to ref parameters)
170 * $retval --> "_retv.retval" if > 1 return values else "_retv"
171 * $[token, text] --> "zzconstr_attr(token, text)"
172 * $[] --> "zzempty_attr()"
174 * It translates the following special items for C++:
175 * (attributes are now stored with 'Token' and $i's are only
176 * pointers to the Tokens. Rules don't have attributes now.)
178 * $j --> "_tbj" where b is the block level
180 * $j->nondigit> "_tbj->nondigit"
182 * $alnum --> "alnum" (used to ref parameters)
184 * $retval --> "_retv.retval" if > 1 return values else "_retv"
185 * $[token, text] --> invalid
191 * #i --> "zzastArg(i)"
192 * #[args] --> "zzmk_ast(zzastnew(), args)"
193 * #[] --> "zzastnew()"
194 * #( root, child1, ..., childn )
195 * --> "zztmake(root, child1, ...., childn, NULL)"
201 * #i --> "_astbi" where b is the block level
202 * #alnum --> "alnum_ast" (used to ref #label)
203 * #[args] --> "new AST(args)"
205 * #( root, child1, ..., childn )
206 * --> "AST::tmake(root, child1, ...., childn, NULL)"
216 * A stack is used to nest action terminators because they can be nested
217 * like crazy: << #[$[..],..] >>
220 #token Action "\>\>" << /* these do not nest */
227 err( eMsgd("action buffer overflow; size %d",ZZLEXBUFSIZE));
230 /* MR1 10-Apr-97 MR1 Previously unable to put right shift operator */
231 /* MR1 in DLG action */
232 /* MR1 Doesn't matter what kind of action it is - reset*/
234 tokenActionActive=0; /* MR1 */
236 #token Pred "\>\>?" << /* these do not nest */
242 err( eMsgd("predicate buffer overflow; size %d",ZZLEXBUFSIZE));
245 /* MR10 */ list_apply(CurActionLabels, (void (*)(void *))mark_label_used_in_sem_pred);
248 /* MR10 */ list_apply(CurActionLabels, (void (*)(void *))mark_label_used_in_sem_pred);
251 /* MRxx */ list_apply(CurActionLabels, (void (*)(void *))mark_label_used_in_sem_pred);
253 /* MR10 */ list_apply(CurActionLabels,mark_label_used_in_sem_pred);
258 #token PassAction "\]" << if ( topint() == ']' ) {
260 if ( istackempty() ) /* terminate action */
266 err( eMsgd("parameter buffer overflow; size %d",ZZLEXBUFSIZE));
270 /* terminate $[..] and #[..] */
271 if ( GenCC ) zzreplstr("))");
276 else if ( topint() == '|' ) { /* end of simple [...] */
282 #token "consumeUntil\( [\ \t]* \{~[\}]+\} [\ \t]* \)"
285 zzreplstr(inline_set(zzbegexpr+
286 strlen("consumeUntil(")));
288 #token "consumeUntil\( ~[\)]+ \)"
290 #token "\n|\r|\r\n" << zzline++; zzmore(); DAWDLE; >>
291 #token "\>" << zzmore(); >>
292 #token "$" << zzmore(); >>
293 #token "$$" << if ( !GenCC ) {zzreplstr("zzaRet"); zzmore();}
294 else err("$$ use invalid in C++ mode"); >>
296 #token "$\[\]" << if ( !GenCC ) {zzreplstr("zzempty_attr"); zzmore();}
297 else err("$[] use invalid in C++ mode"); >>
300 if ( !GenCC ) zzreplstr("zzconstr_attr(");
301 else err("$[..] use invalid in C++ mode");
305 static char buf[100];
306 numericActionLabel=1; /* MR10 */
307 if ( strlen(zzbegexpr)>(size_t)85 )
308 fatal("$i attrib ref too big");
309 set_orel(atoi(zzbegexpr+1), &attribsRefdFromAction);
310 if ( !GenCC ) sprintf(buf,"zzaArg(zztasp%d,%s)",
311 BlkLevel-1,zzbegexpr+1);
312 else sprintf(buf,"_t%d%s",
313 BlkLevel-1,zzbegexpr+1);
316 UsedOldStyleAttrib = 1;
317 if ( UsedNewStyleLabel )
318 err("cannot mix old-style $i with new-style labels");
321 #token "$[0-9]+." <<{
322 static char buf[100];
323 numericActionLabel=1; /* MR10 */
324 if ( strlen(zzbegexpr)>(size_t)85 )
325 fatal("$i.field attrib ref too big");
326 zzbegexpr[strlen(zzbegexpr)-1] = ' ';
327 set_orel(atoi(zzbegexpr+1), &attribsRefdFromAction);
328 if ( !GenCC ) sprintf(buf,"zzaArg(zztasp%d,%s).",
329 BlkLevel-1,zzbegexpr+1);
330 else sprintf(buf,"_t%d%s.",
331 BlkLevel-1,zzbegexpr+1);
334 UsedOldStyleAttrib = 1;
335 if ( UsedNewStyleLabel )
336 err("cannot mix old-style $i with new-style labels");
339 #token "$[0-9]+.[0-9]+" <<{
340 static char buf[100];
341 static char i[20], j[20];
343 numericActionLabel=1; /* MR10 */
344 if (strlen(zzbegexpr)>(size_t)85) fatal("$i.j attrib ref too big");
345 for (p=zzbegexpr+1,q= &i[0]; *p!='.'; p++) {
347 fatalFL("i of $i.j attrib ref too big",
348 FileStr[CurFile], zzline );
352 for (p++, q= &j[0]; *p!='\0'; p++) {
354 fatalFL("j of $i.j attrib ref too big",
355 FileStr[CurFile], zzline );
359 if ( !GenCC ) sprintf(buf,"zzaArg(zztasp%s,%s)",i,j);
360 else sprintf(buf,"_t%s%s",i,j);
363 UsedOldStyleAttrib = 1;
364 if ( UsedNewStyleLabel )
365 err("cannot mix old-style $i with new-style labels");
368 #token "$[_a-zA-Z][_a-zA-Z0-9]*"
369 <<{ static char buf[300]; LabelEntry *el;
371 if ( CurRule != NULL &&
372 strcmp(CurRule, &zzbegexpr[1])==0 ) {
373 if ( !GenCC ) zzreplstr("zzaRet");
375 else if ( CurRetDef != NULL &&
376 strmember(CurRetDef, &zzbegexpr[1])) {
377 if ( hasMultipleOperands( CurRetDef ) ) {
378 require (strlen(zzbegexpr)<=(size_t)285,
379 "$retval attrib ref too big");
380 sprintf(buf,"_retv.%s",&zzbegexpr[1]);
383 else zzreplstr("_retv");
385 else if ( CurParmDef != NULL &&
386 strmember(CurParmDef, &zzbegexpr[1])) {
389 else if ( Elabel==NULL ) {
390 { err("$-variables in actions outside of rules are not allowed"); }
391 } else if ( (el=(LabelEntry *)hash_get(Elabel, &zzbegexpr[1]))!=NULL ) {
393 /* MR10 */ /* element labels might exist without an elem when */
394 /* MR10 */ /* it is a forward reference (to a rule) */
396 /* MR10 */ if ( GenCC && (el->elem == NULL || el->elem->ntype==nRuleRef) )
397 /* MR10 */ { err(eMsg1("There are no token ptrs for rule references: '$%s'",&zzbegexpr[1])); }
399 /* MR10 */ if ( !GenCC && (el->elem == NULL || el->elem->ntype==nRuleRef) && GenAST) {
400 /* MR10 */ err("You can no longer use attributes returned by rules when also using ASTs");
401 /* MR10 */ err(" Use upward inheritance (\"rule >[Attrib a] : ... <<$a=...\>\>\")");
404 /* MR10 */ /* keep track of <<... $label ...>> for semantic predicates in guess mode */
405 /* MR10 */ /* element labels contain pointer to the owners node */
407 /* MR10 */ if (el->elem != NULL && el->elem->ntype == nToken) {
408 /* MR10 */ list_add(&CurActionLabels,el);
412 warn(eMsg1("$%s not parameter, return value, (defined) element label",&zzbegexpr[1]));
416 #token "#0" << zzreplstr("(*_root)"); zzmore(); chkGTFlag(); >>
417 #token "#\[\]" << if ( GenCC ) {
418 if (NewAST) zzreplstr("(newAST)");
419 else zzreplstr("(new AST)");}
420 else {zzreplstr("zzastnew()");} zzmore();
423 #token "#\(\)" << zzreplstr("NULL"); zzmore(); chkGTFlag(); >>
425 static char buf[100];
426 if ( strlen(zzbegexpr)>(size_t)85 )
427 fatal("#i AST ref too big");
428 if ( GenCC ) sprintf(buf,"_ast%d%s",BlkLevel-1,zzbegexpr+1);
429 else sprintf(buf,"zzastArg(%s)",zzbegexpr+1);
432 set_orel(atoi(zzbegexpr+1), &AST_nodes_refd_in_actions);
437 /* MR14 Arpad Beszedes 26-May-98
438 Add support for #line directives when antlr source is pre-processed
442 #token "#line[\ \t]* [0-9]+ {[\ \t]* \"~[\"]+\" ([\ \t]* [0-9]*)* } (\n|\r|\r\n)"
444 zzline = atoi(zzbegexpr+5) - 1; zzline++; zzmore();
445 getFileNameFromTheLineInfo(FileStr[CurFile], zzbegexpr);
448 #token "#line ~[\n\r]* (\n|\r|\r\n)"
453 /* MR14 end of a block to support #line in antlr source code */
455 #token "#[_a-zA-Z][_a-zA-Z0-9]*"
457 if ( !(strcmp(zzbegexpr, "#ifdef")==0 ||
458 strcmp(zzbegexpr, "#if")==0 ||
459 strcmp(zzbegexpr, "#else")==0 ||
460 strcmp(zzbegexpr, "#endif")==0 ||
461 strcmp(zzbegexpr, "#ifndef")==0 ||
462 strcmp(zzbegexpr, "#define")==0 ||
463 strcmp(zzbegexpr, "#pragma")==0 ||
464 strcmp(zzbegexpr, "#undef")==0 ||
465 strcmp(zzbegexpr, "#import")==0 ||
466 strcmp(zzbegexpr, "#line")==0 ||
467 strcmp(zzbegexpr, "#include")==0 ||
468 strcmp(zzbegexpr, "#error")==0) )
470 static char buf[100];
471 sprintf(buf, "%s_ast", zzbegexpr+1);
472 /* MR27 */ list_add(&CurAstLabelsInActions, mystrdup(zzbegexpr+1));
481 if (NewAST) zzreplstr("(newAST(");
482 else zzreplstr("(new AST("); }
483 else zzreplstr("zzmk_ast(zzastnew(),");
494 zzreplstr("ASTBase::tmake(");
498 zzreplstr("zztmake(");
503 #token "#" << zzmore(); >>
507 else if ( topint()==')' ) {
510 else if ( topint()=='}' ) {
512 /* terminate #(..) */
513 zzreplstr(", NULL)");
518 pushint('|'); /* look for '|' to terminate simple [...] */
526 #token "\\\]" << zzreplstr("]"); zzmore(); >>
527 #token "\\\)" << zzreplstr(")"); zzmore(); >>
529 /* MR1 10-Apr-97 MR1 Previously unable to put right shift operator */
530 /* MR1 in DLG action */
532 #token "\\>" << if (! tokenActionActive) zzreplstr(">"); /* MR1 */
537 #token "'" << zzmode(ACTION_CHARS); zzmore();>>
538 #token "\"" << zzmode(ACTION_STRINGS); zzmore();>>
539 #token "\\$" << zzreplstr("$"); zzmore(); >>
540 #token "\\#" << zzreplstr("#"); zzmore(); >>
541 #token "\\(\n|\r|\r\n)" << zzline++; zzmore(); >>
542 #token "\\~[\]\)>$#]" << zzmore(); >> /* escaped char, always ignore */
543 #token "/" << zzmore(); >>
544 #token "/\*" << zzmode(ACTION_COMMENTS); zzmore(); >>
545 #token "\*/" << warn("Missing /*; found dangling */ in action"); zzmore(); >>
546 #token "//" << zzmode(ACTION_CPP_COMMENTS); zzmore(); >>
547 #token "~[\n\r\)\(\\$#\>\]\[\"'/]+" << zzmore(); >>
550 #token "[\t\ ]+" << zzskip(); >> /* Ignore White */
551 #token "\n|\r|\r\n" << zzline++; zzskip(); >> /* Track Line # */
552 #token "\[" << zzmode(ACTIONS); zzmore();
555 #token "\<\<" << action_file=CurFile; action_line=zzline;
556 zzmode(ACTIONS); zzmore();
557 list_free(&CurActionLabels,0); /* MR10 */
558 numericActionLabel=0; /* MR10 */
561 #token "\"" << zzmode(STRINGS); zzmore(); >>
562 #token "/\*" << zzmode(COMMENTS); zzskip(); >>
563 #token "\*/" << warn("Missing /*; found dangling */"); zzskip(); >>
564 #token "//" << zzmode(CPP_COMMENTS); zzskip(); >>
566 /* MR14 Arpad Beszedes 26-May-98
567 Add support for #line directives when antlr source is pre-processed
571 #token "#line[\ \t]* [0-9]+ {[\ \t]* \"~[\"]+\" ([\ \t]* [0-9]*)* } (\n|\r|\r\n)"
573 zzline = atoi(zzbegexpr+5) - 1; zzline++; zzmore();
574 getFileNameFromTheLineInfo(FileStr[CurFile], zzbegexpr);
577 #token "#line ~[\n\r]* (\n|\r|\r\n)"
582 /* MR14 end of a block to support #line in antlr source code */
585 /* 8-Apr-97 Regularize escape sequence for ">>" */
586 /* appearing in string literals */
589 #token "\>\>" << warn("Missing <<; found dangling \>\>"); zzskip(); >> /* MR1 */
591 #token "\@" <<FoundException = 1; /* MR6 */
592 FoundAtOperator = 1;>> /* MR6 */
594 << /* L o o k F o r A n o t h e r F i l e */
597 new_input = NextFile();
598 if ( new_input == NULL ) { NLA=Eof; return; }
602 zzskip(); /* Skip the Eof (@) char i.e continue */
608 #errclass "grammar-element" { element }
609 #errclass "meta-symbol" { "\}" "!" ";" "\|" "\~" "^" "\)" }
611 #token Pragma "{\\}#pragma" /* MR21 */
612 #token FirstSetSymbol "{\\}#FirstSetSymbol" /* MR21 */
614 * Get a grammar -- Build a list of rules like:
627 grammar : <<Graph g;>>
628 ( "{\\}#header" Action /* MR13 */
630 if ( HdrAction==NULL ) {
631 HdrAction = (char *) calloc(strlen(LATEXT(1))+1, sizeof(char));
632 require(HdrAction!=NULL, "rule grammar: cannot allocate header action");
633 strcpy(HdrAction, LATEXT(1));
635 else warn("additional #header statement ignored");
637 | "{\\}#first" Action
639 if ( FirstAction==NULL ) {
640 FirstAction = (char *) calloc(strlen(LATEXT(1))+1, sizeof(char));
641 require(FirstAction!=NULL, "rule grammar: cannot allocate #first action");
642 strcpy(FirstAction, LATEXT(1));
644 warn("additional #first statement ignored");
648 | "{\\}#parser" QuotedTerm
651 warn("#parser meta-op incompatible with -CC; ignored");
654 if ( strcmp(ParserName,"zzparser")==0 ) {
655 ParserName=StripQuotes(mystrdup(LATEXT(1)));
656 if ( RulePrefix[0]!='\0' )
658 warn("#parser meta-op incompatible with '-gp prefix'; '-gp' ignored");
662 else warn("additional #parser statement ignored");
665 | "{\\}#tokdefs" QuotedTerm
668 zzantlr_state st; FILE *f; struct zzdlg_state dst;
669 UserTokenDefsFile = mystrdup(LATEXT(1));
670 zzsave_antlr_state(&st);
671 zzsave_dlg_state(&dst);
672 fname = mystrdup(LATEXT(1));
673 f = fopen(StripQuotes(fname), "r");
674 if ( f==NULL ) {warn(eMsg1("cannot open token defs file '%s'", fname+1));}
676 ANTLRm(enum_file(fname+1), f, PARSE_ENUM_FILE);
679 zzrestore_antlr_state(&st);
680 zzrestore_dlg_state(&dst);
685 UserAction *ua = newUserAction(LATEXT(1));
686 ua->file = action_file; ua->line = action_line;
687 if ( class_nest_level>0 ) list_add(&class_before_actions, ua);
688 else list_add(&BeforeActions, ua);
698 | default_exception_handler
702 if ( class_nest_level==0 )
703 warn("missing class definition for trailing '}'");
708 rule <<g=$3; SynDiag = (Junction *) $3.left;>>
711 <<if ( $1.left!=NULL ) {
714 /* MR21a */ /* Avoid use of a malformed graph when CannotContinue */
715 /* MR21a */ /* is already set */
717 /* MR21a */ if (! (CannotContinue && g.left == NULL)) {
718 /* MR21a */ g = Or(g, $1);
731 if ( class_nest_level==0 )
732 warn("missing class definition for trailing '}'");
738 UserAction *ua = newUserAction(LATEXT(1));
739 ua->file = action_file; ua->line = action_line;
740 if ( class_nest_level>0 ) list_add(&class_after_actions, ua);
741 else list_add(&AfterActions, ua);
752 if ( class_nest_level==0 )
753 warn("missing class definition for trailing '}'");
759 <<CannotContinue=TRUE;>>
764 : <<int go=1; char name[MaxRuleName+1];>>
766 ( NonTerminal <<if(go) strncpy(name,LATEXT(1),MaxRuleName);>>
767 | TokenTerm <<if(go) strncpy(name,LATEXT(1),MaxRuleName);>>
770 if ( CurrentClassName[0]!='\0' && strcmp(CurrentClassName,name)!=0
772 err("only one grammar class allowed in this release");
775 else strcpy(CurrentClassName, name);
777 <<if ( !GenCC ) { err("class meta-op used without C++ option"); }>>
780 /* MR10 */ <<if (ClassDeclStuff == NULL) {
781 /* MR10 */ ClassDeclStuff=(char *)calloc(MaxClassDeclStuff+1,sizeof(char));
783 /* MR10 */ strncat(ClassDeclStuff," ",MaxClassDeclStuff);
784 /* MR10 */ strncat(ClassDeclStuff,LATEXT(1),MaxClassDeclStuff);
786 /* MR22 */ if (0 == strcmp(LATEXT(1),"public")) break;
787 /* MR22 */ if (0 == strcmp(LATEXT(1),"private")) break;
788 /* MR22 */ if (0 == strcmp(LATEXT(1),"protected")) break;
789 /* MR22 */ if (0 == strcmp(LATEXT(1),"virtual")) break;
790 /* MR22 */ if (0 == strcmp(LATEXT(1),",")) break;
791 /* MR22 */ if (0 == strcmp(LATEXT(1),":")) break;
792 /* MR22 */ if (BaseClassName != NULL) break;
793 /* MR22 */ BaseClassName=(char *)calloc(strlen(LATEXT(1))+1,sizeof(char));
794 /* MR22 */ require(BaseClassName!=NULL, "rule grammar: cannot allocate base class name");
795 /* MR22 */ strcpy(BaseClassName,LATEXT(1));
796 /* MR22 */ } while (0);
802 no_classes_found = 0;
803 if ( class_nest_level>=1 ) {warn("cannot have nested classes");}
804 else class_nest_level++;
807 <<CannotContinue=TRUE;>>
810 * Build -o-->o-R-o-->o- where -o-R-o- is the block from rule 'block'.
811 * Construct the RuleBlk front and EndRule node on the end of the
812 * block. This is used to add FOLLOW pointers to the rule end. Add the
813 * new rule name to the Rname hash table and sets its rulenum.
814 * Store the parameter definitions if any are found.
816 * Note that locks are required on the RuleBlk and EndRule nodes to thwart
817 * infinite recursion.
819 * Return the left graph pointer == NULL to indicate error/dupl rule def.
827 RuleEntry *q; Junction *p; Graph r; int f, l; ECnode *e;
828 set toksrefd, rulesrefd;
829 char *pdecl=NULL, *ret=NULL, *a; CurRetDef = CurParmDef = NULL;
831 CurElementLabels = NULL;
832 CurAstLabelsInActions = NULL; /* MR27 */
833 /* We want a new element label hash table for each rule */
834 if ( Elabel!=NULL ) killHashTable(Elabel);
835 Elabel = newHashTable();
836 attribsRefdFromAction = empty;
840 if ( hash_get(Rname, LATEXT(1))!=NULL ) {
841 err(eMsg1("duplicate rule definition: '%s'",LATEXT(1)));
846 q = (RuleEntry *)hash_add(Rname,
848 (Entry *)newRuleEntry(LATEXT(1)));
852 f = CurFile; l = zzline;
855 { "!" <<if ( q!=NULL ) q->noAST = TRUE;>> }
859 << pdecl = (char *) calloc(strlen(LATEXT(1))+1, sizeof(char));
860 require(pdecl!=NULL, "rule rule: cannot allocate param decl");
861 strcpy(pdecl, LATEXT(1));
867 << ret = (char *) calloc(strlen(LATEXT(1))+1, sizeof(char));
868 require(ret!=NULL, "rule rule: cannot allocate ret type");
869 strcpy(ret, LATEXT(1));
873 { QuotedTerm <<if ( q!=NULL ) q->egroup=mystrdup(LATEXT(1));>> }
875 if ( GenEClasseForRules && q!=NULL ) {
877 require(e!=NULL, "cannot allocate error class node");
878 if ( q->egroup == NULL ) {a = q->str; a[0] = (char)toupper(a[0]);}
880 if ( Tnum( a ) == 0 )
882 e->tok = addTname( a );
883 list_add(&eclasses, (char *)e);
884 if ( q->egroup == NULL ) a[0] = (char)tolower(a[0]);
885 /* refers to itself */
886 list_add(&(e->elist), mystrdup(q->str));
889 warn(eMsg1("default errclass for '%s' would conflict with token/errclass/tokclass",a));
890 if ( q->egroup == NULL ) a[0] = (char)tolower(a[0]);
896 if (BlkLevel >= MAX_BLK_LEVEL) fatal("Blocks nested too deeply");
897 /* MR23 */ CurBlockID_array[BlkLevel] = CurBlockID;
898 /* MR23 */ CurAltNum_array[BlkLevel] = CurAltNum;
902 block[&toksrefd, &rulesrefd]
903 <<r = makeBlk($7,0, NULL /* pFirstSetSymbol */ );
904 CurRuleBlk = (Junction *)r.left;
905 CurRuleBlk->blockid = CurBlockID;
906 CurRuleBlk->jtype = RuleBlk;
907 if ( q!=NULL ) CurRuleBlk->rname = q->str;
908 CurRuleBlk->file = f;
909 CurRuleBlk->line = l;
910 CurRuleBlk->pdecl = pdecl;
911 CurRuleBlk->ret = ret;
912 CurRuleBlk->lock = makelocks();
913 CurRuleBlk->pred_lock = makelocks();
914 CurRuleBlk->tokrefs = toksrefd;
915 CurRuleBlk->rulerefs = rulesrefd;
916 p = newJunction(); /* add EndRule Node */
917 ((Junction *)r.right)->p1 = (Node *)p;
918 r.right = (Node *) p;
920 p->lock = makelocks();
921 p->pred_lock = makelocks();
923 if ( q!=NULL ) q->rulenum = NumRules;
927 /* MR23 */ CurBlockID_array[BlkLevel] = (-1);
928 /* MR23 */ CurAltNum_array[BlkLevel] = (-1);
931 <<altFixup();leFixup();egFixup();>> /* MR7 */
934 << a = (char *) calloc(strlen(LATEXT(1))+1, sizeof(char));
935 require(a!=NULL, "rule rule: cannot allocate error action");
936 strcpy(a, LATEXT(1));
937 CurRuleBlk->erraction = a;
940 ( exception_group > [eg]
942 list_add(&CurExGroups, (void *)eg);
943 if (eg->label == NULL || *eg->label=='\0' ) q->has_rule_exception = 1;
947 <<if ( q==NULL ) $0.left = NULL; else $0 = $7;>>
948 <<CurRuleBlk->exceptions = CurExGroups;>>
949 <<CurRuleBlk->el_labels = CurElementLabels;>>
950 <<CurRuleNode->ast_labels_in_actions = CurAstLabelsInActions;>> /* MR27 */
951 <<CurRuleNode = NULL;>> /* MR27 Moved */
953 <<CannotContinue=TRUE;>>
956 * pragma : "{\\}#pragma" "dup\-labeled\-tokens"
957 * <<Pragma_DupLabeledTokens=1;>>
963 laction : <<char *a;>>
968 a = (char *) calloc(strlen(LATEXT(1))+1, sizeof(char));
969 require(a!=NULL, "rule laction: cannot allocate action");
970 strcpy(a, LATEXT(1));
971 list_add(&LexActions, a);
974 <<CannotContinue=TRUE;>>
977 /* MR1 11-Apr-97 Provide mechanism for inserting code into DLG class */
978 /* MR1 via #lexmember <<....>> & #lexprefix <<...>> */
983 lmember: <<char *a;>> /* MR1 */
985 /* MR1 */ "{\\}#lexmember"
988 /* MR1 */ if (! GenCC) {
989 /* MR1 */ err("Use #lexmember only in C++ mode (to insert code in DLG class header");
991 /* MR1 */ a = (char *) calloc(strlen(LATEXT(1))+1, sizeof(char));
992 /* MR1 */ require(a!=NULL, "rule lmember: cannot allocate action");
993 /* MR1 */ strcpy(a, LATEXT(1));
994 /* MR1 */ list_add(&LexMemberActions, a);
998 /* MR1 */ <<CannotContinue=TRUE;>>
1002 lprefix: <<char *a;>> /* MR1 */
1004 /* MR1 */ "{\\}#lexprefix"
1007 /* MR1 */ if (! GenCC) {
1008 /* MR1 */ err("Use #lexprefix only in C++ mode (to insert code in DLG class header");
1010 /* MR1 */ a = (char *) calloc(strlen(LATEXT(1))+1, sizeof(char));
1011 /* MR1 */ require(a!=NULL, "rule lprefix: cannot allocate action");
1012 /* MR1 */ strcpy(a, LATEXT(1));
1013 /* MR1 */ list_add(&LexPrefixActions, a);
1017 /* MR1 */ <<CannotContinue=TRUE;>>
1020 * #pred upper <<isupper()>>? predicate literal
1021 * #pred lower <<islower()>>? predicate literal
1022 * #pred up_or_low upper || lower predicate expression
1023 * concealed interdependence
1024 * #pred up_or_low_2 <<isletter()>>? A || B predicate literal equals predicate expr
1025 * analyze using lower||upper
1026 * generate using isLetter()
1031 aPred: <<PredEntry *predEntry=NULL;
1033 Predicate *predExpr=NULL;
1034 char *predLiteral=NULL;
1037 int predExprPresent=0;
1043 MR_usingPredNames=1; /* will need to use -mrhoist version of genPredTree */
1046 /* used to allow NonTerminal but it caused problems
1047 when a rule name immediately followed a #pred statement */
1049 TokenTerm <<name=mystrdup(LATEXT(1));>>
1052 /* don't free - referenced in predicates */
1054 CurPredName=(char *)calloc(1,strlen(name) + 10);
1055 strcat(CurPredName,"#pred ");
1056 strcat(CurPredName,name);
1058 predEntry=(PredEntry *) hash_get(Pname,name);
1059 if (predEntry != NULL) {
1060 warnFL(eMsg1("#pred %s previously defined - ignored",name),
1061 FileStr[action_file],action_line);
1068 Pred <<predLiteral=mystrdup(LATEXT(1));
1069 save_line=action_line;
1070 save_file=action_file;
1074 predOrExpr>[predExpr] <<predExprPresent=1;>>
1077 <<if (predLiteral != NULL && name != NULL) {
1080 * predExpr may be NULL due to syntax errors
1081 * or simply omitted by the user
1084 predEntry=newPredEntry(name);
1085 predEntry->file=save_file;
1086 predEntry->line=save_line;
1087 predExpr=MR_predFlatten(predExpr);
1088 predEntry->predLiteral=predLiteral;
1089 if (! predExprPresent || predExpr == NULL) {
1090 predExpr=new_pred();
1091 predExpr->expr=predLiteral;
1092 predExpr->source=newActionNode();
1093 predExpr->source->action=predExpr->expr;
1094 predExpr->source->rname=CurPredName;
1095 predExpr->source->line=action_line;
1096 predExpr->source->file=action_file;
1097 predExpr->source->is_predicate=1;
1098 predExpr->k=predicateLookaheadDepth(predExpr->source);
1100 predEntry->pred=predExpr;
1101 hash_add(Pname,name,(Entry *)predEntry);
1104 predicate_free(predExpr);
1108 <<save_line=zzline; save_file=CurFile;>>
1110 predOrExpr>[predExpr]
1112 <<if (predExpr != NULL && name != NULL) {
1113 predEntry=newPredEntry(name);
1114 predEntry->file=CurFile;
1115 predEntry->line=zzline;
1116 predExpr=MR_predFlatten(predExpr);
1117 predEntry->pred=predExpr;
1118 hash_add(Pname,name,(Entry *)predEntry);
1121 predicate_free(predExpr);
1129 <<predicate_free(predExpr);
1132 /* rule predOrExpr */
1134 predOrExpr>[Predicate *result] :
1135 <<Predicate *ORnode;
1136 Predicate *predExpr;
1137 Predicate **tail=NULL;
1139 predAndExpr>[predExpr]
1142 ORnode->expr=PRED_OR_LIST;
1143 if (predExpr != NULL) {
1144 ORnode->down=predExpr;
1145 tail=&predExpr->right;
1148 ( "\|\|" predAndExpr>[predExpr]
1150 if (predExpr != NULL) {
1152 tail=&predExpr->right;
1164 <<predicate_free(ORnode);>>
1166 /* rule predAndExpr */
1168 predAndExpr>[Predicate *result] :
1169 <<Predicate *ANDnode;
1170 Predicate *predExpr;
1171 Predicate **tail=NULL;
1173 predPrimary>[predExpr]
1176 ANDnode->expr=PRED_AND_LIST;
1177 if (predExpr != NULL) {
1178 ANDnode->down=predExpr;
1179 tail=&predExpr->right;
1182 ( "&&" predPrimary>[predExpr]
1184 if (predExpr != NULL) {
1186 tail=&predExpr->right;
1198 <<predicate_free(ANDnode);>>
1201 /* rule predPrimary */
1203 predPrimary>[Predicate *result] :
1206 PredEntry *predEntry=NULL;
1207 Predicate *predExpr=NULL;
1210 TokenTerm <<name=mystrdup(LATEXT(1));>>
1213 predEntry=(PredEntry *) hash_get(Pname,name);
1214 if (predEntry == NULL) {
1215 warnFL(eMsg1("no previously defined #pred with name \"%s\"",name),
1216 FileStr[CurFile],zzline);
1220 predExpr=predicate_dup(predEntry->pred);
1221 predExpr->predEntry=predEntry;
1226 | "\(" predOrExpr>[predExpr] "\)"
1231 | "!" predPrimary>[predExpr]
1233 predExpr->inverted=!predExpr->inverted;
1239 predicate_free(predExpr);
1242 /* rule aLexclass */
1244 aLexclass: "{\\}#lexclass" TokenTerm <<lexclass(mystrdup(LATEXT(1)));>>
1246 <<CannotContinue=TRUE;>>
1250 error : <<char *t=NULL; ECnode *e; int go=1; TermEntry *p;>>
1252 (<<;>> TokenTerm <<t=mystrdup(LATEXT(1));>>
1253 | QuotedTerm <<t=mystrdup(LATEXT(1));>>
1256 require(e!=NULL, "cannot allocate error class node");
1257 e->lexclass = CurrentLexClass;
1258 if ( Tnum( (t=StripQuotes(t)) ) == 0 )
1260 if ( hash_get(Texpr, t) != NULL )
1261 warn(eMsg1("errclass name conflicts with regular expression '%s'",t));
1262 e->tok = addTname( t );
1263 set_orel(e->tok, &imag_tokens);
1264 require((p=(TermEntry *)hash_get(Tname, t)) != NULL,
1265 "hash table mechanism is broken");
1266 p->classname = 1; /* entry is errclass name, not token */
1267 list_add(&eclasses, (char *)e);
1271 warn(eMsg1("redefinition of errclass or conflict w/token or tokclass '%s'; ignored",t));
1277 ( NonTerminal <<if ( go ) t=mystrdup(LATEXT(1));>>
1278 | TokenTerm <<if ( go ) t=mystrdup(LATEXT(1));>>
1279 | QuotedTerm <<if ( go ) t=mystrdup(LATEXT(1));>>
1281 <<if ( go ) list_add(&(e->elist), t);>>
1283 ( NonTerminal <<if ( go ) t=mystrdup(LATEXT(1));>>
1284 | TokenTerm <<if ( go ) t=mystrdup(LATEXT(1));>>
1285 | QuotedTerm <<if ( go ) t=mystrdup(LATEXT(1));>>
1287 <<if ( go ) list_add(&(e->elist), t);>>
1291 <<CannotContinue=TRUE;>>
1295 tclass : <<char *t=NULL; TCnode *e; int go=1,tok,totok; TermEntry *p, *term, *toterm;>>
1296 <<char *akaString=NULL; int save_file; int save_line;>>
1297 <<char *totext=NULL; >>
1298 "{\\}#tokclass" TokenTerm <<t=mystrdup(LATEXT(1));>>
1300 require(e!=NULL, "cannot allocate token class node");
1301 e->lexclass = CurrentLexClass;
1302 if ( Tnum( t ) == 0 )
1304 e->tok = addTname( t );
1305 set_orel(e->tok, &imag_tokens);
1306 set_orel(e->tok, &tokclasses);
1307 require((p=(TermEntry *)hash_get(Tname, t)) != NULL,
1308 "hash table mechanism is broken");
1309 p->classname = 1; /* entry is class name, not token */
1310 p->tclass = e; /* save ptr to this tclass def */
1311 list_add(&tclasses, (char *)e);
1315 warn(eMsg1("redefinition of tokclass or conflict w/token '%s'; ignored",t));
1322 /* MR23 */ QuotedTerm
1323 /* MR23 */ <<akaString=mystrdup(StripQuotes(LATEXT(1)));
1324 /* MR11 */ save_file=CurFile;save_line=zzline;
1331 /* MR23 */ if (p!= NULL && akaString != NULL) {
1332 /* MR23 */ if (p->akaString != NULL) {
1333 /* MR23 */ if (strcmp(p->akaString,akaString) != 0) {
1334 /* MR23 */ warnFL(eMsg2("this #tokclass statement conflicts with a previous #tokclass %s(\"%s\") statement",
1335 /* MR23 */ t,p->akaString),
1336 /* MR23 */ FileStr[save_file],save_line);
1339 /* MR23 */ p->akaString=akaString;
1348 term = (TermEntry *) hash_get(Tname, LATEXT(1));
1349 if ( term==NULL && UserDefdTokens ) {
1350 err("implicit token definition not allowed with #tokdefs");
1353 else {t=mystrdup(LATEXT(1)); tok=addTname(LATEXT(1));}
1361 toterm = (TermEntry *) hash_get(Tname, LATEXT(1));
1362 if ( toterm==NULL && UserDefdTokens ) {
1363 err("implicit token definition not allowed with #tokdefs");
1366 totext=mystrdup(LATEXT(1)); totok=addTname(LATEXT(1));
1374 term = (TermEntry *) hash_get(Texpr, LATEXT(1));
1375 if ( term==NULL && UserDefdTokens ) {
1376 err("implicit token definition not allowed with #tokdefs");
1379 else {t=mystrdup(LATEXT(1)); tok=addTexpr(LATEXT(1));}
1383 if (totext == NULL) {
1384 list_add(&(e->tlist), t);
1386 list_add(&(e->tlist),"..");
1387 list_add(&(e->tlist),t);
1388 list_add(&(e->tlist),totext);
1393 )+ // MR15 Manfred Kogler - forbid empty #tokclass sets (was "+")
1396 <<CannotContinue=TRUE;>>
1400 token : <<char *t=NULL, *e=NULL, *a=NULL; int tnum=0;>>
1401 <<char *akaString=NULL; TermEntry *te;int save_file=0,save_line=0;>> /* MR11 */
1404 /* MR1 10-Apr-97 MR1 Allow shift right operator in DLG actions */
1405 /* MR1 Danger when parser feedback to lexer */
1408 <<tokenActionActive=1;>> /* MR1 */
1409 { TokenTerm <<t=mystrdup(LATEXT(1));>>
1413 /* MR11 */ QuotedTerm
1414 /* MR11 */ <<akaString=mystrdup(StripQuotes(LATEXT(1)));
1415 /* MR11 */ save_file=CurFile;save_line=zzline;
1420 { "=" "[0-9]+" /* define the token type number */
1421 <<tnum = atoi(LATEXT(1));>>
1424 { QuotedTerm <<e=mystrdup(LATEXT(1));>> }
1427 a = (char *) calloc(strlen(LATEXT(1))+1, sizeof(char));
1428 require(a!=NULL, "rule token: cannot allocate action");
1429 strcpy(a, LATEXT(1));
1435 <<chkToken(t, e, a, tnum);>>
1438 te=(TermEntry *)hash_get(Tname,t);
1439 if (te != NULL && akaString != NULL) {
1440 if (te->akaString != NULL) {
1441 if (strcmp(te->akaString,akaString) != 0) {
1442 warnFL(eMsg2("this #token statement conflicts with a previous #token %s(\"%s\") statement",
1444 FileStr[save_file],save_line);
1447 te->akaString=akaString;
1453 <<CannotContinue=TRUE;>>
1457 block[set *toksrefd, set *rulesrefd]
1461 int saveinalt = inAlt;
1464 *$rulesrefd = empty;
1465 set_clr(AST_nodes_refd_in_actions);
1467 /* MR23 */ CurBlockID_array[BlkLevel] = CurBlockID;
1469 /* MR23 */ CurAltNum_array[BlkLevel] = CurAltNum;
1470 saveblah = attribsRefdFromAction;
1471 attribsRefdFromAction = empty;
1474 alt[toksrefd,rulesrefd] <<b = g = $1;>>
1477 if ( ((Junction *)g.left)->p1->ntype == nAction )
1479 ActionNode *actionNode=(ActionNode *)
1480 ( ( (Junction *)g.left) ->p1);
1481 if (!actionNode->is_predicate )
1483 actionNode->init_action = TRUE;
1484 /* MR12c */ if (actionNode->noHoist) {
1485 /* MR12c */ errFL("<<nohoist>> appears as init-action - use <<>> <<nohoist>>",
1486 /* MR12c */ FileStr[actionNode->file],actionNode->line);
1490 ((Junction *)g.left)->blockid = CurBlockID;
1493 ( exception_group > [eg]
1496 /* MR7 ***** eg->altID = makeAltID(CurBlockID,CurAltNum); *****/
1497 /* MR7 ***** CurAltStart->exception_label = eg->altID; *****/
1498 list_add(&CurExGroups, (void *)eg);
1503 /* MR23 */ CurAltNum_array[BlkLevel] = CurAltNum;
1507 alt[toksrefd,rulesrefd] <<g = Or(g, $2);>>
1509 ((Junction *)g.left)->blockid = CurBlockID;
1512 ( exception_group > [eg]
1515 /* MR7 ***** eg->altID = makeAltID(CurBlockID,CurAltNum); *****/
1516 /* MR7 ***** CurAltStart->exception_label = eg->altID; *****/
1517 list_add(&CurExGroups, (void *)eg);
1523 /* MR23 */ CurAltNum_array[BlkLevel] = CurAltNum;
1528 <<attribsRefdFromAction = saveblah; inAlt = saveinalt;>>
1530 <<CannotContinue=TRUE;>>
1534 alt[set *toksrefd, set *rulesrefd]
1535 : <<int n=0; Graph g; int e_num=0, old_not=0; Node *node; set elems, dif;
1536 int first_on_line = 1, use_def_MT_handler = 0;
1537 g.left=NULL; g.right=NULL;
1543 { "\@" /* handle MismatchedToken signals with default handler */
1544 <<use_def_MT_handler = 1;>>
1547 ( <<;>> /* MR9 Removed unreferenced variable "tok" */
1548 { <<old_not=0;>> "\~" <<old_not=1;>> }
1549 element[old_not, first_on_line, use_def_MT_handler] > [node]
1550 <<if ( node!=NULL && node->ntype!=nAction ) first_on_line = 0;>>
1552 if ( $2.left!=NULL ) {
1556 if ( node->ntype!=nAction ) e_num++;
1557 /* record record number of all rule and token refs */
1558 if ( node->ntype==nToken ) {
1559 TokNode *tk = (TokNode *)((Junction *)$2.left)->p1;
1561 set_orel(e_num, &elems);
1563 else if ( node->ntype==nRuleRef ) {
1564 RuleRefNode *rn = (RuleRefNode *)((Junction *)$2.left)->p1;
1566 set_orel(e_num, $rulesrefd);
1572 <<if ( n == 0 ) g = emptyAlt();
1574 /* We want to reduce number of LT(i) calls and the number of
1575 * local attribute variables in C++ mode (for moment, later we'll
1576 * do for C also). However, if trees are being built, they
1577 * require most of the attrib variables to create the tree nodes
1578 * with; therefore, we gen a token ptr for each token ref in C++
1580 if ( GenCC && !GenAST )
1582 /* This now free's the temp set -ATG 5/6/95 */
1584 temp = set_and(elems, attribsRefdFromAction);
1585 set_orin($toksrefd, temp);
1588 else set_orin($toksrefd, elems);
1590 dif = set_dif(attribsRefdFromAction, elems);
1591 if ( set_deg(dif)>0 )
1592 err("one or more $i in action(s) refer to non-token elements");
1596 set_free(attribsRefdFromAction);
1600 <<CannotContinue=TRUE;>>
1602 /* rule element_label */
1604 element_label > [LabelEntry *label]
1605 : <<TermEntry *t=NULL; LabelEntry *l=NULL; RuleEntry *r=NULL; char *lab;>>
1606 LABEL <<lab = mystrdup(LATEXT(1));>>
1608 UsedNewStyleLabel = 1;
1609 if ( UsedOldStyleAttrib ) err("cannot mix with new-style labels with old-style $i");
1610 t = (TermEntry *) hash_get(Tname, lab);
1611 if ( t==NULL ) t = (TermEntry *) hash_get(Texpr, lab);
1612 if ( t==NULL ) r = (RuleEntry *) hash_get(Rname, lab);
1614 err(eMsg1("label definition clashes with token/tokclass definition: '%s'", lab));
1617 else if ( r!=NULL ) {
1618 err(eMsg1("label definition clashes with rule definition: '%s'", lab));
1622 /* we don't clash with anybody else */
1623 l = (LabelEntry *) hash_get(Elabel, lab);
1624 if ( l==NULL ) { /* ok to add new element label */
1625 l = (LabelEntry *)hash_add(Elabel,
1627 (Entry *)newLabelEntry(lab));
1628 /* add to list of element labels for this rule */
1629 list_add(&CurElementLabels, (void *)lab);
1630 /* MR7 */ leAdd(l); /* list of labels waiting for exception group definitions */
1634 err(eMsg1("label definitions must be unique per rule: '%s'", lab));
1644 element[int old_not, int first_on_line, int use_def_MT_handler] > [Node *node]
1647 Predicate *pred = NULL;
1648 int local_use_def_MT_handler=0;
1651 set toksrefd, rulesrefd;
1653 TokNode *p=NULL; RuleRefNode *q; int approx=0;
1654 LabelEntry *label=NULL;
1658 int height; /* MR11 */
1659 int equal_height; /* MR11 */
1661 char* pFirstSetSymbol = NULL; /* MR21 */
1665 {element_label>[label]}
1668 term = (TermEntry *) hash_get(Tname, LATEXT(1));
1669 if ( term==NULL && UserDefdTokens ) {
1670 err("implicit token definition not allowed with #tokdefs");
1671 $$.left = $$.right = NULL;
1674 $$ = buildToken(LATEXT(1));
1675 p=((TokNode *)((Junction *)$$.left)->p1);
1676 term = (TermEntry *) hash_get(Tname, LATEXT(1));
1677 require( term!= NULL, "hash table mechanism is broken");
1678 p->tclass = term->tclass;
1679 p->complement = $old_not;
1680 if ( label!=NULL ) {
1681 p->el_label = label->str;
1682 label->elem = (Node *)p;
1688 <<if ( p!=NULL ) setUpperRange(p, LATEXT(1));>>
1690 <<if ( p!=NULL ) setUpperRange(p, LATEXT(1));>>
1694 if ( p!=NULL && (p->upper_range!=0 || p->tclass || $old_not) )
1695 list_add(&MetaTokenNodes, (void *)p);
1697 ( "^" <<if ( p!=NULL ) p->astnode=ASTroot;>>
1698 | <<if ( p!=NULL ) p->astnode=ASTchild;>>
1699 | "!" <<if ( p!=NULL ) p->astnode=ASTexclude;>>
1701 { "\@" <<local_use_def_MT_handler = 1;>> }
1703 if ( p!=NULL && $first_on_line ) {
1704 CurAltStart = (Junction *)$$.left;
1705 altAdd(CurAltStart); /* MR7 */
1706 p->altstart = CurAltStart;
1709 p->use_def_MT_handler = $use_def_MT_handler || local_use_def_MT_handler;
1714 term = (TermEntry *) hash_get(Texpr, LATEXT(1));
1715 if ( term==NULL && UserDefdTokens ) {
1716 err("implicit token definition not allowed with #tokdefs");
1717 $$.left = $$.right = NULL;
1720 $$ = buildToken(LATEXT(1)); p=((TokNode *)((Junction *)$$.left)->p1);
1721 p->complement = $old_not;
1722 if ( label!=NULL ) {
1723 p->el_label = label->str;
1724 label->elem = (Node *)p;
1730 <<if ( p!=NULL ) setUpperRange(p, LATEXT(1));>>
1732 <<if ( p!=NULL ) setUpperRange(p, LATEXT(1));>>
1735 ( "^" <<if ( p!=NULL ) p->astnode=ASTroot;>>
1736 | <<if ( p!=NULL ) p->astnode=ASTchild;>>
1737 | "!" <<if ( p!=NULL ) p->astnode=ASTexclude;>>
1739 { "\@" <<local_use_def_MT_handler = 1;>> }
1741 if ( p!=NULL && (p->upper_range!=0 || p->tclass || $old_not) )
1742 list_add(&MetaTokenNodes, (void *)p);
1745 if ( $first_on_line ) {
1746 CurAltStart = (Junction *)$$.left;
1747 altAdd(CurAltStart); /* MR7 */
1748 p->altstart = CurAltStart;
1751 p->use_def_MT_handler = $use_def_MT_handler || local_use_def_MT_handler;
1755 | <<if ( $old_not ) warn("~ WILDCARD is an undefined operation (implies 'nothing')");>>
1757 <<$$ = buildWildCard(LATEXT(1)); p=((TokNode *)((Junction *)$$.left)->p1);>>
1758 ( "^" <<p->astnode=ASTroot;>>
1759 | <<p->astnode=ASTchild;>>
1760 | "!" <<p->astnode=ASTexclude;>>
1762 <<list_add(&MetaTokenNodes, (void *)p);>>
1764 if ( $first_on_line ) {
1765 CurAltStart = (Junction *)$$.left;
1766 altAdd(CurAltStart); /* MR7 */
1767 p->altstart = CurAltStart;
1768 if ( label!=NULL ) {
1769 p->el_label = label->str;
1770 label->elem = (Node *)p;
1776 | <<if ( $old_not ) warn("~ NONTERMINAL is an undefined operation");>>
1778 <<$$ = buildRuleRef(LATEXT(1));>>
1779 { "!" <<q = (RuleRefNode *) ((Junction *)$$.left)->p1;
1780 q->astnode=ASTexclude;>>
1783 PassAction <<addParm(((Junction *)$$.left)->p1, LATEXT(1));>>
1785 <<rr=(RuleRefNode *) ((Junction *)$$.left)->p1;>>
1790 a = (char *) calloc(strlen(LATEXT(1))+1, sizeof(char));
1791 require(a!=NULL, "rule element: cannot allocate assignment");
1792 strcpy(a, LATEXT(1));
1797 if ( label!=NULL ) {
1798 rr->el_label = label->str;
1799 label->elem = (Node *)rr;
1801 if ( $first_on_line ) {
1802 CurAltStart = (Junction *)$$.left;
1803 altAdd(CurAltStart); /* MR7 */
1804 ((RuleRefNode *)((Junction *)$$.left)->p1)->altstart = CurAltStart;
1810 | <<if ( $old_not ) warn("~ ACTION is an undefined operation");>>
1811 Action <<$0 = buildAction(LATEXT(1),action_file,action_line, 0);>>
1812 <<if ( $first_on_line ) { /* MR7 */
1813 CurAltStart = (Junction *)$0.left; /* MR7 */
1814 altAdd(CurAltStart); /* MR7 */
1816 <<$node = (Node *) ((Junction *)$0.left)->p1;>>
1818 | <<if ( $old_not ) warn("~ SEMANTIC-PREDICATE is an undefined operation");>>
1819 Pred <<$0 = buildAction(LATEXT(1),action_file,action_line, 1);>>
1820 <<act = (ActionNode *) ((Junction *)$0.left)->p1;>>
1821 <<if (numericActionLabel) { /* MR10 */
1822 list_add(&NumericPredLabels,act); /* MR10 */
1823 numericActionLabel=0; /* MR10 */
1829 a = (char *) calloc(strlen(LATEXT(1))+1, sizeof(char));
1830 require(a!=NULL, "rule element: cannot allocate predicate fail action");
1831 strcpy(a, LATEXT(1));
1835 <<if ( $first_on_line ) { /* MR7 */
1836 CurAltStart = (Junction *)$0.left; /* MR7 */
1837 altAdd(CurAltStart); /* MR7 */
1839 <<$node = (Node *)act;>>
1841 | <<if ( $old_not ) warn("~ BLOCK is an undefined operation");>>
1843 if (BlkLevel >= MAX_BLK_LEVEL) fatal("Blocks nested too deeply");
1844 /* MR23 */ CurBlockID_array[BlkLevel] = CurBlockID;
1845 /* MR23 */ CurAltNum_array[BlkLevel] = CurAltNum;
1848 ( "approx" <<approx=LL_k;>>
1849 | "LL\(1\)" <<approx = 1;>> /* MR20 */
1850 | "LL\(2\)" <<approx = 2;>> /* MR20 */
1854 /* MR21 */ { FirstSetSymbol
1856 /* MR21 */ ( NonTerminal
1858 /* MR21 */ pFirstSetSymbol = (char *) calloc(strlen(LATEXT(1))+1,
1859 /* MR21 */ sizeof(char));
1860 /* MR21 */ require(pFirstSetSymbol!=NULL,
1861 /* MR21 */ "cannot allocate first set name");
1862 /* MR21 */ strcpy(pFirstSetSymbol, LATEXT(1));
1864 /* MR21 */ | TokenTerm
1866 /* MR21 */ pFirstSetSymbol = (char *) calloc(strlen(LATEXT(1))+1,
1867 /* MR21 */ sizeof(char));
1868 /* MR21 */ require(pFirstSetSymbol!=NULL,
1869 /* MR21 */ "cannot allocate first set name");
1870 /* MR21 */ strcpy(pFirstSetSymbol, LATEXT(1));
1878 "\(" block[&toksrefd,&rulesrefd] "\)"
1880 /* MR23 */ CurBlockID_array[BlkLevel] = (-1);
1881 /* MR23 */ CurAltNum_array[BlkLevel] = (-1);
1885 ( "\*" <<$$ = makeLoop($$,approx,pFirstSetSymbol);>>
1886 | "\+" <<$$ = makePlus($$,approx,pFirstSetSymbol);>>
1889 ( "=>" <<ampersandStyle=0;>>
1890 | "&&" <<ampersandStyle=1;>> /* MR10 (g)? && <<p>>? */
1892 Pred /* generalized predicate */
1893 /* first make into a predicate */
1894 <<$$ = buildAction(LATEXT(1),action_file,action_line,1);>>
1895 <<act = (ActionNode *) ((Junction *)$$.left)->p1;>>
1896 <<semDepth=predicateLookaheadDepth(act);>> /* MR10 */
1897 <<if (numericActionLabel) { /* MR10 */
1898 list_add(&NumericPredLabels,act); /* MR10 */
1899 numericActionLabel=0; /* MR10 */
1905 a = (char *)calloc(strlen(LATEXT(1))+1, sizeof(char));
1906 require(a!=NULL, "rule element: cannot allocate predicate fail action");
1907 strcpy(a, LATEXT(1));
1911 <<if ($first_on_line) { /* MR7 */
1912 CurAltStart=(Junction *)$$.left; /* MR7 */
1913 altAdd(CurAltStart); /* MR7 */
1915 <<$node = (Node *)act;>>
1917 /* for now, just snag context */
1919 pred = computePredFromContextGuard(blk,&predMsgDone); /* MR10 */
1920 if ( pred==NULL) { /* MR10 */
1921 if ( !predMsgDone) err("invalid or missing context guard"); /* MR10 */
1922 predMsgDone=1; /* MR10 */
1924 act->guardNodes=(Junction *)blk.left; /* MR11 */
1925 pred->expr = act->action;
1927 /* MR10 */ pred->ampersandStyle = ampersandStyle; /* 0 means (g)? => ... 1 means (g)? && ... */
1928 /* MR13 */ if (pred->tcontext != NULL) {
1929 /* MR13 */ height=MR_max_height_of_tree(pred->tcontext);
1930 /* MR13 */ equal_height=MR_all_leaves_same_height(pred->tcontext,height);
1931 /* MR13 */ if (! equal_height) {
1932 /* MR13 */ errFL("in guarded predicates all tokens in the guard must be at the same height",
1933 /* MR13 */ FileStr[act->file],act->line);
1936 /* MR10 */ if (ampersandStyle) {
1937 /* MR10 */ act->ampersandPred = pred;
1938 /* MR11 */ if (! HoistPredicateContext) {
1939 /* MR11 */ errFL("without \"-prc on\" (guard)? && <<pred>>? ... doesn't make sense",
1940 /* MR11 */ FileStr[act->file],act->line);
1943 /* MR10 */ act->guardpred = pred;
1945 /* MR10 */ if (pred->k != semDepth) {
1946 /* MR10 */ warn(eMsgd2("length of guard (%d) does not match the length of semantic predicate (%d)",
1947 /* MR10 */ pred->k,semDepth));
1951 | <<$$ = makeBlk($$,approx,pFirstSetSymbol);
1953 ((Junction *) ((Junction *)$$.left)->p1)->guess=1;
1954 if ( !$first_on_line ) {
1955 err("(...)? predicate must be first element of production");
1959 | <<$$ = makeBlk($$,approx,pFirstSetSymbol);>>
1962 if ( pred==NULL && !predMsgDone) { /* MR10 */
1963 ((Junction *)((Junction *)$$.left)->p1)->blockid = CurBlockID;
1964 ((Junction *)((Junction *)$$.left)->p1)->tokrefs = toksrefd;
1965 ((Junction *)((Junction *)$$.left)->p1)->rulerefs = rulesrefd;
1966 if ( $first_on_line ) { /* MR7 */
1967 CurAltStart = (Junction *)((Junction *)((Junction *)$$.left)->p1); /* MR7 */
1968 altAdd(CurAltStart); /* MR7 */
1970 $node = (Node *) ((Junction *)$$.left)->p1;
1974 | "\{" block[&toksrefd,&rulesrefd]
1975 <<$$ = makeOpt($2,approx,pFirstSetSymbol);
1976 /* MR23 */ CurBlockID_array[BlkLevel] = (-1);
1977 /* MR23 */ CurAltNum_array[BlkLevel] = (-1);
1982 ((Junction *)((Junction *)$$.left)->p1)->blockid = CurBlockID;
1983 ((Junction *)((Junction *)$$.left)->p1)->tokrefs = toksrefd;
1984 ((Junction *)((Junction *)$$.left)->p1)->rulerefs = rulesrefd;
1986 <<if ( $first_on_line ) { /* MR7 */
1987 CurAltStart = (Junction *) ((Junction *)((Junction *)$$.left)->p1); /* MR7 */
1988 altAdd(CurAltStart); /* MR7 */
1991 <<$node = (Node *) ((Junction *)$$.left)->p1;>>
1995 /* Error catching alternatives */
1996 | "\*" <<warn("don't you want a ')' with that '*'?"); CannotContinue=TRUE;>>
1997 | "\+" <<warn("don't you want a ')' with that '+'?"); CannotContinue=TRUE;>>
1998 | "\>" <<warn("'>' can only appear after a nonterminal"); CannotContinue=TRUE;>>
1999 | PassAction <<warn("[...] out of context 'rule > [...]'");
2000 CannotContinue=TRUE;>>
2002 <<CannotContinue=TRUE;>>
2004 /* rule default_exception_handler */
2006 default_exception_handler
2007 : exception_group > [DefaultExGroup]
2010 /* rule exception_group */
2012 exception_group > [ExceptionGroup *eg]
2013 : <<ExceptionHandler *h; LabelEntry *label=NULL; /* MR6 */
2014 FoundException = 1; FoundExceptionGroup = 1;>> /* MR6 */
2016 "exception" <<$eg = (ExceptionGroup *)calloc(1, sizeof(ExceptionGroup));>>
2018 PassAction /* did they attach a label? */
2021 p[strlen(p)-1] = '\0'; /* kill trailing space */
2022 label = (LabelEntry *) hash_get(Elabel, LATEXT(1)+1);
2025 err(eMsg1("unknown label in exception handler: '%s'", LATEXT(1)+1));
2029 ( exception_handler > [h]
2030 <<list_add(&($eg->handlers), (void *)h);>>
2032 { "default" ":" Action
2034 ExceptionHandler *eh = (ExceptionHandler *)
2035 calloc(1, sizeof(ExceptionHandler));
2036 char *a = (char *) calloc(strlen(LATEXT(1))+1, sizeof(char));
2037 require(eh!=NULL, "exception: cannot allocate handler");
2038 require(a!=NULL, "exception: cannot allocate action");
2039 strcpy(a, LATEXT(1));
2041 eh->signalname = (char *) calloc(strlen("default")+1, sizeof(char));
2042 require(eh->signalname!=NULL, "exception: cannot allocate sig name");
2043 strcpy(eh->signalname, "default");
2044 list_add(&($eg->handlers), (void *)eh);
2049 if ( label!=NULL ) {
2050 /* Record ex group in sym tab for this label */
2051 if ( label->ex_group!=NULL ) {
2052 err(eMsg1("duplicate exception handler for label '%s'",label->str));
2054 label->ex_group = $eg;
2055 /* Label the exception group itself */
2056 $eg->label = label->str;
2057 /* Make the labelled element pt to the exception also */
2058 /* MR6 */ if (label->elem == NULL) {
2059 /* MR6 */ err(eMsg1("reference in exception handler to undefined label '%s'",label->str));
2061 switch ( label->elem->ntype ) {
2064 RuleRefNode *r = (RuleRefNode *)label->elem;
2070 TokNode *t = (TokNode *)label->elem;
2075 /* MR6 */ }; /* end test on label->elem */
2076 } /* end test on label->ex_group */
2078 } /* end test on exception label */
2081 /* MR7 */ if (BlkLevel == 1 && label == NULL) {
2082 /* MR7 */ $eg->forRule=1;
2083 /* MR7 */ } else if (label == NULL) {
2084 /* MR7 */ $eg->altID = makeAltID(CurBlockID_array[BlkLevel], CurAltNum_array[BlkLevel]);
2085 /* MR7 */ egAdd($eg);
2087 /* MR7 */ $eg->labelEntry=label;
2090 /* MR7 */ /* You may want to remove this exc from the rule list */
2091 /* MR7 */ /* and handle at the labeled element site. */
2093 /* MR7 */ if (label != NULL) {
2094 /* MR7 */ $eg = NULL;
2099 <<CannotContinue=TRUE;>>
2101 /* rule exception_handler */
2103 exception_handler > [ExceptionHandler *eh]
2104 : <<;>> /* MR9 Removed unreferenced variable "a" */
2107 $eh = (ExceptionHandler *)calloc(1, sizeof(ExceptionHandler));
2108 require($eh!=NULL, "exception: cannot allocate handler");
2112 $eh->signalname = (char *) calloc(strlen(LATEXT(1))+1, sizeof(char));
2113 require($eh->signalname!=NULL, "exception: cannot allocate sig name");
2114 strcpy($eh->signalname, LATEXT(1));
2118 $eh->signalname = (char *) calloc(strlen(LATEXT(1))+1, sizeof(char));
2119 require($eh->signalname!=NULL, "exception: cannot allocate sig name");
2120 strcpy($eh->signalname, LATEXT(1));
2124 { <<$eh->action = NULL;>>
2127 $eh->action = (char *) calloc(strlen(LATEXT(1))+1, sizeof(char));
2128 require($eh->action!=NULL, "exception: cannot allocate action");
2129 strcpy($eh->action, LATEXT(1));
2133 <<CannotContinue=TRUE;>>
2135 #token NonTerminal "[a-z] [A-Za-z0-9_]*"
2137 while ( zzchar==' ' || zzchar=='\t' ) {
2140 if ( zzchar == ':' && inAlt ) NLA = LABEL;
2142 #token TokenTerm "[A-Z] [A-Za-z0-9_]*"
2144 while ( zzchar==' ' || zzchar=='\t' ) {
2147 if ( zzchar == ':' && inAlt ) NLA = LABEL;
2149 #token "{\\}#[A-Za-z0-9_]*" <<warn(eMsg1("unknown meta-op: %s",LATEXT(1))); zzskip(); >>
2151 #lexclass PARSE_ENUM_FILE
2153 #token "[\t\ ]+" << zzskip(); >> /* Ignore White */
2154 #token "\n|\r|\r\n" << zzline++; zzskip(); >> /* Track Line # */
2155 #token "//" << zzmode(TOK_DEF_CPP_COMMENTS); zzmore(); >>
2156 #token "/\*" << zzmode(TOK_DEF_COMMENTS); zzskip(); >>
2157 #token "#ifdef" << zzmode(TOK_DEF_CPP_COMMENTS); zzskip(); >>
2158 #token "#if" << zzmode(TOK_DEF_CPP_COMMENTS); zzskip(); >>
2159 #token "#ifndef" << ; >>
2160 #token "#else" << zzmode(TOK_DEF_CPP_COMMENTS); zzskip(); >>
2161 #token "#endif" << zzmode(TOK_DEF_CPP_COMMENTS); zzskip(); >>
2162 #token "#undef" << zzmode(TOK_DEF_CPP_COMMENTS); zzskip(); >>
2163 #token "#import" << zzmode(TOK_DEF_CPP_COMMENTS); zzskip(); >>
2166 /* rule enum_file */
2168 enum_file[char *fname]
2170 { "#define" ID /* ignore if it smells like a gate */
2171 /* First #define after the first #ifndef (if any) is ignored */
2174 ( ( enum_def[$fname] )+
2182 defines[char *fname]
2183 : <<int v; int maxt=(-1); char *t;>> /* MR3 */
2186 <<t = mystrdup(LATEXT(1));>>
2189 v = atoi(LATEXT(1));
2190 /* fprintf(stderr, "#token %s=%d\n", t, v);*/
2192 /* MR2 Andreas Magnusson (Andreas.Magnusson@mailbox.swipnet.se) */
2193 /* MR2 Fix to bug introduced by 1.33MR1 for #tokdefs */
2194 /* MR2 Don't let #tokdefs be confused by */
2195 /* MR2 DLGminToken and DLGmaxToken */
2197 if ( ! isDLGmaxToken(t)) { /* MR2 */
2199 if ( v>maxt ) maxt=v;
2200 if ( Tnum( t ) == 0 ) {
2201 addForcedTname( t, v );
2203 warnFL(eMsg1("redefinition of token %s; ignored",t),$fname,zzline);
2208 <<TokenNum = maxt + 1;>>
2213 enum_def[char *fname]
2214 : <<int v= 0; int maxt=(-1); char *t;>> /* MR3 */
2218 <<t = mystrdup(LATEXT(1));>>
2219 ( "=" INT <<v=atoi(LATEXT(1));>>
2223 /* fprintf(stderr, "#token %s=%d\n", t, v);*/
2225 if ( v>maxt ) maxt=v; /* MR3 */
2226 if ( Tnum( t ) == 0 ) addForcedTname( t, v );
2228 warnFL(eMsg1("redefinition of token %s; ignored",t),$fname,zzline);
2233 /* MR2 Andreas Magnusson (Andreas.Magnusson@mailbox.swipnet.se) */
2234 /* MR2 Fix to bug introduced by 1.33MR1 for #tokdefs */
2235 /* MR2 Don't let #tokdefs be confused by */
2236 /* MR2 DLGminToken and DLGmaxToken */
2239 <<isDLGmaxToken(LATEXT(1))>>? ID { "=" INT } /* MR2 */
2241 <<t = mystrdup(LATEXT(1));>>
2242 ( "=" INT <<v=atoi(LATEXT(1));>>
2246 /* fprintf(stderr, "#token %s=%d\n", t, v);*/
2248 if ( v>maxt ) maxt=v; /* MR3 */
2249 if ( Tnum( t ) == 0 ) addForcedTname( t, v );
2251 warnFL(eMsg1("redefinition of token %s; ignored",t),$fname,zzline);
2258 <<TokenNum = maxt + 1;>> /* MR3 */
2262 #token ID "[a-zA-Z_][_a-zA-Z0-9]*"
2266 /* MR14 Arpad Beszedes 26-May-98
2267 Add support for #line directives when antlr source is pre-processed
2275 getFileNameFromTheLineInfo(char *toStr, char *fromStr)
2277 getFileNameFromTheLineInfo(toStr, fromStr)
2278 char *toStr, *fromStr;
2283 if (!fromStr || !toStr) return toStr;
2285 /* find the first " */
2289 (fromStr[i] != '\n') &&
2290 (fromStr[i] != '\r') &&
2291 (fromStr[i] != '\"');
2292 i++) /* nothing */ ;
2294 if ( (i == MaxFileName) ||
2295 (fromStr[i] == '\n') ||
2296 (fromStr[i] == '\r') ) {
2300 /* find the second " */
2304 (fromStr[j] != '\n') &&
2305 (fromStr[j] != '\r') &&
2306 (fromStr[j] != '\"');
2307 j++) /* nothing */ ;
2309 if ((j == MaxFileName) ||
2310 (fromStr[j] == '\n') ||
2311 (fromStr[j] == '\r') ) {
2315 /* go back until the last / or \ */
2318 (fromStr[k] != '\"') &&
2319 (fromStr[k] != '/') &&
2320 (fromStr[k] != '\\');
2321 k--) /* nothing */ ;
2323 /* copy the string after " / or \ into toStr */
2325 for (i=k+1; fromStr[i] != '\"'; i++) {
2326 toStr[i-k-1] = fromStr[i];
2329 toStr[i-k-1] = '\0';
2334 /* MR14 end of a block to support #line in antlr source code */
2340 /* MR2 Andreas Magnusson (Andreas.Magnusson@mailbox.swipnet.se) */
2341 /* MR2 Fix to bug introduced by 1.33MR1 for #tokdefs */
2342 /* MR2 Don't let #tokdefs be confused by */
2343 /* MR2 DLGminToken and DLGmaxToken */
2345 /* semantic check on DLGminToken and DLGmaxmaxToken in #tokdefs */
2348 static int isDLGmaxToken(char *Token)
2350 static int isDLGmaxToken(Token)
2354 static char checkStr1[] = "DLGmaxToken";
2355 static char checkStr2[] = "DLGminToken";
2357 if (strcmp(Token, checkStr1) == 0)
2359 else if (strcmp(Token, checkStr2) == 0)
2365 /* semantics of #token */
2368 chkToken(char *t, char *e, char *a, int tnum)
2370 chkToken(t,e,a,tnum)
2377 /* check to see that they don't try to redefine a token as a token class */
2379 p = (TermEntry *) hash_get(Tname, t);
2380 if ( p!=NULL && p->classname ) {
2381 err(eMsg1("redefinition of #tokclass '%s' to #token not allowed; ignored",t));
2382 if ( a!=NULL ) free((char *)a);
2387 if ( t==NULL && e==NULL ) { /* none found */
2388 err("#token requires at least token name or rexpr");
2390 else if ( t!=NULL && e!=NULL ) { /* both found */
2391 if ( UserDefdTokens ) { /* if #tokdefs, must not define new */
2392 p = (TermEntry *) hash_get(Tname, t);
2394 err(eMsg1("new token definition '%s' not allowed - only #token with name already defined by #tokdefs file allowed",t));
2400 if ( hasAction(e) ) {
2401 err(eMsg1("redefinition of action for %s; ignored",e));
2403 else setHasAction(e, a);
2406 else if ( t!=NULL ) { /* only one found */
2407 if ( UserDefdTokens ) {
2408 p = (TermEntry *) hash_get(Tname, t);
2410 err(eMsg1("new token definition '%s' not allowed - only #token with name already defined by #tokdefs file allowed",t));
2414 if ( Tnum( t ) == 0 ) addTname( t );
2416 err(eMsg1("redefinition of token %s; ignored",t));
2419 err(eMsg1("action cannot be attached to a token name (%s); ignored",t));
2423 else if ( e!=NULL ) {
2424 if ( Tnum( e ) == 0 ) addTexpr( e );
2426 if ( hasAction(e) ) {
2427 err(eMsg1("redefinition of action for expr %s; ignored",e));
2429 else if ( a==NULL ) {
2430 err(eMsg1("redefinition of expr %s; ignored",e));
2433 if ( a!=NULL ) setHasAction(e, a);
2436 /* if a token type number was specified, then add the token ID and 'tnum'
2437 * pair to the ForcedTokens list. (only applies if an id was given)
2439 if ( t!=NULL && tnum>0 )
2441 if ( set_el(tnum, reserved_positions) )
2443 err(eMsgd("a token has already been forced to token number %d; ignored", tnum));
2447 list_add(&ForcedTokens, newForcedToken(t,tnum));
2448 set_orel(tnum, &reserved_positions);
2457 match_token(char *s, char **nxt)
2464 if ( !(*s>='A' && *s<='Z') ) return 0;
2466 while ( (*s>='a' && *s<='z') ||
2467 (*s>='A' && *s<='Z') ||
2468 (*s>='0' && *s<='9') ||
2473 if ( *s!=' ' && *s!='}' ) return 0;
2480 match_rexpr(char *s, char **nxt)
2487 if ( *s!='"' ) return 0;
2491 if ( *s=='\n' || *s=='\r' ) /* MR13 */
2492 warn("eoln found in regular expression");
2493 if ( *s=='\\' ) s++;
2501 * Walk a string "{ A .. Z }" where A..Z is a space separated list
2502 * of token references (either labels or reg exprs). Return a
2503 * string "inlineX_set" for some unique integer X. Basically,
2504 * we pretend as if we had seen "#tokclass inlineX { A .. Z }"
2505 * on the input stream outside of an action.
2516 fprintf(stderr, "found consumeUntil( {...} )\n");
2517 while ( *s==' ' || *s=='\t' || *s=='\n' || *s=='\r' ) {s++;}
2520 err("malformed consumeUntil( {...} ); missing '{'");
2524 while ( *s==' ' || *s=='\t' || *s=='\n' || *s=='\r' ) {s++;}
2527 if ( match_token(s,&nxt) ) fprintf(stderr, "found token %s\n", s);
2528 else if ( match_rexpr(s,&nxt) ) fprintf(stderr, "found rexpr %s\n", s);
2530 err("invalid element in consumeUntil( {...} )");
2534 while ( *s==' ' || *s=='\t' || *s=='\n' || *s=='\r' ) {s++;}
2536 return "inlineX_set";
2541 /* ANTLR-specific syntax error message generator
2542 * (define USER_ZZSYN when compiling so don't get 2 definitions)
2546 zzsyn(char *text, int tok, char *egroup, SetWordType *eset, int etok,
2547 int k, char *bad_text)
2549 zzsyn(text, tok, egroup, eset, etok, k, bad_text)
2550 char *text, *egroup, *bad_text;
2557 fprintf(stderr, ErrHdr, FileStr[CurFile]!=NULL?FileStr[CurFile]:"stdin", zzline);
2558 fprintf(stderr, " syntax error at \"%s\"", (tok==zzEOF_TOKEN)?"EOF":text);
2559 if ( !etok && !eset ) {fprintf(stderr, "\n"); return;}
2560 if ( k==1 ) fprintf(stderr, " missing");
2563 fprintf(stderr, "; \"%s\" not", bad_text);
2564 if ( zzset_deg(eset)>1 ) fprintf(stderr, " in");
2566 if ( zzset_deg(eset)>0 ) zzedecode(eset);
2567 else fprintf(stderr, " %s", zztokens[etok]);
2568 if ( strlen(egroup) > (size_t)0 ) fprintf(stderr, " in %s", egroup);
2569 fprintf(stderr, "\n");
2575 void mark_label_used_in_sem_pred(LabelEntry *le) /* MR10 */
2577 void mark_label_used_in_sem_pred(le) /* MR10 */
2582 require (le->elem->ntype == nToken,"mark_label_used... ntype != nToken");
2583 tn=(TokNode *)le->elem;
2584 require (tn->label != 0,"mark_label_used... TokNode has no label");
2585 tn->label_used_in_semantic_pred=1;