Annotation of src/usr.bin/yacc/output.c, Revision 1.11
1.11 ! pvalchev 1: /* $OpenBSD: output.c,v 1.10 2003/06/03 02:56:24 millert Exp $ */
1.2 deraadt 2: /* $NetBSD: output.c,v 1.4 1996/03/19 03:21:41 jtc Exp $ */
3:
4: /*
5: * Copyright (c) 1989 The Regents of the University of California.
6: * All rights reserved.
7: *
8: * This code is derived from software contributed to Berkeley by
9: * Robert Paul Corbett.
10: *
11: * Redistribution and use in source and binary forms, with or without
12: * modification, are permitted provided that the following conditions
13: * are met:
14: * 1. Redistributions of source code must retain the above copyright
15: * notice, this list of conditions and the following disclaimer.
16: * 2. Redistributions in binary form must reproduce the above copyright
17: * notice, this list of conditions and the following disclaimer in the
18: * documentation and/or other materials provided with the distribution.
1.10 millert 19: * 3. Neither the name of the University nor the names of its contributors
1.2 deraadt 20: * may be used to endorse or promote products derived from this software
21: * without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: */
35:
1.1 deraadt 36: #ifndef lint
1.2 deraadt 37: #if 0
38: static char sccsid[] = "@(#)output.c 5.7 (Berkeley) 5/24/93";
39: #else
1.11 ! pvalchev 40: static char rcsid[] = "$OpenBSD: output.c,v 1.10 2003/06/03 02:56:24 millert Exp $";
1.2 deraadt 41: #endif
1.1 deraadt 42: #endif /* not lint */
43:
44: #include "defs.h"
45:
46: static int nvectors;
47: static int nentries;
48: static short **froms;
49: static short **tos;
50: static short *tally;
51: static short *width;
52: static short *state_count;
53: static short *order;
54: static short *base;
55: static short *pos;
56: static int maxtable;
57: static short *table;
58: static short *check;
59: static int lowzero;
60: static int high;
61:
1.8 millert 62: void output_prefix(void);
63: void output_rule_data(void);
64: void output_yydefred(void);
65: void output_actions(void);
66: void token_actions(void);
67: void goto_actions(void);
68: int default_goto(int);
69: void save_column(int, int);
70: void sort_actions(void);
71: void pack_table(void);
72: int matching_vector(int);
73: int pack_vector(int);
74: void output_base(void);
75: void output_table(void);
76: void output_check(void);
77: int is_C_identifier(char *);
78: void output_defines(void);
79: void output_stored_text(void);
80: void output_debug(void);
81: void output_stype(void);
82: void output_trailing_text(void);
83: void output_semantic_actions(void);
84: void free_itemsets(void);
85: void free_shifts(void);
86: void free_reductions(void);
1.1 deraadt 87:
1.6 pvalchev 88: void
1.11 ! pvalchev 89: output(void)
1.1 deraadt 90: {
91: free_itemsets();
92: free_shifts();
93: free_reductions();
94: output_prefix();
95: output_stored_text();
96: output_defines();
97: output_rule_data();
98: output_yydefred();
99: output_actions();
100: free_parser();
101: output_debug();
102: output_stype();
103: if (rflag) write_section(tables);
104: write_section(header);
105: output_trailing_text();
106: write_section(body);
107: output_semantic_actions();
108: write_section(trailer);
109: }
110:
111:
1.6 pvalchev 112: void
1.11 ! pvalchev 113: output_prefix(void)
1.1 deraadt 114: {
115: if (symbol_prefix == NULL)
116: symbol_prefix = "yy";
117: else
118: {
119: ++outline;
120: fprintf(code_file, "#define yyparse %sparse\n", symbol_prefix);
121: ++outline;
122: fprintf(code_file, "#define yylex %slex\n", symbol_prefix);
123: ++outline;
124: fprintf(code_file, "#define yyerror %serror\n", symbol_prefix);
125: ++outline;
126: fprintf(code_file, "#define yychar %schar\n", symbol_prefix);
127: ++outline;
128: fprintf(code_file, "#define yyval %sval\n", symbol_prefix);
129: ++outline;
130: fprintf(code_file, "#define yylval %slval\n", symbol_prefix);
131: ++outline;
132: fprintf(code_file, "#define yydebug %sdebug\n", symbol_prefix);
133: ++outline;
134: fprintf(code_file, "#define yynerrs %snerrs\n", symbol_prefix);
135: ++outline;
136: fprintf(code_file, "#define yyerrflag %serrflag\n", symbol_prefix);
137: ++outline;
138: fprintf(code_file, "#define yyss %sss\n", symbol_prefix);
139: ++outline;
1.4 deraadt 140: fprintf(code_file, "#define yysslim %ssslim\n", symbol_prefix);
141: ++outline;
1.1 deraadt 142: fprintf(code_file, "#define yyssp %sssp\n", symbol_prefix);
143: ++outline;
144: fprintf(code_file, "#define yyvs %svs\n", symbol_prefix);
145: ++outline;
146: fprintf(code_file, "#define yyvsp %svsp\n", symbol_prefix);
1.4 deraadt 147: ++outline;
148: fprintf(code_file, "#define yystacksize %sstacksize\n", symbol_prefix);
1.1 deraadt 149: ++outline;
150: fprintf(code_file, "#define yylhs %slhs\n", symbol_prefix);
151: ++outline;
152: fprintf(code_file, "#define yylen %slen\n", symbol_prefix);
153: ++outline;
154: fprintf(code_file, "#define yydefred %sdefred\n", symbol_prefix);
155: ++outline;
156: fprintf(code_file, "#define yydgoto %sdgoto\n", symbol_prefix);
157: ++outline;
158: fprintf(code_file, "#define yysindex %ssindex\n", symbol_prefix);
159: ++outline;
160: fprintf(code_file, "#define yyrindex %srindex\n", symbol_prefix);
161: ++outline;
162: fprintf(code_file, "#define yygindex %sgindex\n", symbol_prefix);
163: ++outline;
164: fprintf(code_file, "#define yytable %stable\n", symbol_prefix);
165: ++outline;
166: fprintf(code_file, "#define yycheck %scheck\n", symbol_prefix);
167: ++outline;
168: fprintf(code_file, "#define yyname %sname\n", symbol_prefix);
169: ++outline;
170: fprintf(code_file, "#define yyrule %srule\n", symbol_prefix);
171: }
172: ++outline;
173: fprintf(code_file, "#define YYPREFIX \"%s\"\n", symbol_prefix);
174: }
175:
176:
1.6 pvalchev 177: void
1.11 ! pvalchev 178: output_rule_data(void)
1.1 deraadt 179: {
1.7 mpech 180: int i;
181: int j;
1.1 deraadt 182:
1.9 mickey 183:
184: fprintf(output_file,
185: "#if defined(__cplusplus) || defined(__STDC__)\n"
186: "const short %slhs[] =\n"
187: "#else\n"
188: "short %slhs[] =\n"
189: "#endif\n"
190: "\t{%42d,", symbol_prefix, symbol_prefix, symbol_value[start_symbol]);
1.1 deraadt 191:
192: j = 10;
193: for (i = 3; i < nrules; i++)
194: {
195: if (j >= 10)
196: {
197: if (!rflag) ++outline;
198: putc('\n', output_file);
199: j = 1;
200: }
201: else
202: ++j;
203:
204: fprintf(output_file, "%5d,", symbol_value[rlhs[i]]);
205: }
206: if (!rflag) outline += 2;
207: fprintf(output_file, "\n};\n");
208:
1.9 mickey 209: fprintf(output_file,
210: "#if defined(__cplusplus) || defined(__STDC__)\n"
211: "const short %slen[] =\n"
212: "#else\n"
213: "short %slen[] =\n"
214: "#endif\n"
215: "\t{%42d,", symbol_prefix, symbol_prefix, 2);
1.1 deraadt 216:
217: j = 10;
218: for (i = 3; i < nrules; i++)
219: {
220: if (j >= 10)
221: {
222: if (!rflag) ++outline;
223: putc('\n', output_file);
224: j = 1;
225: }
226: else
227: j++;
228:
229: fprintf(output_file, "%5d,", rrhs[i + 1] - rrhs[i] - 1);
230: }
231: if (!rflag) outline += 2;
232: fprintf(output_file, "\n};\n");
233: }
234:
235:
1.6 pvalchev 236: void
1.11 ! pvalchev 237: output_yydefred(void)
1.1 deraadt 238: {
1.7 mpech 239: int i, j;
1.1 deraadt 240:
1.9 mickey 241: fprintf(output_file,
242: "#if defined(__cplusplus) || defined(__STDC__)\n"
243: "const short %sdefred[] =\n"
244: "#else\n"
245: "short %sdefred[] =\n"
246: "#endif\n"
247: "\t{%39d,",
248: symbol_prefix, symbol_prefix, (defred[0] ? defred[0] - 2 : 0));
1.1 deraadt 249:
250: j = 10;
251: for (i = 1; i < nstates; i++)
252: {
253: if (j < 10)
254: ++j;
255: else
256: {
257: if (!rflag) ++outline;
258: putc('\n', output_file);
259: j = 1;
260: }
261:
262: fprintf(output_file, "%5d,", (defred[i] ? defred[i] - 2 : 0));
263: }
264:
265: if (!rflag) outline += 2;
266: fprintf(output_file, "\n};\n");
267: }
268:
269:
1.6 pvalchev 270: void
1.11 ! pvalchev 271: output_actions(void)
1.1 deraadt 272: {
273: nvectors = 2*nstates + nvars;
274:
275: froms = NEW2(nvectors, short *);
276: tos = NEW2(nvectors, short *);
277: tally = NEW2(nvectors, short);
278: width = NEW2(nvectors, short);
279:
280: token_actions();
281: FREE(lookaheads);
282: FREE(LA);
283: FREE(LAruleno);
284: FREE(accessing_symbol);
285:
286: goto_actions();
287: FREE(goto_map + ntokens);
288: FREE(from_state);
289: FREE(to_state);
290:
291: sort_actions();
292: pack_table();
293: output_base();
294: output_table();
295: output_check();
296: }
297:
298:
1.6 pvalchev 299: void
1.11 ! pvalchev 300: token_actions(void)
1.1 deraadt 301: {
1.7 mpech 302: int i, j;
303: int shiftcount, reducecount;
304: int max, min;
305: short *actionrow, *r, *s;
306: action *p;
1.1 deraadt 307:
308: actionrow = NEW2(2*ntokens, short);
309: for (i = 0; i < nstates; ++i)
310: {
311: if (parser[i])
312: {
313: for (j = 0; j < 2*ntokens; ++j)
314: actionrow[j] = 0;
315:
316: shiftcount = 0;
317: reducecount = 0;
318: for (p = parser[i]; p; p = p->next)
319: {
320: if (p->suppressed == 0)
321: {
322: if (p->action_code == SHIFT)
323: {
324: ++shiftcount;
325: actionrow[p->symbol] = p->number;
326: }
327: else if (p->action_code == REDUCE && p->number != defred[i])
328: {
329: ++reducecount;
330: actionrow[p->symbol + ntokens] = p->number;
331: }
332: }
333: }
334:
335: tally[i] = shiftcount;
336: tally[nstates+i] = reducecount;
337: width[i] = 0;
338: width[nstates+i] = 0;
339: if (shiftcount > 0)
340: {
341: froms[i] = r = NEW2(shiftcount, short);
342: tos[i] = s = NEW2(shiftcount, short);
343: min = MAXSHORT;
344: max = 0;
345: for (j = 0; j < ntokens; ++j)
346: {
347: if (actionrow[j])
348: {
349: if (min > symbol_value[j])
350: min = symbol_value[j];
351: if (max < symbol_value[j])
352: max = symbol_value[j];
353: *r++ = symbol_value[j];
354: *s++ = actionrow[j];
355: }
356: }
357: width[i] = max - min + 1;
358: }
359: if (reducecount > 0)
360: {
361: froms[nstates+i] = r = NEW2(reducecount, short);
362: tos[nstates+i] = s = NEW2(reducecount, short);
363: min = MAXSHORT;
364: max = 0;
365: for (j = 0; j < ntokens; ++j)
366: {
367: if (actionrow[ntokens+j])
368: {
369: if (min > symbol_value[j])
370: min = symbol_value[j];
371: if (max < symbol_value[j])
372: max = symbol_value[j];
373: *r++ = symbol_value[j];
374: *s++ = actionrow[ntokens+j] - 2;
375: }
376: }
377: width[nstates+i] = max - min + 1;
378: }
379: }
380: }
381: FREE(actionrow);
382: }
383:
1.6 pvalchev 384: void
1.11 ! pvalchev 385: goto_actions(void)
1.1 deraadt 386: {
1.7 mpech 387: int i, j, k;
1.1 deraadt 388:
389: state_count = NEW2(nstates, short);
390:
391: k = default_goto(start_symbol + 1);
1.9 mickey 392: fprintf(output_file,
393: "#if defined(__cplusplus) || defined(__STDC__)\n"
394: "const short %sdgoto[] =\n"
395: "#else\n"
396: "short %sdgoto[] =\n"
397: "#endif\n"
398: "\t{%40d,", symbol_prefix, symbol_prefix, k);
1.1 deraadt 399: save_column(start_symbol + 1, k);
400:
401: j = 10;
402: for (i = start_symbol + 2; i < nsyms; i++)
403: {
404: if (j >= 10)
405: {
406: if (!rflag) ++outline;
407: putc('\n', output_file);
408: j = 1;
409: }
410: else
411: ++j;
412:
413: k = default_goto(i);
414: fprintf(output_file, "%5d,", k);
415: save_column(i, k);
416: }
417:
418: if (!rflag) outline += 2;
419: fprintf(output_file, "\n};\n");
420: FREE(state_count);
421: }
422:
423: int
1.11 ! pvalchev 424: default_goto(int symbol)
1.1 deraadt 425: {
1.7 mpech 426: int i;
427: int m;
428: int n;
429: int default_state;
430: int max;
1.1 deraadt 431:
432: m = goto_map[symbol];
433: n = goto_map[symbol + 1];
434:
435: if (m == n) return (0);
436:
437: for (i = 0; i < nstates; i++)
438: state_count[i] = 0;
439:
440: for (i = m; i < n; i++)
441: state_count[to_state[i]]++;
442:
443: max = 0;
444: default_state = 0;
445: for (i = 0; i < nstates; i++)
446: {
447: if (state_count[i] > max)
448: {
449: max = state_count[i];
450: default_state = i;
451: }
452: }
453:
454: return (default_state);
455: }
456:
457:
458:
1.6 pvalchev 459: void
1.11 ! pvalchev 460: save_column(int symbol, int default_state)
1.1 deraadt 461: {
1.7 mpech 462: int i;
463: int m;
464: int n;
465: short *sp;
466: short *sp1;
467: short *sp2;
468: int count;
469: int symno;
1.1 deraadt 470:
471: m = goto_map[symbol];
472: n = goto_map[symbol + 1];
473:
474: count = 0;
475: for (i = m; i < n; i++)
476: {
477: if (to_state[i] != default_state)
478: ++count;
479: }
480: if (count == 0) return;
481:
482: symno = symbol_value[symbol] + 2*nstates;
483:
484: froms[symno] = sp1 = sp = NEW2(count, short);
485: tos[symno] = sp2 = NEW2(count, short);
486:
487: for (i = m; i < n; i++)
488: {
489: if (to_state[i] != default_state)
490: {
491: *sp1++ = from_state[i];
492: *sp2++ = to_state[i];
493: }
494: }
495:
496: tally[symno] = count;
497: width[symno] = sp1[-1] - sp[0] + 1;
498: }
499:
1.6 pvalchev 500: void
1.11 ! pvalchev 501: sort_actions(void)
1.1 deraadt 502: {
1.7 mpech 503: int i;
504: int j;
505: int k;
506: int t;
507: int w;
1.1 deraadt 508:
509: order = NEW2(nvectors, short);
510: nentries = 0;
511:
512: for (i = 0; i < nvectors; i++)
513: {
514: if (tally[i] > 0)
515: {
516: t = tally[i];
517: w = width[i];
518: j = nentries - 1;
519:
520: while (j >= 0 && (width[order[j]] < w))
521: j--;
522:
523: while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
524: j--;
525:
526: for (k = nentries - 1; k > j; k--)
527: order[k + 1] = order[k];
528:
529: order[j + 1] = i;
530: nentries++;
531: }
532: }
533: }
534:
535:
1.6 pvalchev 536: void
1.11 ! pvalchev 537: pack_table(void)
1.1 deraadt 538: {
1.7 mpech 539: int i;
540: int place;
541: int state;
1.1 deraadt 542:
543: base = NEW2(nvectors, short);
544: pos = NEW2(nentries, short);
545:
546: maxtable = 1000;
547: table = NEW2(maxtable, short);
548: check = NEW2(maxtable, short);
549:
550: lowzero = 0;
551: high = 0;
552:
553: for (i = 0; i < maxtable; i++)
554: check[i] = -1;
555:
556: for (i = 0; i < nentries; i++)
557: {
558: state = matching_vector(i);
559:
560: if (state < 0)
561: place = pack_vector(i);
562: else
563: place = base[state];
564:
565: pos[i] = place;
566: base[order[i]] = place;
567: }
568:
569: for (i = 0; i < nvectors; i++)
570: {
571: if (froms[i])
572: FREE(froms[i]);
573: if (tos[i])
574: FREE(tos[i]);
575: }
576:
577: FREE(froms);
578: FREE(tos);
579: FREE(pos);
580: }
581:
582:
583: /* The function matching_vector determines if the vector specified by */
584: /* the input parameter matches a previously considered vector. The */
585: /* test at the start of the function checks if the vector represents */
586: /* a row of shifts over terminal symbols or a row of reductions, or a */
587: /* column of shifts over a nonterminal symbol. Berkeley Yacc does not */
588: /* check if a column of shifts over a nonterminal symbols matches a */
589: /* previously considered vector. Because of the nature of LR parsing */
590: /* tables, no two columns can match. Therefore, the only possible */
591: /* match would be between a row and a column. Such matches are */
592: /* unlikely. Therefore, to save time, no attempt is made to see if a */
593: /* column matches a previously considered vector. */
594: /* */
595: /* Matching_vector is poorly designed. The test could easily be made */
596: /* faster. Also, it depends on the vectors being in a specific */
597: /* order. */
598:
599: int
1.11 ! pvalchev 600: matching_vector(int vector)
1.1 deraadt 601: {
1.7 mpech 602: int i;
603: int j;
604: int k;
605: int t;
606: int w;
607: int match;
608: int prev;
1.1 deraadt 609:
610: i = order[vector];
611: if (i >= 2*nstates)
612: return (-1);
613:
614: t = tally[i];
615: w = width[i];
616:
617: for (prev = vector - 1; prev >= 0; prev--)
618: {
619: j = order[prev];
620: if (width[j] != w || tally[j] != t)
621: return (-1);
622:
623: match = 1;
624: for (k = 0; match && k < t; k++)
625: {
626: if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
627: match = 0;
628: }
629:
630: if (match)
631: return (j);
632: }
633:
634: return (-1);
635: }
636:
637:
638:
639: int
1.11 ! pvalchev 640: pack_vector(int vector)
1.1 deraadt 641: {
1.7 mpech 642: int i, j, k, l;
643: int t;
644: int loc;
645: int ok;
646: short *from;
647: short *to;
1.1 deraadt 648: int newmax;
649:
650: i = order[vector];
651: t = tally[i];
652: assert(t);
653:
654: from = froms[i];
655: to = tos[i];
656:
657: j = lowzero - from[0];
658: for (k = 1; k < t; ++k)
659: if (lowzero - from[k] > j)
660: j = lowzero - from[k];
661: for (;; ++j)
662: {
663: if (j == 0)
664: continue;
665: ok = 1;
666: for (k = 0; ok && k < t; k++)
667: {
668: loc = j + from[k];
669: if (loc >= maxtable)
670: {
671: if (loc >= MAXTABLE)
672: fatal("maximum table size exceeded");
673:
674: newmax = maxtable;
675: do { newmax += 200; } while (newmax <= loc);
676: table = (short *) REALLOC(table, newmax*sizeof(short));
677: if (table == 0) no_space();
678: check = (short *) REALLOC(check, newmax*sizeof(short));
679: if (check == 0) no_space();
680: for (l = maxtable; l < newmax; ++l)
681: {
682: table[l] = 0;
683: check[l] = -1;
684: }
685: maxtable = newmax;
686: }
687:
688: if (check[loc] != -1)
689: ok = 0;
690: }
691: for (k = 0; ok && k < vector; k++)
692: {
693: if (pos[k] == j)
694: ok = 0;
695: }
696: if (ok)
697: {
698: for (k = 0; k < t; k++)
699: {
700: loc = j + from[k];
701: table[loc] = to[k];
702: check[loc] = from[k];
703: if (loc > high) high = loc;
704: }
705:
706: while (check[lowzero] != -1)
707: ++lowzero;
708:
709: return (j);
710: }
711: }
712: }
713:
714:
715:
1.6 pvalchev 716: void
1.11 ! pvalchev 717: output_base(void)
1.1 deraadt 718: {
1.7 mpech 719: int i, j;
1.1 deraadt 720:
1.9 mickey 721: fprintf(output_file,
722: "#if defined(__cplusplus) || defined(__STDC__)\n"
723: "const short %ssindex[] =\n"
724: "#else\n"
725: "short %ssindex[] =\n"
726: "#endif\n"
727: "\t{%39d,", symbol_prefix, symbol_prefix, base[0]);
1.1 deraadt 728:
729: j = 10;
730: for (i = 1; i < nstates; i++)
731: {
732: if (j >= 10)
733: {
734: if (!rflag) ++outline;
735: putc('\n', output_file);
736: j = 1;
737: }
738: else
739: ++j;
740:
741: fprintf(output_file, "%5d,", base[i]);
742: }
743:
744: if (!rflag) outline += 2;
1.9 mickey 745: fprintf(output_file,
746: "};\n"
747: "#if defined(__cplusplus) || defined(__STDC__)\n"
748: "const short %srindex[] =\n"
749: "#else\n"
750: "short %srindex[] =\n"
751: "#endif\n"
752: "\t{%39d,", symbol_prefix, symbol_prefix, base[nstates]);
1.1 deraadt 753:
754: j = 10;
755: for (i = nstates + 1; i < 2*nstates; i++)
756: {
757: if (j >= 10)
758: {
759: if (!rflag) ++outline;
760: putc('\n', output_file);
761: j = 1;
762: }
763: else
764: ++j;
765:
766: fprintf(output_file, "%5d,", base[i]);
767: }
768:
769: if (!rflag) outline += 2;
1.9 mickey 770: fprintf(output_file,
771: "};\n"
772: "#if defined(__cplusplus) || defined(__STDC__)\n"
773: "const short %sgindex[] =\n"
774: "#else\n"
775: "short %sgindex[] =\n"
776: "#endif\n"
777: "\t{%39d,", symbol_prefix, symbol_prefix, base[2*nstates]);
1.1 deraadt 778:
779: j = 10;
780: for (i = 2*nstates + 1; i < nvectors - 1; i++)
781: {
782: if (j >= 10)
783: {
784: if (!rflag) ++outline;
785: putc('\n', output_file);
786: j = 1;
787: }
788: else
789: ++j;
790:
791: fprintf(output_file, "%5d,", base[i]);
792: }
793:
794: if (!rflag) outline += 2;
795: fprintf(output_file, "\n};\n");
796: FREE(base);
797: }
798:
799:
1.6 pvalchev 800: void
1.11 ! pvalchev 801: output_table(void)
1.1 deraadt 802: {
1.7 mpech 803: int i;
804: int j;
1.1 deraadt 805:
806: ++outline;
807: fprintf(code_file, "#define YYTABLESIZE %d\n", high);
1.9 mickey 808: fprintf(output_file,
809: "#if defined(__cplusplus) || defined(__STDC__)\n"
810: "const short %stable[] =\n"
811: "#else\n"
812: "short %stable[] =\n"
813: "#endif\n"
814: "\t{%40d,", symbol_prefix, symbol_prefix, table[0]);
1.1 deraadt 815:
816: j = 10;
817: for (i = 1; i <= high; i++)
818: {
819: if (j >= 10)
820: {
821: if (!rflag) ++outline;
822: putc('\n', output_file);
823: j = 1;
824: }
825: else
826: ++j;
827:
828: fprintf(output_file, "%5d,", table[i]);
829: }
830:
831: if (!rflag) outline += 2;
832: fprintf(output_file, "\n};\n");
833: FREE(table);
834: }
835:
836:
1.6 pvalchev 837: void
1.11 ! pvalchev 838: output_check(void)
1.1 deraadt 839: {
1.7 mpech 840: int i;
841: int j;
1.1 deraadt 842:
1.9 mickey 843: fprintf(output_file,
844: "#if defined(__cplusplus) || defined(__STDC__)\n"
845: "const short %scheck[] =\n"
846: "#else\n"
847: "short %scheck[] =\n"
848: "#endif\n"
849: "\t{%40d,", symbol_prefix, symbol_prefix, check[0]);
1.1 deraadt 850:
851: j = 10;
852: for (i = 1; i <= high; i++)
853: {
854: if (j >= 10)
855: {
856: if (!rflag) ++outline;
857: putc('\n', output_file);
858: j = 1;
859: }
860: else
861: ++j;
862:
863: fprintf(output_file, "%5d,", check[i]);
864: }
865:
866: if (!rflag) outline += 2;
867: fprintf(output_file, "\n};\n");
868: FREE(check);
869: }
870:
871:
872: int
1.11 ! pvalchev 873: is_C_identifier(char *name)
1.1 deraadt 874: {
1.7 mpech 875: char *s;
876: int c;
1.1 deraadt 877:
878: s = name;
879: c = *s;
880: if (c == '"')
881: {
882: c = *++s;
883: if (!isalpha(c) && c != '_' && c != '$')
884: return (0);
885: while ((c = *++s) != '"')
886: {
887: if (!isalnum(c) && c != '_' && c != '$')
888: return (0);
889: }
890: return (1);
891: }
892:
893: if (!isalpha(c) && c != '_' && c != '$')
894: return (0);
895: while (c = *++s)
896: {
897: if (!isalnum(c) && c != '_' && c != '$')
898: return (0);
899: }
900: return (1);
901: }
902:
903:
1.6 pvalchev 904: void
1.11 ! pvalchev 905: output_defines(void)
1.1 deraadt 906: {
1.7 mpech 907: int c, i;
908: char *s;
1.1 deraadt 909:
910: for (i = 2; i < ntokens; ++i)
911: {
912: s = symbol_name[i];
913: if (is_C_identifier(s))
914: {
915: fprintf(code_file, "#define ");
916: if (dflag) fprintf(defines_file, "#define ");
917: c = *s;
918: if (c == '"')
919: {
920: while ((c = *++s) != '"')
921: {
922: putc(c, code_file);
923: if (dflag) putc(c, defines_file);
924: }
925: }
926: else
927: {
928: do
929: {
930: putc(c, code_file);
931: if (dflag) putc(c, defines_file);
932: }
933: while (c = *++s);
934: }
935: ++outline;
936: fprintf(code_file, " %d\n", symbol_value[i]);
937: if (dflag) fprintf(defines_file, " %d\n", symbol_value[i]);
938: }
939: }
940:
941: ++outline;
942: fprintf(code_file, "#define YYERRCODE %d\n", symbol_value[1]);
943:
944: if (dflag && unionized)
945: {
946: fclose(union_file);
947: union_file = fopen(union_file_name, "r");
948: if (union_file == NULL) open_error(union_file_name);
949: while ((c = getc(union_file)) != EOF)
950: putc(c, defines_file);
951: fprintf(defines_file, " YYSTYPE;\nextern YYSTYPE %slval;\n",
952: symbol_prefix);
953: }
954: }
955:
956:
1.6 pvalchev 957: void
1.11 ! pvalchev 958: output_stored_text(void)
1.1 deraadt 959: {
1.7 mpech 960: int c;
961: FILE *in, *out;
1.1 deraadt 962:
963: fclose(text_file);
964: text_file = fopen(text_file_name, "r");
965: if (text_file == NULL)
966: open_error(text_file_name);
967: in = text_file;
968: if ((c = getc(in)) == EOF)
969: return;
970: out = code_file;
971: if (c == '\n')
972: ++outline;
973: putc(c, out);
974: while ((c = getc(in)) != EOF)
975: {
976: if (c == '\n')
977: ++outline;
978: putc(c, out);
979: }
980: if (!lflag)
981: fprintf(out, line_format, ++outline + 1, code_file_name);
982: }
983:
984:
1.6 pvalchev 985: void
1.11 ! pvalchev 986: output_debug(void)
1.1 deraadt 987: {
1.7 mpech 988: int i, j, k, max;
1.1 deraadt 989: char **symnam, *s;
990:
991: ++outline;
992: fprintf(code_file, "#define YYFINAL %d\n", final_state);
993: outline += 3;
994: fprintf(code_file, "#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n",
995: tflag);
996: if (rflag)
997: fprintf(output_file, "#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n",
998: tflag);
999:
1000: max = 0;
1001: for (i = 2; i < ntokens; ++i)
1002: if (symbol_value[i] > max)
1003: max = symbol_value[i];
1004: ++outline;
1005: fprintf(code_file, "#define YYMAXTOKEN %d\n", max);
1006:
1007: symnam = (char **) MALLOC((max+1)*sizeof(char *));
1008: if (symnam == 0) no_space();
1009:
1010: /* Note that it is not necessary to initialize the element */
1011: /* symnam[max]. */
1012: for (i = 0; i < max; ++i)
1013: symnam[i] = 0;
1014: for (i = ntokens - 1; i >= 2; --i)
1015: symnam[symbol_value[i]] = symbol_name[i];
1016: symnam[0] = "end-of-file";
1017:
1018: if (!rflag) ++outline;
1.9 mickey 1019: fprintf(output_file,
1020: "#if YYDEBUG\n"
1021: "#if defined(__cplusplus) || defined(__STDC__)\n"
1022: "const char * const %sname[] =\n"
1023: "#else\n"
1024: "char *%sname[] =\n"
1025: "#endif\n"
1026: "\t{", symbol_prefix, symbol_prefix);
1.1 deraadt 1027: j = 80;
1028: for (i = 0; i <= max; ++i)
1029: {
1.6 pvalchev 1030: if ((s = symnam[i]) != '\0')
1.1 deraadt 1031: {
1032: if (s[0] == '"')
1033: {
1034: k = 7;
1035: while (*++s != '"')
1036: {
1037: ++k;
1038: if (*s == '\\')
1039: {
1040: k += 2;
1041: if (*++s == '\\')
1042: ++k;
1043: }
1044: }
1045: j += k;
1046: if (j > 80)
1047: {
1048: if (!rflag) ++outline;
1049: putc('\n', output_file);
1050: j = k;
1051: }
1052: fprintf(output_file, "\"\\\"");
1053: s = symnam[i];
1054: while (*++s != '"')
1055: {
1056: if (*s == '\\')
1057: {
1058: fprintf(output_file, "\\\\");
1059: if (*++s == '\\')
1060: fprintf(output_file, "\\\\");
1061: else
1062: putc(*s, output_file);
1063: }
1064: else
1065: putc(*s, output_file);
1066: }
1067: fprintf(output_file, "\\\"\",");
1068: }
1069: else if (s[0] == '\'')
1070: {
1071: if (s[1] == '"')
1072: {
1073: j += 7;
1074: if (j > 80)
1075: {
1076: if (!rflag) ++outline;
1077: putc('\n', output_file);
1078: j = 7;
1079: }
1080: fprintf(output_file, "\"'\\\"'\",");
1081: }
1082: else
1083: {
1084: k = 5;
1085: while (*++s != '\'')
1086: {
1087: ++k;
1088: if (*s == '\\')
1089: {
1090: k += 2;
1091: if (*++s == '\\')
1092: ++k;
1093: }
1094: }
1095: j += k;
1096: if (j > 80)
1097: {
1098: if (!rflag) ++outline;
1099: putc('\n', output_file);
1100: j = k;
1101: }
1102: fprintf(output_file, "\"'");
1103: s = symnam[i];
1104: while (*++s != '\'')
1105: {
1106: if (*s == '\\')
1107: {
1108: fprintf(output_file, "\\\\");
1109: if (*++s == '\\')
1110: fprintf(output_file, "\\\\");
1111: else
1112: putc(*s, output_file);
1113: }
1114: else
1115: putc(*s, output_file);
1116: }
1117: fprintf(output_file, "'\",");
1118: }
1119: }
1120: else
1121: {
1122: k = strlen(s) + 3;
1123: j += k;
1124: if (j > 80)
1125: {
1126: if (!rflag) ++outline;
1127: putc('\n', output_file);
1128: j = k;
1129: }
1130: putc('"', output_file);
1131: do { putc(*s, output_file); } while (*++s);
1132: fprintf(output_file, "\",");
1133: }
1134: }
1135: else
1136: {
1137: j += 2;
1138: if (j > 80)
1139: {
1140: if (!rflag) ++outline;
1141: putc('\n', output_file);
1142: j = 2;
1143: }
1144: fprintf(output_file, "0,");
1145: }
1146: }
1147: if (!rflag) outline += 2;
1148: fprintf(output_file, "\n};\n");
1149: FREE(symnam);
1150:
1151: if (!rflag) ++outline;
1.9 mickey 1152: fprintf(output_file,
1153: "#if defined(__cplusplus) || defined(__STDC__)\n"
1154: "const char * const %srule[] =\n"
1155: "#else\n"
1156: "char *%srule[] =\n"
1157: "#endif\n"
1158: "\t{", symbol_prefix, symbol_prefix);
1.1 deraadt 1159: for (i = 2; i < nrules; ++i)
1160: {
1161: fprintf(output_file, "\"%s :", symbol_name[rlhs[i]]);
1162: for (j = rrhs[i]; ritem[j] > 0; ++j)
1163: {
1164: s = symbol_name[ritem[j]];
1165: if (s[0] == '"')
1166: {
1167: fprintf(output_file, " \\\"");
1168: while (*++s != '"')
1169: {
1170: if (*s == '\\')
1171: {
1172: if (s[1] == '\\')
1173: fprintf(output_file, "\\\\\\\\");
1174: else
1175: fprintf(output_file, "\\\\%c", s[1]);
1176: ++s;
1177: }
1178: else
1179: putc(*s, output_file);
1180: }
1181: fprintf(output_file, "\\\"");
1182: }
1183: else if (s[0] == '\'')
1184: {
1185: if (s[1] == '"')
1186: fprintf(output_file, " '\\\"'");
1187: else if (s[1] == '\\')
1188: {
1189: if (s[2] == '\\')
1190: fprintf(output_file, " '\\\\\\\\");
1191: else
1192: fprintf(output_file, " '\\\\%c", s[2]);
1193: s += 2;
1194: while (*++s != '\'')
1195: putc(*s, output_file);
1196: putc('\'', output_file);
1197: }
1198: else
1199: fprintf(output_file, " '%c'", s[1]);
1200: }
1201: else
1202: fprintf(output_file, " %s", s);
1203: }
1204: if (!rflag) ++outline;
1205: fprintf(output_file, "\",\n");
1206: }
1207:
1208: if (!rflag) outline += 2;
1209: fprintf(output_file, "};\n#endif\n");
1210: }
1211:
1212:
1.6 pvalchev 1213: void
1.11 ! pvalchev 1214: output_stype(void)
1.1 deraadt 1215: {
1216: if (!unionized && ntags == 0)
1217: {
1218: outline += 3;
1219: fprintf(code_file, "#ifndef YYSTYPE\ntypedef int YYSTYPE;\n#endif\n");
1220: }
1221: }
1222:
1223:
1.6 pvalchev 1224: void
1.11 ! pvalchev 1225: output_trailing_text(void)
1.1 deraadt 1226: {
1.7 mpech 1227: int c, last;
1228: FILE *in, *out;
1.1 deraadt 1229:
1230: if (line == 0)
1231: return;
1232:
1233: in = input_file;
1234: out = code_file;
1235: c = *cptr;
1236: if (c == '\n')
1237: {
1238: ++lineno;
1239: if ((c = getc(in)) == EOF)
1240: return;
1241: if (!lflag)
1242: {
1243: ++outline;
1244: fprintf(out, line_format, lineno, input_file_name);
1245: }
1246: if (c == '\n')
1247: ++outline;
1248: putc(c, out);
1249: last = c;
1250: }
1251: else
1252: {
1253: if (!lflag)
1254: {
1255: ++outline;
1256: fprintf(out, line_format, lineno, input_file_name);
1257: }
1258: do { putc(c, out); } while ((c = *++cptr) != '\n');
1259: ++outline;
1260: putc('\n', out);
1261: last = '\n';
1262: }
1263:
1264: while ((c = getc(in)) != EOF)
1265: {
1266: if (c == '\n')
1267: ++outline;
1268: putc(c, out);
1269: last = c;
1270: }
1271:
1272: if (last != '\n')
1273: {
1274: ++outline;
1275: putc('\n', out);
1276: }
1277: if (!lflag)
1278: fprintf(out, line_format, ++outline + 1, code_file_name);
1279: }
1280:
1281:
1.6 pvalchev 1282: void
1.11 ! pvalchev 1283: output_semantic_actions(void)
1.1 deraadt 1284: {
1.7 mpech 1285: int c, last;
1286: FILE *out;
1.1 deraadt 1287:
1288: fclose(action_file);
1289: action_file = fopen(action_file_name, "r");
1290: if (action_file == NULL)
1291: open_error(action_file_name);
1292:
1293: if ((c = getc(action_file)) == EOF)
1294: return;
1295:
1296: out = code_file;
1297: last = c;
1298: if (c == '\n')
1299: ++outline;
1300: putc(c, out);
1301: while ((c = getc(action_file)) != EOF)
1302: {
1303: if (c == '\n')
1304: ++outline;
1305: putc(c, out);
1306: last = c;
1307: }
1308:
1309: if (last != '\n')
1310: {
1311: ++outline;
1312: putc('\n', out);
1313: }
1314:
1315: if (!lflag)
1316: fprintf(out, line_format, ++outline + 1, code_file_name);
1317: }
1318:
1319:
1.6 pvalchev 1320: void
1.11 ! pvalchev 1321: free_itemsets(void)
1.1 deraadt 1322: {
1.7 mpech 1323: core *cp, *next;
1.1 deraadt 1324:
1325: FREE(state_table);
1326: for (cp = first_state; cp; cp = next)
1327: {
1328: next = cp->next;
1329: FREE(cp);
1330: }
1331: }
1332:
1333:
1.6 pvalchev 1334: void
1.11 ! pvalchev 1335: free_shifts(void)
1.1 deraadt 1336: {
1.7 mpech 1337: shifts *sp, *next;
1.1 deraadt 1338:
1339: FREE(shift_table);
1340: for (sp = first_shift; sp; sp = next)
1341: {
1342: next = sp->next;
1343: FREE(sp);
1344: }
1345: }
1346:
1347:
1348:
1.6 pvalchev 1349: void
1.11 ! pvalchev 1350: free_reductions(void)
1.1 deraadt 1351: {
1.7 mpech 1352: reductions *rp, *next;
1.1 deraadt 1353:
1354: FREE(reduction_table);
1355: for (rp = first_reduction; rp; rp = next)
1356: {
1357: next = rp->next;
1358: FREE(rp);
1359: }
1360: }