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