1 /* LLgen generated code from source LLgen.g */
3 #define LL_LEXI scanner
5 #if __STDC__ || __cplusplus
8 #define LL_LEXI scanner
9 /* $Id: incl,v 2.13 1997/02/21 15:44:09 ceriel Exp $ */
13 #define LL_assert(x) assert(x)
15 #define LL_assert(x) /* nothing */
20 #define LL_SAFE(x) /* Nothing */
21 #define LL_SSCANDONE(x) if (LLsymb != x) LLsafeerror(x)
22 #define LL_SCANDONE(x) if (LLsymb != x) LLerror(x)
23 #define LL_NOSCANDONE(x) LLscan(x)
25 #define LLscan(x) if ((LLsymb = LL_LEXI()) != x) LLerror(x)
28 extern unsigned int LLscnt[];
29 extern unsigned int LLtcnt[];
33 extern int LLstartsymb;
36 #define LLsdecr(d) {LL_assert(LLscnt[d] > 0); LLscnt[d]--;}
37 #define LLtdecr(d) {LL_assert(LLtcnt[d] > 0); LLtcnt[d]--;}
38 #define LLsincr(d) LLscnt[d]++
39 #define LLtincr(d) LLtcnt[d]++
42 extern int LL_LEXI(void);
43 extern void LLread(void);
44 extern int LLskip(void);
45 extern int LLnext(int);
46 extern void LLerror(int);
47 extern void LLsafeerror(int);
48 extern void LLnewlevel(unsigned int *);
49 extern void LLoldlevel(unsigned int *);
51 extern void LLscan(int);
54 extern int LLfirst(int, int);
57 extern void LLnc_recover(void);
59 #else /* not LL_ANSI_C */
74 extern LLnc_recover();
76 #endif /* not LL_ANSI_C */
86 static string rcsid = "$Id: LLgen.g,v 2.28 1997/02/21 11:27:38 ceriel Exp $";
88 p_mem alloc(), ralloc();
93 static int acount; /* count #of global actions */
96 static p_gram alt_table;
101 static p_gram rule_table;
103 static int max_rules;
106 /* Here are defined : */
111 STATIC p_gram copyrule();
112 /* and of course LLparse() */
119 nonterms[porder].n_next = index;
123 nonterms[porder].n_next = -1;
131 tokens[porder].t_next = index;
135 tokens[porder].t_next = -1;
140 alt_table = (p_gram )alloc(ALTINCR*sizeof(t_gram));
143 rule_table = (p_gram )alloc(RULEINCR*sizeof(t_gram));
145 max_rules = RULEINCR;
149 static void LL1_def(void);
150 static void LL2_rule(void);
151 static void LL3_listel(void);
152 static void LL4_firsts(void);
153 static void LL5_productions(
156 static void LL6_simpleproduction(
158 p_gram *p ,register int *conflres) ;
159 static void LL7_elem(
161 register p_gram pres) ;
162 static void LL8_repeats(
164 int *kind ,int *cnt) ;
165 static void LL9_number(
173 static LL5_productions();
174 static LL6_simpleproduction();
176 static LL8_repeats();
189 { acount = 0; p_init(); }
192 L_1 : {switch(LLcsymb) {
193 case /* EOFILE */ 0 : ;
195 default:{int LL_1=LLnext(0);
199 else if (LL_1 & 1) goto L_1;}
200 case /* C_IDENT */ 2 : ;
201 case /* C_ACTION */ 7 : ;
202 case /* C_TOKEN */ 8 : ;
203 case /* C_START */ 9 : ;
204 case /* C_FIRST */ 13 : ;
205 case /* C_LEXICAL */ 14 : ;
206 case /* C_PREFIX */ 15 : ;
207 case /* C_ONERROR */ 16 : ;
218 * Put an endmarker in temporary file
222 free((p_mem) rule_table);
223 free((p_mem) alt_table);
238 case /* C_IDENT */ 2 : ;
241 case /* C_TOKEN */ 8 : ;
249 L_4 : {switch(LLcsymb) {
250 case /* ';' */ 24 : ;
252 default:{int LL_2=LLnext(44);
256 else if (LL_2 & 1) goto L_4;}
257 case /* ',' */ 23 : ;
270 case /* C_START */ 9 : ;
275 LL_NOSCANDONE(C_IDENT);
277 { p = store(lextoken.t_string); }
281 LL_NOSCANDONE(C_IDENT);
284 * Put the declaration in the list
287 register p_gram temp;
290 temp = search(NONTERM,lextoken.t_string,BOTH);
291 ff = (p_start) alloc(sizeof(t_start));
292 ff->ff_nont = g_getcont(temp);
296 while (ff = ff->ff_next) {
297 if (! strcmp(p, ff->ff_name)) {
298 error(linecount, "\"%s\" already used in a %%start", p);
306 case /* C_LEXICAL */ 14 : ;
309 LL_NOSCANDONE(C_IDENT);
312 lexical = store(lextoken.t_string);
314 else error(linecount,"Duplicate %%lexical");
319 case /* C_PREFIX */ 15 : ;
322 LL_NOSCANDONE(C_IDENT);
325 prefix = store(lextoken.t_string);
326 if (strlen(prefix) > 6) {
328 "%%prefix too long");
332 else error(linecount,"Duplicate %%prefix");
337 case /* C_ONERROR */ 16 : ;
340 LL_NOSCANDONE(C_IDENT);
343 #ifdef NON_CORRECTING
345 warning(linecount, "%%onerror conflicts with -n option");
350 onerror = store(lextoken.t_string);
352 else error(linecount,"Duplicate %%onerror");
358 LL_SSCANDONE(C_ACTION);
362 case /* C_FIRST */ 13 : ;
376 LL_NOSCANDONE(C_IDENT);
378 { p_gram temp = search(TERMINAL,lextoken.t_string,ENTERING);
379 newtorder(g_getcont(temp));
380 tokens[g_getcont(temp)].t_lineno = linecount;
395 register p_gram temp;
404 { temp = search(NONTERM,lextoken.t_string,BOTH);
405 p = &nonterms[g_getcont(temp)];
408 "Nonterminal %s already defined", lextoken.t_string);
411 * Remember the order in which the nonterminals
412 * were defined. Code must be generated in that
413 * order to keep track with the actions on the
416 newnorder(p - nonterms);
419 p->n_lineno = linecount;
420 p->n_off = ftell(fact);
424 L_1 : {switch(LLcsymb) {
425 case /* C_ACTION */ 7 : ;
426 case /* ':' */ 25 : ;
429 default:{int LL_3=LLnext(262);
434 else if (LL_3 & 1) goto L_1;}
435 case /* C_PARAMS */ 6 : ;
439 { if (lextoken.t_num > 0) {
440 p->n_flags |= PARAMS;
441 if (lextoken.t_num > 15) {
442 error(linecount,"Too many parameters");
444 else setntparams(p,lextoken.t_num);
451 L_2 : {switch(LLcsymb) {
452 case /* ':' */ 25 : ;
455 default:{int LL_4=LLnext(263);
460 else if (LL_4 & 1) goto L_2;}
461 case /* C_ACTION */ 7 : ;
465 { p->n_flags |= LOCALS; }
472 { in_production = 1; }
481 { in_production = 0; }
483 { nonterms[g_getcont(temp)].n_rule = rr;}
508 { o_lc = linecount; }
509 LL6_simpleproduction(
513 { if (conflres & DEF) haddefault = 1; }
514 goto L_2; /* so that the label is used for certain */
517 case /* '|' */ 26 : ;
525 { n_lc = linecount; }
527 LL6_simpleproduction(
531 { if (n_alts >= max_alts-2) {
532 alt_table = (p_gram ) ralloc(
534 (unsigned)(max_alts+=ALTINCR)*sizeof(t_gram));
539 "More than one %%default in alternation");
543 mkalt(*p,conflres,o_lc,&alt_table[n_alts++]);
551 L_4 : {switch(LLcsymb) {
552 case /* ';' */ 24 : ;
553 case /* ']' */ 28 : ;
555 default:{int LL_5=LLnext(124);
559 else if (LL_5 & 1) goto L_4;}
560 case /* '|' */ 26 : ;
568 { if (conflres & (COND|PREFERING|AVOIDING)) {
570 "Resolver on last alternative not allowed");
572 mkalt(*p,conflres,n_lc,&alt_table[n_alts++]);
574 g_settype((&alt_table[n_alts]),EORULE);
575 *p = copyrule(&alt_table[n_alts-altcnt],altcnt+1);
578 case /* ';' */ 24 : ;
579 case /* ']' */ 28 : ;
584 { if (conflres & (COND|PREFERING|AVOIDING)) {
586 "No alternation conflict resolver allowed here");
589 if (conflres & DEF) {
591 "No %%default allowed here");
596 default: if (LLskip()) goto L_2;
600 { n_alts -= altcnt; }
606 mkalt(prod,condition,lc,res) p_gram prod; register p_gram res; {
608 * Create an alternation and initialise it.
616 list = (p_link) alloc(50 * sizeof(t_link));
621 l->l_flag = condition;
623 g_settype(res,ALTERNATION);
631 LL6_simpleproduction(
634 p_gram *p ,register int *conflres)
637 p,conflres) p_gram *p; register int *conflres;
651 L_1 : {switch(LLcsymb) {
652 case /* C_IDENT */ 2 : ;
653 case /* C_LITERAL */ 4 : ;
654 case /* C_ACTION */ 7 : ;
655 case /* C_IF */ 10 : ;
656 case /* C_AVOID */ 17 : ;
657 case /* C_PREFER */ 18 : ;
658 case /* C_SUBSTART */ 20 : ;
659 case /* C_ERRONEOUS */ 21 : ;
660 case /* C_ILLEGAL */ 22 : ;
661 case /* ';' */ 24 : ;
662 case /* '|' */ 26 : ;
663 case /* '[' */ 27 : ;
664 case /* ']' */ 28 : ;
667 default:{int LL_6=LLnext(275);
672 else if (LL_6 & 1) goto L_1;}
673 case /* C_DEFAULT */ 19 : ;
677 { *conflres |= DEF; }
682 L_2 : {switch(LLcsymb) {
683 case /* C_IDENT */ 2 : ;
684 case /* C_LITERAL */ 4 : ;
685 case /* C_ACTION */ 7 : ;
686 case /* C_SUBSTART */ 20 : ;
687 case /* C_ERRONEOUS */ 21 : ;
688 case /* C_ILLEGAL */ 22 : ;
689 case /* ';' */ 24 : ;
690 case /* '|' */ 26 : ;
691 case /* '[' */ 27 : ;
692 case /* ']' */ 28 : ;
695 default:{int LL_7=LLnext(-8);
700 else if (LL_7 & 1) goto L_2;}
701 case /* C_IF */ 10 : ;
702 case /* C_AVOID */ 17 : ;
703 case /* C_PREFER */ 18 : ;
706 case /* C_IF */ 10 : ;
708 LL_NOSCANDONE(C_EXPR);
710 { *conflres |= COND; }
715 { *conflres |= PREFERING; }
717 case /* C_AVOID */ 17 : ;
720 { *conflres |= AVOIDING; }
727 L_6 : {switch(LLcsymb) {
728 case /* C_IDENT */ 2 : ;
729 case /* C_LITERAL */ 4 : ;
730 case /* C_ACTION */ 7 : ;
731 case /* C_SUBSTART */ 20 : ;
732 case /* C_ERRONEOUS */ 21 : ;
733 case /* ';' */ 24 : ;
734 case /* '|' */ 26 : ;
735 case /* '[' */ 27 : ;
736 case /* ']' */ 28 : ;
739 default:{int LL_8=LLnext(278);
744 else if (LL_8 & 1) goto L_6;}
745 case /* C_ILLEGAL */ 22 : ;
750 #ifdef NON_CORRECTING
751 if (n_rules >= max_rules-2) {
752 rule_table = (p_gram) ralloc(
754 (unsigned)(max_rules+=RULEINCR)*sizeof(t_gram));
757 rule_table[n_rules++] =
758 *search(TERMINAL, "LLILLEGAL", BOTH);
759 if (*conflres & DEF) {
760 error(linecount, "%%illegal not allowed in %%default rule");
769 L_7 : {switch(LLcsymb) {
770 case /* ';' */ 24 : ;
771 case /* '|' */ 26 : ;
772 case /* ']' */ 28 : ;
774 default:{int LL_9=LLnext(-12);
778 else if (LL_9 & 1) goto L_7;}
779 case /* C_IDENT */ 2 : ;
780 case /* C_NUMBER */ 3 : ;
781 case /* C_LITERAL */ 4 : ;
782 case /* C_ACTION */ 7 : ;
783 case /* C_SUBSTART */ 20 : ;
784 case /* C_ERRONEOUS */ 21 : ;
785 case /* '[' */ 27 : ;
786 case /* '?' */ 29 : ;
787 case /* '*' */ 30 : ;
788 case /* '+' */ 31 : ;
794 { if (n_rules >= max_rules-2) {
795 rule_table = (p_gram) ralloc(
797 (unsigned)(max_rules+=RULEINCR)*sizeof(t_gram));
802 goto L_9; /* so that the label is used for certain */
805 case /* C_NUMBER */ 3 : ;
806 case /* '?' */ 29 : ;
807 case /* '*' */ 30 : ;
808 case /* '+' */ 31 : ;
814 { if (g_gettype(&elem) != TERM) {
815 rule_table[n_rules] = elem;
816 g_settype((&rule_table[n_rules+1]),EORULE);
817 mkterm(copyrule(&rule_table[n_rules],2),
824 case /* C_IDENT */ 2 : ;
825 case /* C_LITERAL */ 4 : ;
826 case /* C_ACTION */ 7 : ;
827 case /* C_SUBSTART */ 20 : ;
828 case /* C_ERRONEOUS */ 21 : ;
829 case /* ';' */ 24 : ;
830 case /* '|' */ 26 : ;
831 case /* '[' */ 27 : ;
832 case /* ']' */ 28 : ;
837 { if (g_gettype(&elem) == TERM) {
838 register p_term q = g_getterm(&elem);
840 if (! (q->t_flags & RESOLVER) &&
841 g_gettype(q->t_rule) != ALTERNATION &&
842 g_gettype(q->t_rule) != EORULE) {
843 while (g_gettype(q->t_rule) != EORULE) {
844 rule_table[n_rules++] = *q->t_rule++;
846 if (n_rules >= max_rules-2) {
847 rule_table = (p_gram) ralloc(
849 (unsigned)(max_rules+=RULEINCR)*sizeof(t_gram));
852 elem = *--(q->t_rule);
855 if (q == t_list - 1) {
865 default: if (LLskip()) goto L_9;
869 { if (!termdeleted && g_gettype(&elem) == TERM) {
872 q = g_getterm(&elem);
875 if ((q->t_flags & RESOLVER) &&
876 (kind == PLUS || kind == FIXED)) {
878 "%%while not allowed in this term");
881 * A persistent fixed term is the same
882 * as a non-persistent fixed term.
883 * Should we complain?
884 if ((q->t_flags & PERSISTENT) &&
887 "Illegal %%persistent");
893 rule_table[n_rules++] = elem;
904 g_settype((&rule_table[n_rules]),EORULE);
907 if (g_gettype(&rule_table[n_rules]) == TERM &&
909 q = g_getterm(&rule_table[n_rules]);
910 if (r_getkind(q) == FIXED &&
915 if (!*p) *p = copyrule(&rule_table[n_rules],
923 mkterm(prod,flags,lc,result) p_gram prod; register p_gram result; {
925 * Create a term, initialise it and return
926 * a grammar element containing it
932 t_list = (p_term) alloc(50 * sizeof(t_term));
939 g_settype(result,TERM);
941 result->g_lineno = lc;
951 register p_gram pres)
954 pres) register p_gram pres;
962 #ifdef NON_CORRECTING
967 case /* '[' */ 27 : ;
977 L_4 : {switch(LLcsymb) {
978 case /* C_IDENT */ 2 : ;
979 case /* C_LITERAL */ 4 : ;
980 case /* C_ACTION */ 7 : ;
981 case /* C_IF */ 10 : ;
982 case /* C_PERSISTENT */ 12 : ;
983 case /* C_AVOID */ 17 : ;
984 case /* C_PREFER */ 18 : ;
985 case /* C_DEFAULT */ 19 : ;
986 case /* C_SUBSTART */ 20 : ;
987 case /* C_ERRONEOUS */ 21 : ;
988 case /* C_ILLEGAL */ 22 : ;
989 case /* '|' */ 26 : ;
990 case /* '[' */ 27 : ;
991 case /* ']' */ 28 : ;
994 default:{int LL_10=LLnext(267);
999 else if (LL_10 & 1) goto L_4;}
1000 case /* C_WHILE */ 11 : ;
1003 LL_NOSCANDONE(C_EXPR);
1004 # line 488 "LLgen.g"
1010 L_5 : {switch(LLcsymb) {
1011 case /* C_IDENT */ 2 : ;
1012 case /* C_LITERAL */ 4 : ;
1013 case /* C_ACTION */ 7 : ;
1014 case /* C_IF */ 10 : ;
1015 case /* C_AVOID */ 17 : ;
1016 case /* C_PREFER */ 18 : ;
1017 case /* C_DEFAULT */ 19 : ;
1018 case /* C_SUBSTART */ 20 : ;
1019 case /* C_ERRONEOUS */ 21 : ;
1020 case /* C_ILLEGAL */ 22 : ;
1021 case /* '|' */ 26 : ;
1022 case /* '[' */ 27 : ;
1023 case /* ']' */ 28 : ;
1026 default:{int LL_11=LLnext(268);
1031 else if (LL_11 & 1) goto L_5;}
1032 case /* C_PERSISTENT */ 12 : ;
1034 LL_SAFE(C_PERSISTENT);
1035 # line 490 "LLgen.g"
1036 { t |= PERSISTENT; }
1042 # line 492 "LLgen.g"
1046 # line 493 "LLgen.g"
1048 mkterm(p1,t,ln,pres);
1052 case /* C_IDENT */ 2 : ;
1053 case /* C_LITERAL */ 4 : ;
1054 case /* C_ERRONEOUS */ 21 : ;
1057 L_6 : {switch(LLcsymb) {
1060 case /* C_ERRONEOUS */ 21 : ;
1061 LL_SAFE(C_ERRONEOUS);
1062 # line 497 "LLgen.g"
1064 #ifdef NON_CORRECTING
1071 goto L_8; /* so that the label is used for certain */
1074 case /* C_IDENT */ 2 : ;
1079 LL_SSCANDONE(C_IDENT);
1080 # line 505 "LLgen.g"
1081 { pe = search(UNKNOWN,lextoken.t_string,BOTH);
1083 #ifdef NON_CORRECTING
1085 if (g_gettype(pres) != TERMINAL){
1087 "Erroneous only allowed on terminal");
1091 pres->g_erroneous = 1;
1098 L_10 : {switch(LLcsymb) {
1099 case /* C_IDENT */ 2 : ;
1100 case /* C_NUMBER */ 3 : ;
1101 case /* C_LITERAL */ 4 : ;
1102 case /* C_ACTION */ 7 : ;
1103 case /* C_SUBSTART */ 20 : ;
1104 case /* C_ERRONEOUS */ 21 : ;
1105 case /* ';' */ 24 : ;
1106 case /* '|' */ 26 : ;
1107 case /* '[' */ 27 : ;
1108 case /* ']' */ 28 : ;
1109 case /* '?' */ 29 : ;
1110 case /* '*' */ 30 : ;
1111 case /* '+' */ 31 : ;
1114 default:{int LL_12=LLnext(262);
1119 else if (LL_12 & 1) goto L_10;}
1120 case /* C_PARAMS */ 6 : ;
1123 # line 520 "LLgen.g"
1124 { if (lextoken.t_num > 15) {
1125 error(linecount,"Too many parameters");
1126 } else g_setnpar(pres,lextoken.t_num);
1127 if (g_gettype(pres) == TERMINAL) {
1129 "Terminal with parameters");
1136 default: if (LLskip()) goto L_8;
1138 case /* C_LITERAL */ 4 : ;
1141 # line 529 "LLgen.g"
1142 { pe = search(LITERAL,lextoken.t_string,BOTH);
1144 #ifdef NON_CORRECTING
1146 pres->g_erroneous = 1;
1155 # line 537 "LLgen.g"
1156 { g_settype(pres,ACTION);
1157 pres->g_lineno = linecount;
1158 #ifdef NON_CORRECTING
1159 g_setsubparse(pres, (p_start) 0);
1163 L_11 : {switch(LLcsymb) {
1166 case /* C_SUBSTART */ 20 : ;
1169 LL_SAFE(C_SUBSTART);
1170 # line 546 "LLgen.g"
1172 #ifdef NON_CORRECTING
1176 LL_NOSCANDONE(C_IDENT);
1177 # line 553 "LLgen.g"
1179 #ifdef NON_CORRECTING
1180 register p_gram temp;
1181 register p_start subp;
1183 temp = search(NONTERM,lextoken.t_string,BOTH);
1184 subp = (p_start) alloc (sizeof(t_start));
1186 subp->ff_nont = g_getcont(temp);
1187 subp->ff_name = (string) 0;
1188 subp->ff_next = (p_start) 0;
1189 g_setsubparse(pres, subp);
1195 L_12 : {switch(LLcsymb) {
1196 case /* ';' */ 24 : ;
1198 default:{int LL_13=LLnext(44);
1202 else if (LL_13 & 1) goto L_12;}
1203 case /* ',' */ 23 : ;
1205 LL_NOSCANDONE(C_IDENT);
1206 # line 569 "LLgen.g"
1208 #ifdef NON_CORRECTING
1209 register p_gram temp;
1210 register p_start ff;
1212 temp = search(NONTERM,lextoken.t_string,BOTH);
1214 ff = g_getsubparse(pres);
1216 if (ff->ff_nont == g_getcont(temp)) {
1217 warning(linecount, "\"%s\" used twice in %%substart", lextoken.t_string);
1224 ff = (p_start) alloc(sizeof(t_start));
1225 ff->ff_nont = g_getcont(temp);
1226 ff->ff_name = (string) 0;
1227 ff->ff_next = g_getsubparse(pres);
1228 g_setsubparse(pres, ff);
1244 LL_SCANDONE(C_ACTION);
1255 # line 600 "LLgen.g"
1256 int *kind ,int *cnt)
1258 # line 600 "LLgen.g"
1259 kind,cnt) int *kind; int *cnt;
1262 # line 600 "LLgen.g"
1267 # line 602 "LLgen.g"
1271 case /* '*' */ 30 : ;
1272 case /* '+' */ 31 : ;
1277 # line 603 "LLgen.g"
1280 case /* '+' */ 31 : ;
1282 # line 604 "LLgen.g"
1288 L_7 : {switch(LLcsymb) {
1289 case /* C_IDENT */ 2 : ;
1290 case /* C_LITERAL */ 4 : ;
1291 case /* C_ACTION */ 7 : ;
1292 case /* C_SUBSTART */ 20 : ;
1293 case /* C_ERRONEOUS */ 21 : ;
1294 case /* ';' */ 24 : ;
1295 case /* '|' */ 26 : ;
1296 case /* '[' */ 27 : ;
1297 case /* ']' */ 28 : ;
1300 default:{int LL_14=LLnext(259);
1305 else if (LL_14 & 1) goto L_7;}
1306 case /* C_NUMBER */ 3 : ;
1309 # line 606 "LLgen.g"
1314 # line 607 "LLgen.g"
1317 if (*kind == STAR) *kind = OPT;
1318 if (*kind == PLUS) *kind = FIXED;
1322 case /* C_NUMBER */ 3 : ;
1324 # line 613 "LLgen.g"
1329 # line 614 "LLgen.g"
1338 # line 617 "LLgen.g"
1341 # line 617 "LLgen.g"
1346 # line 619 "LLgen.g"
1347 { *t = lextoken.t_num;
1348 if (*t <= 0 || *t >= 8192) {
1349 error(linecount,"Illegal number");
1362 # line 626 "LLgen.g"
1368 LL_NOSCANDONE(C_IDENT);
1369 # line 628 "LLgen.g"
1370 { p = store(lextoken.t_string); }
1374 LL_NOSCANDONE(C_IDENT);
1377 # line 630 "LLgen.g"
1379 * Store this %first in the list belonging
1380 * to this input file
1383 register p_first ff;
1385 temp = search(NONTERM,lextoken.t_string,BOTH);
1386 ff = (p_first) alloc(sizeof(t_first));
1387 ff->ff_nont = g_getcont(temp);
1389 ff->ff_next = pfile->f_firsts;
1390 pfile->f_firsts = ff;
1394 # line 645 "LLgen.g"
1398 copyrule(p,length) register p_gram p; {
1400 * Returns a pointer to a grammar rule that was created in
1401 * p. The space pointed to by p can now be reused
1406 t = (p_gram) alloc((unsigned) length * sizeof(t_gram));