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