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