2 * A n t l r T r a n s l a t i o n H e a d e r
4 * Terence Parr, Will Cohen, and Hank Dietz: 1989-1994
5 * Purdue University Electrical Engineering
6 * With AHPCRC, University of Minnesota
10 #define ANTLR_VERSION 132
22 #define zzcr_attr(attr,tok,t)
34 static void chkToken(char *, char *, char *, int);
36 static void chkToken();
39 static int class_nest_level = 0;
41 extern set attribsRefdFromAction;
42 extern int UsedOldStyleAttrib;
43 extern int UsedNewStyleLabel;
62 if ( !((setwd1[LA(1)]&0x1))) break;
64 zzmatch(88); zzCONSUME;
67 if ( HdrAction==NULL ) {
68 HdrAction = (char *) calloc(strlen(LATEXT(1))+1, sizeof(char));
69 require(HdrAction!=NULL, "rule grammar: cannot allocate header action");
70 strcpy(HdrAction, LATEXT(1));
72 else warn("additional #header statement ignored");
78 zzmatch(89); zzCONSUME;
82 warn("#parser meta-op incompatible with -CC; ignored");
85 if ( strcmp(ParserName,"zzparser")==0 ) {
86 ParserName=StripQuotes(mystrdup(LATEXT(1)));
87 if ( RulePrefix[0]!='\0' )
89 warn("#parser meta-op incompatible with '-gp prefix'; '-gp' ignored");
93 else warn("additional #parser statement ignored");
100 zzmatch(90); zzCONSUME;
104 zzantlr_state st; FILE *f; struct zzdlg_state dst;
105 UserTokenDefsFile = mystrdup(LATEXT(1));
106 zzsave_antlr_state(&st);
107 zzsave_dlg_state(&dst);
108 fname = mystrdup(LATEXT(1));
109 f = fopen(StripQuotes(fname), "r");
110 if ( f==NULL ) {warn(eMsg1("cannot open token defs file '%s'", fname+1));}
112 ANTLRm(enum_file(fname+1), f, PARSE_ENUM_FILE);
115 zzrestore_antlr_state(&st);
116 zzrestore_dlg_state(&dst);
133 if ( !((setwd1[LA(1)]&0x2))) break;
134 if ( (LA(1)==Action) ) {
137 UserAction *ua = newUserAction(LATEXT(1));
138 ua->file = action_file; ua->line = action_line;
139 if ( class_nest_level>0 ) list_add(&class_before_actions, ua);
140 else list_add(&BeforeActions, ua);
146 if ( (LA(1)==101) ) {
150 if ( (LA(1)==102) ) {
154 if ( (LA(1)==105) ) {
158 if ( (LA(1)==103) ) {
162 if ( (LA(1)==104) ) {
166 if ( (LA(1)==122) ) {
167 default_exception_handler();
177 if ( class_nest_level==0 )
178 warn("missing class definition for trailing '}'");
197 g=zzaArg(zztasp1,3); SynDiag = (Junction *) zzaArg(zztasp1,3 ).left;
203 if ( !((setwd1[LA(1)]&0x4))) break;
204 if ( (LA(1)==NonTerminal) ) {
206 if ( zzaArg(zztasp2,1 ).left!=NULL ) {g.right = NULL; g = Or(g, zzaArg(zztasp2,1));}
209 if ( (LA(1)==102) ) {
213 if ( (LA(1)==105) ) {
217 if ( (LA(1)==103) ) {
221 if ( (LA(1)==104) ) {
232 if ( class_nest_level==0 )
233 warn("missing class definition for trailing '}'");
254 if ( !((setwd1[LA(1)]&0x8))) break;
255 if ( (LA(1)==Action) ) {
258 UserAction *ua = newUserAction(LATEXT(1));
259 ua->file = action_file; ua->line = action_line;
260 if ( class_nest_level>0 ) list_add(&class_after_actions, ua);
261 else list_add(&AfterActions, ua);
267 if ( (LA(1)==101) ) {
271 if ( (LA(1)==103) ) {
275 if ( (LA(1)==104) ) {
286 if ( class_nest_level==0 )
287 warn("missing class definition for trailing '}'");
302 zzmatch(Eof); zzCONSUME;
308 zzsyn(zzMissText, zzBadTok, (ANTLRChar *)"", zzMissSet, zzMissTok, zzErrk, zzBadText);
309 zzresynch(setwd1, 0x10);
324 int go=1; char name[MaxRuleName+1];
325 zzmatch(92); zzCONSUME;
330 if ( (LA(1)==NonTerminal) ) {
331 zzmatch(NonTerminal);
332 if(go) strncpy(name,LATEXT(1),MaxRuleName);
337 if ( (LA(1)==TokenTerm) ) {
339 if(go) strncpy(name,LATEXT(1),MaxRuleName);
343 else {zzFAIL(1,zzerr1,&zzMissSet,&zzMissText,&zzBadTok,&zzBadText,&zzErrk); goto fail;}
349 if ( CurrentClassName[0]!='\0' && strcmp(CurrentClassName,name)!=0
351 err("only one grammar class allowed in this release");
354 else strcpy(CurrentClassName, name);
355 if ( !GenCC ) { err("class meta-op used without C++ option"); }
358 no_classes_found = 0;
359 if ( class_nest_level>=1 ) {warn("cannot have nested classes");}
360 else class_nest_level++;
368 zzsyn(zzMissText, zzBadTok, (ANTLRChar *)"", zzMissSet, zzMissTok, zzErrk, zzBadText);
369 zzresynch(setwd1, 0x20);
385 ListNode *ex_groups = NULL;
387 RuleEntry *q; Junction *p; Graph r; int f, l; ECnode *e;
388 set toksrefd, rulesrefd;
389 char *pdecl=NULL, *ret=NULL, *a; CurRetDef = CurParmDef = NULL;
391 CurElementLabels = NULL;
392 /* We want a new element label hash table for each rule */
393 if ( Elabel!=NULL ) killHashTable(Elabel);
394 Elabel = newHashTable();
395 attribsRefdFromAction = empty;
396 zzmatch(NonTerminal);
398 if ( hash_get(Rname, LATEXT(1))!=NULL ) {
399 err(eMsg1("duplicate rule definition: '%s'",LATEXT(1)));
404 q = (RuleEntry *)hash_add(Rname,
406 (Entry *)newRuleEntry(LATEXT(1)));
410 f = CurFile; l = zzline;
420 if ( q!=NULL ) q->noAST = TRUE;
432 if ( (setwd1[LA(1)]&0x40) ) {
438 zzmatch(97); zzCONSUME;
444 pdecl = (char *) calloc(strlen(LATEXT(1))+1, sizeof(char));
445 require(pdecl!=NULL, "rule rule: cannot allocate param decl");
446 strcpy(pdecl, LATEXT(1));
459 zzmatch(98); zzCONSUME;
461 ret = (char *) calloc(strlen(LATEXT(1))+1, sizeof(char));
462 require(ret!=NULL, "rule rule: cannot allocate ret type");
463 strcpy(ret, LATEXT(1));
475 if ( (LA(1)==QuotedTerm) ) {
477 if ( q!=NULL ) q->egroup=mystrdup(LATEXT(1));
485 if ( GenEClasseForRules && q!=NULL ) {
487 require(e!=NULL, "cannot allocate error class node");
488 if ( q->egroup == NULL ) {a = q->str; a[0] = (char)toupper(a[0]);}
490 if ( Tnum( a ) == 0 )
492 e->tok = addTname( a );
493 list_add(&eclasses, (char *)e);
494 if ( q->egroup == NULL ) a[0] = (char)tolower(a[0]);
495 /* refers to itself */
496 list_add(&(e->elist), mystrdup(q->str));
499 warn(eMsg1("default errclass for '%s' would conflict with token/errclass/tokclass",a));
500 if ( q->egroup == NULL ) a[0] = (char)tolower(a[0]);
509 block( &toksrefd, &rulesrefd );
510 r = makeBlk(zzaArg(zztasp1,7),0);
511 CurRuleBlk = (Junction *)r.left;
512 CurRuleBlk->blockid = CurBlockID;
513 CurRuleBlk->jtype = RuleBlk;
514 if ( q!=NULL ) CurRuleBlk->rname = q->str;
515 CurRuleBlk->file = f;
516 CurRuleBlk->line = l;
517 CurRuleBlk->pdecl = pdecl;
518 CurRuleBlk->ret = ret;
519 CurRuleBlk->lock = makelocks();
520 CurRuleBlk->pred_lock = makelocks();
521 CurRuleBlk->tokrefs = toksrefd;
522 CurRuleBlk->rulerefs = rulesrefd;
523 p = newJunction(); /* add EndRule Node */
524 ((Junction *)r.right)->p1 = (Node *)p;
525 r.right = (Node *) p;
527 p->lock = makelocks();
528 p->pred_lock = makelocks();
530 if ( q!=NULL ) q->rulenum = NumRules;
531 zzaArg(zztasp1,7) = r;
541 if ( (LA(1)==Action) ) {
543 a = (char *) calloc(strlen(LATEXT(1))+1, sizeof(char));
544 require(a!=NULL, "rule rule: cannot allocate error action");
545 strcpy(a, LATEXT(1));
546 CurRuleBlk->erraction = a;
557 while ( (LA(1)==122) ) {
558 eg = exception_group();
561 list_add(&CurExGroups, (void *)eg);
562 if ( eg->label=='\0' ) q->has_rule_exception = 1;
569 if ( q==NULL ) zzaArg(zztasp1,0 ).left = NULL; else zzaArg(zztasp1,0) = zzaArg(zztasp1,7);
571 CurRuleBlk->exceptions = CurExGroups;
572 CurRuleBlk->el_labels = CurElementLabels;
578 zzsyn(zzMissText, zzBadTok, (ANTLRChar *)"", zzMissSet, zzMissTok, zzErrk, zzBadText);
579 zzresynch(setwd1, 0x80);
595 zzmatch(101); zzCONSUME;
598 a = (char *) calloc(strlen(LATEXT(1))+1, sizeof(char));
599 require(a!=NULL, "rule laction: cannot allocate action");
600 strcpy(a, LATEXT(1));
601 list_add(&LexActions, a);
609 zzsyn(zzMissText, zzBadTok, (ANTLRChar *)"", zzMissSet, zzMissTok, zzErrk, zzBadText);
610 zzresynch(setwd2, 0x1);
625 zzmatch(102); zzCONSUME;
627 lexclass(mystrdup(LATEXT(1)));
635 zzsyn(zzMissText, zzBadTok, (ANTLRChar *)"", zzMissSet, zzMissTok, zzErrk, zzBadText);
636 zzresynch(setwd2, 0x2);
651 char *t=NULL; ECnode *e; int go=1; TermEntry *p;
652 zzmatch(103); zzCONSUME;
658 if ( (LA(1)==TokenTerm) ) {
660 t=mystrdup(LATEXT(1));
665 if ( (LA(1)==QuotedTerm) ) {
667 t=mystrdup(LATEXT(1));
671 else {zzFAIL(1,zzerr2,&zzMissSet,&zzMissText,&zzBadTok,&zzBadText,&zzErrk); goto fail;}
677 require(e!=NULL, "cannot allocate error class node");
678 e->lexclass = CurrentLexClass;
679 if ( Tnum( (t=StripQuotes(t)) ) == 0 )
681 if ( hash_get(Texpr, t) != NULL )
682 warn(eMsg1("errclass name conflicts with regular expression '%s'",t));
683 e->tok = addTname( t );
684 set_orel(e->tok, &imag_tokens);
685 require((p=(TermEntry *)hash_get(Tname, t)) != NULL,
686 "hash table mechanism is broken");
687 p->classname = 1; /* entry is errclass name, not token */
688 list_add(&eclasses, (char *)e);
692 warn(eMsg1("redefinition of errclass or conflict w/token or tokclass '%s'; ignored",t));
696 zzmatch(95); zzCONSUME;
701 if ( (LA(1)==NonTerminal) ) {
702 zzmatch(NonTerminal);
703 if ( go ) t=mystrdup(LATEXT(1));
708 if ( (LA(1)==TokenTerm) ) {
710 if ( go ) t=mystrdup(LATEXT(1));
715 if ( (LA(1)==QuotedTerm) ) {
717 if ( go ) t=mystrdup(LATEXT(1));
721 else {zzFAIL(1,zzerr3,&zzMissSet,&zzMissText,&zzBadTok,&zzBadText,&zzErrk); goto fail;}
727 if ( go ) list_add(&(e->elist), t);
732 while ( (setwd2[LA(1)]&0x4) ) {
737 if ( (LA(1)==NonTerminal) ) {
738 zzmatch(NonTerminal);
739 if ( go ) t=mystrdup(LATEXT(1));
744 if ( (LA(1)==TokenTerm) ) {
746 if ( go ) t=mystrdup(LATEXT(1));
751 if ( (LA(1)==QuotedTerm) ) {
753 if ( go ) t=mystrdup(LATEXT(1));
757 else {zzFAIL(1,zzerr4,&zzMissSet,&zzMissText,&zzBadTok,&zzBadText,&zzErrk); goto fail;}
763 if ( go ) list_add(&(e->elist), t);
769 zzmatch(91); zzCONSUME;
775 zzsyn(zzMissText, zzBadTok, (ANTLRChar *)"", zzMissSet, zzMissTok, zzErrk, zzBadText);
776 zzresynch(setwd2, 0x8);
791 char *t=NULL; TCnode *e; int go=1,tok; TermEntry *p, *term;
792 zzmatch(104); zzCONSUME;
794 t=mystrdup(LATEXT(1));
798 require(e!=NULL, "cannot allocate token class node");
799 e->lexclass = CurrentLexClass;
800 if ( Tnum( t ) == 0 )
802 e->tok = addTname( t );
803 set_orel(e->tok, &imag_tokens);
804 set_orel(e->tok, &tokclasses);
805 require((p=(TermEntry *)hash_get(Tname, t)) != NULL,
806 "hash table mechanism is broken");
807 p->classname = 1; /* entry is class name, not token */
808 p->tclass = e; /* save ptr to this tclass def */
809 list_add(&tclasses, (char *)e);
813 warn(eMsg1("redefinition of tokclass or conflict w/token '%s'; ignored",t));
817 zzmatch(95); zzCONSUME;
822 while ( (setwd2[LA(1)]&0x10) ) {
827 if ( (LA(1)==TokenTerm) ) {
830 term = (TermEntry *) hash_get(Tname, LATEXT(1));
831 if ( term==NULL && UserDefdTokens ) {
832 err("implicit token definition not allowed with #tokdefs");
835 else {t=mystrdup(LATEXT(1)); tok=addTname(LATEXT(1));}
841 if ( (LA(1)==QuotedTerm) ) {
844 term = (TermEntry *) hash_get(Texpr, LATEXT(1));
845 if ( term==NULL && UserDefdTokens ) {
846 err("implicit token definition not allowed with #tokdefs");
849 else {t=mystrdup(LATEXT(1)); tok=addTexpr(LATEXT(1));}
854 else {zzFAIL(1,zzerr5,&zzMissSet,&zzMissText,&zzBadTok,&zzBadText,&zzErrk); goto fail;}
859 if ( go ) list_add(&(e->tlist), t);
865 zzmatch(91); zzCONSUME;
871 zzsyn(zzMissText, zzBadTok, (ANTLRChar *)"", zzMissSet, zzMissTok, zzErrk, zzBadText);
872 zzresynch(setwd2, 0x20);
887 char *t=NULL, *e=NULL, *a=NULL; int tnum=0;
888 zzmatch(105); zzCONSUME;
893 if ( (LA(1)==TokenTerm) ) {
895 t=mystrdup(LATEXT(1));
902 if ( (LA(1)==106) ) {
903 zzmatch(106); zzCONSUME;
905 tnum = atoi(LATEXT(1));
920 if ( (LA(1)==QuotedTerm) ) {
922 e=mystrdup(LATEXT(1));
933 if ( (LA(1)==Action) ) {
936 a = (char *) calloc(strlen(LATEXT(1))+1, sizeof(char));
937 require(a!=NULL, "rule token: cannot allocate action");
938 strcpy(a, LATEXT(1));
945 chkToken(t, e, a, tnum);
951 zzsyn(zzMissText, zzBadTok, (ANTLRChar *)"", zzMissSet, zzMissTok, zzErrk, zzBadText);
952 zzresynch(setwd2, 0x40);
958 block( set *toksrefd, set *rulesrefd )
960 block(toksrefd,rulesrefd)
972 int saveinalt = inAlt;
976 set_clr(AST_nodes_refd_in_actions);
979 saveblah = attribsRefdFromAction;
980 attribsRefdFromAction = empty;
981 alt( toksrefd,rulesrefd );
982 b = g = zzaArg(zztasp1,1);
984 if ( ((Junction *)g.left)->p1->ntype == nAction )
986 if ( !((ActionNode *)(((Junction *)g.left)->p1))->is_predicate )
988 ((ActionNode *)(((Junction *)g.left)->p1))->init_action = TRUE;
991 ((Junction *)g.left)->blockid = CurBlockID;
996 while ( (LA(1)==122) ) {
997 eg = exception_group();
1001 eg->altID = makeAltID(CurBlockID,CurAltNum);
1002 CurAltStart->exception_label = eg->altID;
1003 list_add(&CurExGroups, (void *)eg);
1015 while ( (LA(1)==108) ) {
1020 alt( toksrefd,rulesrefd );
1021 g = Or(g, zzaArg(zztasp2,2));
1023 ((Junction *)g.left)->blockid = CurBlockID;
1028 while ( (LA(1)==122) ) {
1029 eg = exception_group();
1033 eg->altID = makeAltID(CurBlockID,CurAltNum);
1034 CurAltStart->exception_label = eg->altID;
1035 list_add(&CurExGroups, (void *)eg);
1048 zzaArg(zztasp1,0) = b;
1049 attribsRefdFromAction = saveblah; inAlt = saveinalt;
1054 CannotContinue=TRUE;
1055 zzsyn(zzMissText, zzBadTok, (ANTLRChar *)"", zzMissSet, zzMissTok, zzErrk, zzBadText);
1056 zzresynch(setwd2, 0x80);
1062 alt( set *toksrefd, set *rulesrefd )
1064 alt(toksrefd,rulesrefd)
1073 int n=0,ne=0; Graph g; int e_num=0, not=0; Node *node; set elems, dif;
1074 int first_on_line = 1, use_def_MT_handler = 0;
1075 g.left=NULL; g.right=NULL;
1083 if ( (LA(1)==84) ) {
1085 use_def_MT_handler = 1;
1097 while ( (setwd3[LA(1)]&0x1) ) {
1103 if ( (LA(1)==109) ) {
1112 node = element( not, first_on_line, use_def_MT_handler );
1114 if ( node!=NULL && node->ntype!=nAction ) first_on_line = 0;
1116 if ( zzaArg(zztasp2,2 ).left!=NULL ) {
1117 g = Cat(g, zzaArg(zztasp2,2));
1120 if ( node->ntype!=nAction ) e_num++;
1121 /* record record number of all rule and token refs */
1122 if ( node->ntype==nToken ) {
1123 TokNode *tk = (TokNode *)((Junction *)zzaArg(zztasp2,2 ).left)->p1;
1125 set_orel(e_num, &elems);
1127 else if ( node->ntype==nRuleRef ) {
1128 RuleRefNode *rn = (RuleRefNode *)((Junction *)zzaArg(zztasp2,2 ).left)->p1;
1130 set_orel(e_num, rulesrefd);
1139 if ( n == 0 ) g = emptyAlt();
1140 zzaArg(zztasp1,0) = g;
1141 /* We want to reduce number of LT(i) calls and the number of
1142 * local attribute variables in C++ mode (for moment, later we'll
1143 * do for C also). However, if trees are being built, they
1144 * require most of the attrib variables to create the tree nodes
1145 * with; therefore, we gen a token ptr for each token ref in C++
1147 if ( GenCC && !GenAST )
1149 /* This now free's the temp set -ATG 5/6/95 */
1151 temp = set_and(elems, attribsRefdFromAction);
1152 set_orin( toksrefd, temp);
1155 else set_orin( toksrefd, elems);
1157 dif = set_dif(attribsRefdFromAction, elems);
1158 if ( set_deg(dif)>0 )
1159 err("one or more $i in action(s) refer to non-token elements");
1163 set_free(attribsRefdFromAction);
1169 CannotContinue=TRUE;
1170 zzsyn(zzMissText, zzBadTok, (ANTLRChar *)"", zzMissSet, zzMissTok, zzErrk, zzBadText);
1171 zzresynch(setwd3, 0x2);
1183 PURIFY(_retv,sizeof( LabelEntry * ))
1188 TermEntry *t=NULL; LabelEntry *l=NULL; RuleEntry *r=NULL; char *lab;
1190 lab = mystrdup(LATEXT(1));
1194 UsedNewStyleLabel = 1;
1195 if ( UsedOldStyleAttrib ) err("cannot mix with new-style labels with old-style $i");
1196 t = (TermEntry *) hash_get(Tname, lab);
1197 if ( t==NULL ) t = (TermEntry *) hash_get(Texpr, lab);
1198 if ( t==NULL ) r = (RuleEntry *) hash_get(Rname, lab);
1200 err(eMsg1("label definition clashes with token/tokclass definition: '%s'", lab));
1203 else if ( r!=NULL ) {
1204 err(eMsg1("label definition clashes with rule definition: '%s'", lab));
1208 /* we don't clash with anybody else */
1209 l = (LabelEntry *) hash_get(Elabel, lab);
1210 if ( l==NULL ) { /* ok to add new element label */
1211 l = (LabelEntry *)hash_add(Elabel,
1213 (Entry *)newLabelEntry(lab));
1214 /* add to list of element labels for this rule */
1215 list_add(&CurElementLabels, (void *)lab);
1219 err(eMsg1("label definitions must be unique per rule: '%s'", lab));
1223 zzmatch(99); zzCONSUME;
1228 zzsyn(zzMissText, zzBadTok, (ANTLRChar *)"", zzMissSet, zzMissTok, zzErrk, zzBadText);
1229 zzresynch(setwd3, 0x4);
1236 element( int not, int first_on_line, int use_def_MT_handler )
1238 element(not,first_on_line,use_def_MT_handler)
1241 int use_def_MT_handler ;
1245 PURIFY(_retv,sizeof( Node * ))
1252 Predicate *pred = NULL;
1253 int local_use_def_MT_handler=0;
1256 set toksrefd, rulesrefd;
1258 TokNode *p=NULL; RuleRefNode *q; int approx=0;
1259 LabelEntry *label=NULL;
1261 if ( (setwd3[LA(1)]&0x8) ) {
1266 if ( (LA(1)==LABEL) ) {
1267 label = element_label();
1277 if ( (LA(1)==TokenTerm) ) {
1280 term = (TermEntry *) hash_get(Tname, LATEXT(1));
1281 if ( term==NULL && UserDefdTokens ) {
1282 err("implicit token definition not allowed with #tokdefs");
1283 zzaRet.left = zzaRet.right = NULL;
1286 zzaRet = buildToken(LATEXT(1));
1287 p=((TokNode *)((Junction *)zzaRet.left)->p1);
1288 term = (TermEntry *) hash_get(Tname, LATEXT(1));
1289 require( term!= NULL, "hash table mechanism is broken");
1290 p->tclass = term->tclass;
1291 p->complement = not;
1292 if ( label!=NULL ) {
1293 p->el_label = label->str;
1294 label->elem = (Node *)p;
1303 if ( (LA(1)==110) ) {
1304 zzmatch(110); zzCONSUME;
1309 if ( (LA(1)==QuotedTerm) ) {
1310 zzmatch(QuotedTerm);
1311 if ( p!=NULL ) setUpperRange(p, LATEXT(1));
1316 if ( (LA(1)==TokenTerm) ) {
1318 if ( p!=NULL ) setUpperRange(p, LATEXT(1));
1322 else {zzFAIL(1,zzerr6,&zzMissSet,&zzMissText,&zzBadTok,&zzBadText,&zzErrk); goto fail;}
1332 if ( p!=NULL && (p->upper_range!=0 || p->tclass || not) )
1333 list_add(&MetaTokenNodes, (void *)p);
1338 if ( (LA(1)==111) ) {
1340 if ( p!=NULL ) p->astnode=ASTroot;
1345 if ( (setwd3[LA(1)]&0x10) ) {
1346 if ( p!=NULL ) p->astnode=ASTchild;
1349 if ( (LA(1)==96) ) {
1351 if ( p!=NULL ) p->astnode=ASTexclude;
1355 else {zzFAIL(1,zzerr7,&zzMissSet,&zzMissText,&zzBadTok,&zzBadText,&zzErrk); goto fail;}
1365 if ( (LA(1)==84) ) {
1367 local_use_def_MT_handler = 1;
1375 if ( p!=NULL && first_on_line ) {
1376 CurAltStart = (Junction *)zzaRet.left;
1377 p->altstart = CurAltStart;
1380 p->use_def_MT_handler = use_def_MT_handler || local_use_def_MT_handler;
1384 if ( (LA(1)==QuotedTerm) ) {
1385 zzmatch(QuotedTerm);
1387 term = (TermEntry *) hash_get(Texpr, LATEXT(1));
1388 if ( term==NULL && UserDefdTokens ) {
1389 err("implicit token definition not allowed with #tokdefs");
1390 zzaRet.left = zzaRet.right = NULL;
1393 zzaRet = buildToken(LATEXT(1)); p=((TokNode *)((Junction *)zzaRet.left)->p1);
1394 p->complement = not;
1395 if ( label!=NULL ) {
1396 p->el_label = label->str;
1397 label->elem = (Node *)p;
1406 if ( (LA(1)==110) ) {
1407 zzmatch(110); zzCONSUME;
1412 if ( (LA(1)==QuotedTerm) ) {
1413 zzmatch(QuotedTerm);
1414 if ( p!=NULL ) setUpperRange(p, LATEXT(1));
1419 if ( (LA(1)==TokenTerm) ) {
1421 if ( p!=NULL ) setUpperRange(p, LATEXT(1));
1425 else {zzFAIL(1,zzerr8,&zzMissSet,&zzMissText,&zzBadTok,&zzBadText,&zzErrk); goto fail;}
1438 if ( (LA(1)==111) ) {
1440 if ( p!=NULL ) p->astnode=ASTroot;
1445 if ( (setwd3[LA(1)]&0x20) ) {
1446 if ( p!=NULL ) p->astnode=ASTchild;
1449 if ( (LA(1)==96) ) {
1451 if ( p!=NULL ) p->astnode=ASTexclude;
1455 else {zzFAIL(1,zzerr9,&zzMissSet,&zzMissText,&zzBadTok,&zzBadText,&zzErrk); goto fail;}
1465 if ( (LA(1)==84) ) {
1467 local_use_def_MT_handler = 1;
1475 if ( p!=NULL && (p->upper_range!=0 || p->tclass || not) )
1476 list_add(&MetaTokenNodes, (void *)p);
1478 if ( first_on_line ) {
1479 CurAltStart = (Junction *)zzaRet.left;
1480 p->altstart = CurAltStart;
1483 p->use_def_MT_handler = use_def_MT_handler || local_use_def_MT_handler;
1487 if ( (LA(1)==WildCard) ) {
1488 if ( not ) warn("~ WILDCARD is an undefined operation (implies 'nothing')");
1490 zzaRet = buildWildCard(LATEXT(1)); p=((TokNode *)((Junction *)zzaRet.left)->p1);
1497 if ( (LA(1)==111) ) {
1504 if ( (setwd3[LA(1)]&0x40) ) {
1505 p->astnode=ASTchild;
1508 if ( (LA(1)==96) ) {
1510 p->astnode=ASTexclude;
1514 else {zzFAIL(1,zzerr10,&zzMissSet,&zzMissText,&zzBadTok,&zzBadText,&zzErrk); goto fail;}
1520 list_add(&MetaTokenNodes, (void *)p);
1522 if ( first_on_line ) {
1523 CurAltStart = (Junction *)zzaRet.left;
1524 p->altstart = CurAltStart;
1525 if ( label!=NULL ) {
1526 p->el_label = label->str;
1527 label->elem = (Node *)p;
1533 if ( (LA(1)==NonTerminal) ) {
1534 if ( not ) warn("~ NONTERMINAL is an undefined operation");
1535 zzmatch(NonTerminal);
1536 zzaRet = buildRuleRef(LATEXT(1));
1543 if ( (LA(1)==96) ) {
1545 q = (RuleRefNode *) ((Junction *)zzaRet.left)->p1;
1546 q->astnode=ASTexclude;
1557 if ( (setwd3[LA(1)]&0x80) ) {
1562 if ( (LA(1)==97) ) {
1563 zzmatch(97); zzCONSUME;
1568 zzmatch(PassAction);
1569 addParm(((Junction *)zzaRet.left)->p1, LATEXT(1));
1576 rr=(RuleRefNode *) ((Junction *)zzaRet.left)->p1;
1582 if ( (LA(1)==98) ) {
1583 zzmatch(98); zzCONSUME;
1584 zzmatch(PassAction);
1586 a = (char *) calloc(strlen(LATEXT(1))+1, sizeof(char));
1587 require(a!=NULL, "rule element: cannot allocate assignment");
1588 strcpy(a, LATEXT(1));
1597 if ( label!=NULL ) {
1598 rr->el_label = label->str;
1599 label->elem = (Node *)rr;
1601 if ( first_on_line ) {
1602 CurAltStart = (Junction *)zzaRet.left;
1603 ((RuleRefNode *)((Junction *)zzaRet.left)->p1)->altstart = CurAltStart;
1607 else {zzFAIL(1,zzerr11,&zzMissSet,&zzMissText,&zzBadTok,&zzBadText,&zzErrk); goto fail;}
1616 if ( (LA(1)==Action) ) {
1617 if ( not ) warn("~ ACTION is an undefined operation");
1619 zzaArg(zztasp1,0) = buildAction(LATEXT(1),action_file,action_line, 0);
1622 if ( first_on_line ) CurAltStart = (Junction *)zzaArg(zztasp1,0 ).left;
1623 _retv = (Node *) ((Junction *)zzaArg(zztasp1,0 ).left)->p1;
1626 if ( (LA(1)==Pred) ) {
1627 if ( not ) warn("~ SEMANTIC-PREDICATE is an undefined operation");
1629 zzaArg(zztasp1,0) = buildAction(LATEXT(1),action_file,action_line, 1);
1632 act = (ActionNode *) ((Junction *)zzaArg(zztasp1,0 ).left)->p1;
1638 if ( (LA(1)==PassAction) ) {
1639 zzmatch(PassAction);
1641 a = (char *) calloc(strlen(LATEXT(1))+1, sizeof(char));
1642 require(a!=NULL, "rule element: cannot allocate predicate fail action");
1643 strcpy(a, LATEXT(1));
1651 if ( first_on_line ) CurAltStart = (Junction *)zzaArg(zztasp1,0 ).left;
1652 _retv = (Node *)act;
1655 if ( (setwd4[LA(1)]&0x1) ) {
1656 if ( not ) warn("~ BLOCK is an undefined operation");
1662 if ( (LA(1)==112) ) {
1663 zzmatch(112); zzCONSUME;
1668 if ( (LA(1)==113) ) {
1675 if ( (LA(1)==114) ) {
1682 if ( (LA(1)==115) ) {
1688 else {zzFAIL(1,zzerr12,&zzMissSet,&zzMissText,&zzBadTok,&zzBadText,&zzErrk); goto fail;}
1702 if ( (LA(1)==116) ) {
1703 zzmatch(116); zzCONSUME;
1704 block( &toksrefd,&rulesrefd );
1706 blk = zzaRet = zzaArg(zztasp2,2); --BlkLevel;
1713 if ( (LA(1)==118) ) {
1715 zzaRet = makeLoop(zzaRet,approx);
1720 if ( (LA(1)==119) ) {
1722 zzaRet = makePlus(zzaRet,approx);
1727 if ( (LA(1)==120) ) {
1728 zzmatch(120); zzCONSUME;
1733 if ( (LA(1)==121) ) {
1734 zzmatch(121); zzCONSUME;
1736 zzaRet = buildAction(LATEXT(1),action_file,action_line,1);
1739 act = (ActionNode *) ((Junction *)zzaRet.left)->p1;
1745 if ( (LA(1)==PassAction) ) {
1746 zzmatch(PassAction);
1748 a = (char *)calloc(strlen(LATEXT(1))+1, sizeof(char));
1749 require(a!=NULL, "rule element: cannot allocate predicate fail action");
1750 strcpy(a, LATEXT(1));
1758 if ( first_on_line) CurAltStart=(Junction *)zzaRet.left;
1759 _retv = (Node *)act;
1761 pred = computePredicateFromContextGuard(blk);
1763 err("invalid or missing context guard");
1765 pred->expr = act->action;
1767 act->guardpred = pred;
1771 if ( (setwd4[LA(1)]&0x2) ) {
1772 zzaRet = makeBlk(zzaRet,approx);
1774 ((Junction *) ((Junction *)zzaRet.left)->p1)->guess=1;
1775 if ( ! first_on_line ) {
1776 err("(...)? predicate must be first element of production");
1779 else {zzFAIL(1,zzerr13,&zzMissSet,&zzMissText,&zzBadTok,&zzBadText,&zzErrk); goto fail;}
1786 if ( (setwd4[LA(1)]&0x4) ) {
1787 zzaRet = makeBlk(zzaRet,approx);
1789 else {zzFAIL(1,zzerr14,&zzMissSet,&zzMissText,&zzBadTok,&zzBadText,&zzErrk); goto fail;}
1798 ((Junction *)((Junction *)zzaRet.left)->p1)->blockid = CurBlockID;
1799 ((Junction *)((Junction *)zzaRet.left)->p1)->tokrefs = toksrefd;
1800 ((Junction *)((Junction *)zzaRet.left)->p1)->rulerefs = rulesrefd;
1801 if ( first_on_line )
1802 CurAltStart = (Junction *)((Junction *)((Junction *)zzaRet.left)->p1);
1803 _retv = (Node *) ((Junction *)zzaRet.left)->p1;
1807 if ( (LA(1)==95) ) {
1808 zzmatch(95); zzCONSUME;
1809 block( &toksrefd,&rulesrefd );
1810 zzaRet = makeOpt(zzaArg(zztasp2,2),approx); --BlkLevel;
1813 ((Junction *)((Junction *)zzaRet.left)->p1)->blockid = CurBlockID;
1814 ((Junction *)((Junction *)zzaRet.left)->p1)->tokrefs = toksrefd;
1815 ((Junction *)((Junction *)zzaRet.left)->p1)->rulerefs = rulesrefd;
1818 if ( first_on_line )
1819 CurAltStart = (Junction *) ((Junction *)((Junction *)zzaRet.left)->p1);
1820 _retv = (Node *) ((Junction *)zzaRet.left)->p1;
1822 else {zzFAIL(1,zzerr15,&zzMissSet,&zzMissText,&zzBadTok,&zzBadText,&zzErrk); goto fail;}
1829 if ( (LA(1)==118) ) {
1831 warn("don't you want a ')' with that '*'?"); CannotContinue=TRUE;
1836 if ( (LA(1)==119) ) {
1838 warn("don't you want a ')' with that '+'?"); CannotContinue=TRUE;
1843 if ( (LA(1)==98) ) {
1845 warn("'>' can only appear after a nonterminal"); CannotContinue=TRUE;
1850 if ( (LA(1)==PassAction) ) {
1851 zzmatch(PassAction);
1852 warn("[...] out of context 'rule > [...]'");
1853 CannotContinue=TRUE;
1857 else {zzFAIL(1,zzerr16,&zzMissSet,&zzMissText,&zzBadTok,&zzBadText,&zzErrk); goto fail;}
1869 CannotContinue=TRUE;
1870 zzsyn(zzMissText, zzBadTok, (ANTLRChar *)"", zzMissSet, zzMissTok, zzErrk, zzBadText);
1871 zzresynch(setwd4, 0x8);
1878 default_exception_handler(void)
1880 default_exception_handler()
1887 DefaultExGroup = exception_group();
1893 zzsyn(zzMissText, zzBadTok, (ANTLRChar *)"", zzMissSet, zzMissTok, zzErrk, zzBadText);
1894 zzresynch(setwd4, 0x10);
1900 exception_group(void)
1905 ExceptionGroup * _retv;
1906 PURIFY(_retv,sizeof( ExceptionGroup * ))
1911 ExceptionHandler *h; LabelEntry *label=NULL; FoundException = 1;
1913 _retv = (ExceptionGroup *)calloc(1, sizeof(ExceptionGroup));
1921 if ( (LA(1)==PassAction) ) {
1922 zzmatch(PassAction);
1925 p[strlen(p)-1] = '\0'; /* kill trailing space */
1926 label = (LabelEntry *) hash_get(Elabel, LATEXT(1)+1);
1929 err(eMsg1("unknown label in exception handler: '%s'", LATEXT(1)+1));
1941 while ( (LA(1)==124) ) {
1942 h = exception_handler();
1944 list_add(&(_retv->handlers), (void *)h);
1954 if ( (LA(1)==123) ) {
1955 zzmatch(123); zzCONSUME;
1956 zzmatch(99); zzCONSUME;
1959 ExceptionHandler *eh = (ExceptionHandler *)
1960 calloc(1, sizeof(ExceptionHandler));
1961 char *a = (char *) calloc(strlen(LATEXT(1))+1, sizeof(char));
1962 require(eh!=NULL, "exception: cannot allocate handler");
1963 require(a!=NULL, "exception: cannot allocate action");
1964 strcpy(a, LATEXT(1));
1966 eh->signalname = (char *) calloc(strlen("default")+1, sizeof(char));
1967 require(eh->signalname!=NULL, "exception: cannot allocate sig name");
1968 strcpy(eh->signalname, "default");
1969 list_add(&(_retv->handlers), (void *)eh);
1978 if ( label!=NULL ) {
1979 /* Record ex group in sym tab for this label */
1980 if ( label->ex_group!=NULL ) {
1981 err(eMsg1("duplicate exception handler for label '%s'",label->str));
1984 label->ex_group = _retv;
1985 /* Label the exception group itself */
1986 _retv->label = label->str;
1987 /* Make the labelled element pt to the exception also */
1988 switch ( label->elem->ntype ) {
1991 RuleRefNode *r = (RuleRefNode *)label->elem;
1992 r->ex_group = _retv;
1997 TokNode *t = (TokNode *)label->elem;
1998 t->ex_group = _retv;
2003 /* You may want to remove this exc from the rule list
2004 * and handle at the labeled element site.
2012 CannotContinue=TRUE;
2013 zzsyn(zzMissText, zzBadTok, (ANTLRChar *)"", zzMissSet, zzMissTok, zzErrk, zzBadText);
2014 zzresynch(setwd4, 0x20);
2021 exception_handler(void)
2026 ExceptionHandler * _retv;
2027 PURIFY(_retv,sizeof( ExceptionHandler * ))
2035 _retv = (ExceptionHandler *)calloc(1, sizeof(ExceptionHandler));
2036 require(_retv!=NULL, "exception: cannot allocate handler");
2043 if ( (LA(1)==NonTerminal) ) {
2044 zzmatch(NonTerminal);
2046 _retv->signalname = (char *) calloc(strlen(LATEXT(1))+1, sizeof(char));
2047 require(_retv->signalname!=NULL, "exception: cannot allocate sig name");
2048 strcpy(_retv->signalname, LATEXT(1));
2053 if ( (LA(1)==TokenTerm) ) {
2056 _retv->signalname = (char *) calloc(strlen(LATEXT(1))+1, sizeof(char));
2057 require(_retv->signalname!=NULL, "exception: cannot allocate sig name");
2058 strcpy(_retv->signalname, LATEXT(1));
2062 else {zzFAIL(1,zzerr17,&zzMissSet,&zzMissText,&zzBadTok,&zzBadText,&zzErrk); goto fail;}
2067 zzmatch(99); zzCONSUME;
2072 _retv->action = NULL;
2073 if ( (LA(1)==Action) ) {
2076 _retv->action = (char *) calloc(strlen(LATEXT(1))+1, sizeof(char));
2077 require(_retv->action!=NULL, "exception: cannot allocate action");
2078 strcpy(_retv->action, LATEXT(1));
2089 CannotContinue=TRUE;
2090 zzsyn(zzMissText, zzBadTok, (ANTLRChar *)"", zzMissSet, zzMissTok, zzErrk, zzBadText);
2091 zzresynch(setwd4, 0x40);
2098 enum_file( char *fname )
2108 if ( (setwd4[LA(1)]&0x80) ) {
2113 if ( (LA(1)==132) ) {
2114 zzmatch(132); zzCONSUME;
2115 zzmatch(ID); zzCONSUME;
2120 if ( (LA(1)==138) ) {
2121 zzmatch(138); zzCONSUME;
2122 zzmatch(ID); zzCONSUME;
2135 if ( (LA(1)==140) ) {
2144 } while ( (LA(1)==140) );
2150 if ( (LA(1)==138) ) {
2153 else {zzFAIL(1,zzerr18,&zzMissSet,&zzMissText,&zzBadTok,&zzBadText,&zzErrk); goto fail;}
2160 if ( (LA(1)==Eof) ) {
2162 else {zzFAIL(1,zzerr19,&zzMissSet,&zzMissText,&zzBadTok,&zzBadText,&zzErrk); goto fail;}
2168 zzsyn(zzMissText, zzBadTok, (ANTLRChar *)"", zzMissSet, zzMissTok, zzErrk, zzBadText);
2169 zzresynch(setwd5, 0x1);
2175 defines( char *fname )
2185 int v,maxt= -1; char *t;
2192 zzmatch(138); zzCONSUME;
2194 t = mystrdup(LATEXT(1));
2199 v = atoi(LATEXT(1));
2200 /* fprintf(stderr, "#token %s=%d\n", t, v);*/
2202 if ( v>maxt ) maxt=v;
2203 if ( Tnum( t ) == 0 ) addForcedTname( t, v );
2205 warnFL(eMsg1("redefinition of token %s; ignored",t), fname,zzline);
2210 } while ( (LA(1)==138) );
2214 TokenNum = maxt + 1;
2219 zzsyn(zzMissText, zzBadTok, (ANTLRChar *)"", zzMissSet, zzMissTok, zzErrk, zzBadText);
2220 zzresynch(setwd5, 0x2);
2226 enum_def( char *fname )
2237 zzmatch(140); zzCONSUME;
2238 zzmatch(ID); zzCONSUME;
2239 zzmatch(141); zzCONSUME;
2241 t = mystrdup(LATEXT(1));
2248 if ( (LA(1)==142) ) {
2249 zzmatch(142); zzCONSUME;
2256 if ( (setwd5[LA(1)]&0x4) ) {
2259 else {zzFAIL(1,zzerr20,&zzMissSet,&zzMissText,&zzBadTok,&zzBadText,&zzErrk); goto fail;}
2265 /* fprintf(stderr, "#token %s=%d\n", t, v);*/
2267 if ( Tnum( t ) == 0 ) addForcedTname( t, v );
2269 warnFL(eMsg1("redefinition of token %s; ignored",t), fname,zzline);
2275 while ( (LA(1)==143) ) {
2276 zzmatch(143); zzCONSUME;
2281 if ( (LA(1)==ID) ) {
2283 t = mystrdup(LATEXT(1));
2290 if ( (LA(1)==142) ) {
2291 zzmatch(142); zzCONSUME;
2298 if ( (setwd5[LA(1)]&0x8) ) {
2301 else {zzFAIL(1,zzerr21,&zzMissSet,&zzMissText,&zzBadTok,&zzBadText,&zzErrk); goto fail;}
2307 /* fprintf(stderr, "#token %s=%d\n", t, v);*/
2309 if ( Tnum( t ) == 0 ) addForcedTname( t, v );
2311 warnFL(eMsg1("redefinition of token %s; ignored",t), fname,zzline);
2322 zzmatch(144); zzCONSUME;
2331 zzsyn(zzMissText, zzBadTok, (ANTLRChar *)"", zzMissSet, zzMissTok, zzErrk, zzBadText);
2332 zzresynch(setwd5, 0x10);
2336 /* semantics of #token */
2339 chkToken(char *t, char *e, char *a, int tnum)
2341 chkToken(t,e,a,tnum)
2348 /* check to see that they don't try to redefine a token as a token class */
2350 p = (TermEntry *) hash_get(Tname, t);
2351 if ( p!=NULL && p->classname ) {
2352 err(eMsg1("redefinition of #tokclass '%s' to #token not allowed; ignored",t));
2353 if ( a!=NULL ) free((char *)a);
2358 if ( t==NULL && e==NULL ) { /* none found */
2359 err("#token requires at least token name or rexpr");
2361 else if ( t!=NULL && e!=NULL ) { /* both found */
2362 if ( UserDefdTokens ) { /* if #tokdefs, must not define new */
2363 p = (TermEntry *) hash_get(Tname, t);
2365 err(eMsg1("#token definition '%s' not allowed with #tokdefs; ignored",t));
2371 if ( hasAction(e) ) {
2372 err(eMsg1("redefinition of action for %s; ignored",e));
2374 else setHasAction(e, a);
2377 else if ( t!=NULL ) { /* only one found */
2378 if ( UserDefdTokens ) {
2379 err(eMsg1("#token definition '%s' not allowed with #tokdefs; ignored",t));
2382 if ( Tnum( t ) == 0 ) addTname( t );
2384 err(eMsg1("redefinition of token %s; ignored",t));
2387 err(eMsg1("action cannot be attached to a token name (%s); ignored",t));
2391 else if ( e!=NULL ) {
2392 if ( Tnum( e ) == 0 ) addTexpr( e );
2394 if ( hasAction(e) ) {
2395 err(eMsg1("redefinition of action for expr %s; ignored",e));
2397 else if ( a==NULL ) {
2398 err(eMsg1("redefinition of expr %s; ignored",e));
2401 if ( a!=NULL ) setHasAction(e, a);
2404 /* if a token type number was specified, then add the token ID and 'tnum'
2405 * pair to the ForcedTokens list. (only applies if an id was given)
2407 if ( t!=NULL && tnum>0 )
2409 if ( set_el(tnum, reserved_positions) )
2411 err(eMsgd("a token has already been forced to token number %d; ignored", tnum));
2415 list_add(&ForcedTokens, newForcedToken(t,tnum));
2416 set_orel(tnum, &reserved_positions);
2423 match_token(char *s, char **nxt)
2430 if ( !(*s>='A' && *s<='Z') ) return 0;
2432 while ( (*s>='a' && *s<='z') ||
2433 (*s>='A' && *s<='Z') ||
2434 (*s>='0' && *s<='9') ||
2439 if ( *s!=' ' && *s!='}' ) return 0;
2446 match_rexpr(char *s, char **nxt)
2453 if ( *s!='"' ) return 0;
2458 warn("eoln found in regular expression");
2459 if ( *s=='\\' ) s++;
2467 * Walk a string "{ A .. Z }" where A..Z is a space separated list
2468 * of token references (either labels or reg exprs). Return a
2469 * string "inlineX_set" for some unique integer X. Basically,
2470 * we pretend as if we had seen "#tokclass inlineX { A .. Z }"
2471 * on the input stream outside of an action.
2482 fprintf(stderr, "found consumeUntil( {...} )\n");
2483 while ( *s==' ' || *s=='\t' || *s=='\n' || *s=='\r' ) {s++;}
2486 err("malformed consumeUntil( {...} ); missing '{'");
2490 while ( *s==' ' || *s=='\t' || *s=='\n' || *s=='\r' ) {s++;}
2493 if ( match_token(s,&nxt) ) fprintf(stderr, "found token %s\n", s);
2494 else if ( match_rexpr(s,&nxt) ) fprintf(stderr, "found rexpr %s\n", s);
2496 err("invalid element in consumeUntil( {...} )");
2500 while ( *s==' ' || *s=='\t' || *s=='\n' || *s=='\r' ) {s++;}
2502 return "inlineX_set";
2505 /* ANTLR-specific syntax error message generator
2506 * (define USER_ZZSYN when compiling so don't get 2 definitions)
2510 zzsyn(char *text, int tok, char *egroup, SetWordType *eset, int etok,
2511 int k, char *bad_text)
2513 zzsyn(text, tok, egroup, eset, etok, k, bad_text)
2514 char *text, *egroup, *bad_text;
2521 fprintf(stderr, ErrHdr, FileStr[CurFile]!=NULL?FileStr[CurFile]:"stdin", zzline);
2522 fprintf(stderr, " syntax error at \"%s\"", (tok==zzEOF_TOKEN)?"EOF":text);
2523 if ( !etok && !eset ) {fprintf(stderr, "\n"); return;}
2524 if ( k==1 ) fprintf(stderr, " missing");
2527 fprintf(stderr, "; \"%s\" not", bad_text);
2528 if ( zzset_deg(eset)>1 ) fprintf(stderr, " in");
2530 if ( zzset_deg(eset)>0 ) zzedecode(eset);
2531 else fprintf(stderr, " %s", zztokens[etok]);
2532 if ( strlen(egroup) > (size_t)0 ) fprintf(stderr, " in %s", egroup);
2533 fprintf(stderr, "\n");