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