home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 404_02 / bisnp / output.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-16  |  32.4 KB  |  1,654 lines

  1. /* Output the generated parsing program for bison,
  2.    Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of Bison, the GNU Compiler Compiler.
  5.  
  6. Bison is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. Bison is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with Bison; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. /* functions to output parsing data to various files.  Entries are:
  22.  
  23.   output_headers ()
  24.  
  25. Output constant strings to the beginning of certain files.
  26.  
  27.   output_trailers()
  28.  
  29. Output constant strings to the ends of certain files.
  30.  
  31.   output ()
  32.  
  33. Output the parsing tables and the parser code to ftable.
  34.  
  35. The parser tables consist of these tables.
  36. Starred ones needed only for the semantic parser.
  37.  
  38. yytranslate = vector mapping yylex's token numbers into bison's token numbers.
  39.  
  40. yytname = vector of string-names indexed by bison token number
  41.  
  42. yyrline = vector of line-numbers of all rules.  For yydebug printouts.
  43.  
  44. yyrhs = vector of items of all rules.
  45.         This is exactly what ritems contains.  For yydebug and for semantic
  46.     parser.
  47.  
  48. yyprhs[r] = index in yyrhs of first item for rule r.
  49.  
  50. yyr1[r] = symbol number of symbol that rule r derives.
  51.  
  52. yyr2[r] = number of symbols composing right hand side of rule r.
  53.  
  54. * yystos[s] = the symbol number of the symbol that leads to state s.
  55.  
  56. yydefact[s] = default rule to reduce with in state s,
  57.           when yytable doesn't specify something else to do.
  58.           Zero means the default is an error.
  59.  
  60. yydefgoto[i] = default state to go to after a reduction of a rule that
  61.            generates variable ntokens + i, except when yytable
  62.            specifies something else to do.
  63.  
  64. yypact[s] = index in yytable of the portion describing state s.
  65.             The lookahead token's type is used to index that portion
  66.             to find out what to do.
  67.  
  68.         If the value in yytable is positive,
  69.         we shift the token and go to that state.
  70.  
  71.         If the value is negative, it is minus a rule number to reduce by.
  72.  
  73.         If the value is zero, the default action from yydefact[s] is used.
  74.  
  75. yypgoto[i] = the index in yytable of the portion describing 
  76.              what to do after reducing a rule that derives variable i + ntokens.
  77.              This portion is indexed by the parser state number
  78.          as of before the text for this nonterminal was read.
  79.          The value from yytable is the state to go to.
  80.  
  81. yytable = a vector filled with portions for different uses,
  82.           found via yypact and yypgoto.
  83.  
  84. yycheck = a vector indexed in parallel with yytable.
  85.       It indicates, in a roundabout way, the bounds of the
  86.       portion you are trying to examine.
  87.  
  88.       Suppose that the portion of yytable starts at index p
  89.       and the index to be examined within the portion is i.
  90.       Then if yycheck[p+i] != i, i is outside the bounds
  91.       of what is actually allocated, and the default
  92.       (from yydefact or yydefgoto) should be used.
  93.       Otherwise, yytable[p+i] should be used.
  94.  
  95. YYFINAL = the state number of the termination state.
  96. YYFLAG = most negative short int.  Used to flag ??
  97. YYNTBASE = ntokens.
  98.  
  99. */
  100.  
  101. #include <stdio.h>
  102. #include "system.h"
  103. #include "machine.h"
  104. #include "new.h"
  105. #include "files.h"
  106. #include "gram.h"
  107. #include "state.h"
  108. #include "symtab.h"
  109.  
  110.  
  111. extern int debugflag;
  112. extern int nolinesflag;
  113. extern int definesflag;
  114.  
  115. extern char **tags;
  116. extern int tokensetsize;
  117. extern int final_state;
  118. extern core **state_table;
  119. extern shifts **shift_table;
  120. extern errs **err_table;
  121. extern reductions **reduction_table;
  122. extern short *accessing_symbol;
  123. extern unsigned *LA;
  124. extern short *LAruleno;
  125. extern short *lookaheads;
  126. extern char *consistent;
  127. extern short *goto_map;
  128. extern short *from_state;
  129. extern short *to_state;
  130.  
  131. void output_token_translations();
  132. void output_gram();
  133. void output_stos();
  134. void output_rule_data();
  135. void output_defines();
  136. void output_actions();
  137. void token_actions();
  138. void save_row();
  139. void goto_actions();
  140. void save_column();
  141. void sort_actions();
  142. void pack_table();
  143. void output_base();
  144. void output_table();
  145. void output_check();
  146. void output_parser();
  147. void output_program();
  148. void free_itemset();
  149. void free_shifts();
  150. void free_reductions();
  151. void free_itemsets();
  152. int action_row();
  153. int default_goto();
  154. int matching_state();
  155. int pack_vector();
  156.  
  157. extern void berror();
  158. extern void fatals();
  159.  
  160. static int nvectors;
  161. static int nentries;
  162. static short **froms;
  163. static short **tos;
  164. static short *tally;
  165. static short *width;
  166. static short *actrow;
  167. static short *state_count;
  168. static short *order;
  169. static short *base;
  170. static short *pos;
  171. static short *table;
  172. static short *check;
  173. static int lowzero;
  174. static int high;
  175.  
  176. void
  177. output_section();
  178. void
  179. output_token_defines_fmt();
  180. extern bucket *errtoken;
  181. void output_token_defines();
  182. void output_token_const_def();
  183. void output_token_const_decl();
  184. void output_token_enum();
  185. extern int line_fparser;
  186. extern int line_fhskel;
  187. extern char *parser_fname;
  188. extern char *hskel_fname;
  189. extern char *version_string;
  190.  
  191.  
  192.  
  193.  
  194.  
  195. #define    GUARDSTR    "\n#include \"%s\"\nextern int yyerror;\n\
  196. extern int yycost;\nextern char * yymsg;\nextern YYSTYPE yyval;\n\n\
  197. yyguard(n, yyvsp, yylsp)\nregister int n;\nregister YYSTYPE *yyvsp;\n\
  198. register YYLTYPE *yylsp;\n\
  199. {\n  yyerror = 0;\nyycost = 0;\n  yymsg = 0;\nswitch (n)\n    {"
  200.  
  201. #define    ACTSTR        "\n#include \"%s\"\nextern YYSTYPE yyval;\
  202. \nextern int yychar;\
  203. yyaction(n, yyvsp, yylsp)\nregister int n;\nregister YYSTYPE *yyvsp;\n\
  204. register YYLTYPE *yylsp;\n{\n  switch (n)\n{"
  205.  
  206. #define    ACTSTR_SIMPLE    "\n  switch (yyn) {\n"
  207.  
  208. void
  209. output_before_read()
  210.   fprintf(ftable, "\n/*  A Bison++ parser, made from %s  */\n\n", infile);
  211.   fprintf(ftable, " /* with Bison++ version %s  */\n\n", version_string);
  212.   output_section(fparser,ftable);
  213.   if(definesflag) output_section(fhskel,fdefines);
  214. };
  215.  
  216. void
  217. output_headers()
  218. {
  219.   if(definesflag) output_section(fhskel,fdefines);
  220.   output_section(fparser,ftable);
  221.   if (pure_parser)
  222.     {
  223.      fprintf(ftable, "#define YY_%s_PURE 1\n",parser_name);
  224.      if(definesflag) fprintf(fdefines, "#define YY_%s_PURE 1\n\n",parser_name);
  225.     }
  226.   /* start writing the guard and action files, if they are needed.  */
  227.   if (semantic_parser)
  228.     fprintf(fguard, GUARDSTR, attrsfile);
  229.   fprintf(faction, (semantic_parser ? ACTSTR : ACTSTR_SIMPLE), attrsfile);
  230.   if(definesflag) output_section(fhskel,fdefines);
  231.   output_section(fparser,ftable);
  232.  
  233.  
  234.   /* Rename certain symbols if -p was specified.  */
  235.   if (spec_name_prefix)
  236.     {
  237.       fprintf(ftable, "#define YY_%s_PARSE %sparse\n",
  238.           parser_name, spec_name_prefix);
  239.       fprintf(ftable, "#define YY_%s_LEX %slex\n",
  240.           parser_name, spec_name_prefix);
  241.       fprintf(ftable, "#define YY_%s_ERROR %serror\n",
  242.           parser_name, spec_name_prefix);
  243.       fprintf(ftable, "#define YY_%s_LVAL %slval\n",
  244.           parser_name, spec_name_prefix);
  245.       fprintf(ftable, "#define YY_%s_CHAR %schar\n",
  246.           parser_name, spec_name_prefix);
  247.       fprintf(ftable, "#define YY_%s_DEBUG %sdebug\n",
  248.           parser_name, spec_name_prefix);
  249.     }
  250.   if (spec_name_prefix && definesflag)
  251.     {
  252.       fprintf(fdefines, "#define YY_%s_PARSE %sparse\n",
  253.           parser_name, spec_name_prefix);
  254.       fprintf(fdefines, "#define YY_%s_LEX %slex\n",
  255.           parser_name, spec_name_prefix);
  256.       fprintf(fdefines, "#define YY_%s_ERROR %serror\n",
  257.           parser_name, spec_name_prefix);
  258.       fprintf(fdefines, "#define YY_%s_LVAL %slval\n",
  259.           parser_name, spec_name_prefix);
  260.       fprintf(fdefines, "#define YY_%s_CHAR %schar\n",
  261.           parser_name, spec_name_prefix);
  262.       fprintf(fdefines, "#define YY_%s_DEBUG %sdebug\n",
  263.           parser_name, spec_name_prefix);
  264.     }
  265.  
  266. }
  267.  
  268.  
  269. void
  270. output_trailers()
  271. {
  272.   if(definesflag) output_section(fhskel,fdefines);
  273.   output_section(fparser,ftable);
  274.   /* output the definition of YYLTYPE into the fattrs and fdefines files.  */
  275.   if(debugflag)
  276.    {fprintf(ftable, 
  277.     "#define YY_%s_DEBUG %d\n"
  278.         ,parser_name,!!debugflag);
  279.     if (definesflag)
  280.      fprintf(fdefines, 
  281.       "#define YY_%s_DEBUG %d\n",
  282.             parser_name,!!debugflag);
  283.     }
  284.   if(definesflag) output_section(fhskel,fdefines);
  285.   output_section(fparser,ftable);
  286.   /* Now we know whether we need the line-number stack.
  287.      If we do, write its type into the .tab.h file.  */
  288.   if (yylsp_needed)
  289.     {
  290.      /* fattrs winds up in the .tab.c file, before bison.simple.  */
  291.       fprintf(ftable, "#define YYLSP_%s_NEEDED\n",parser_name);
  292.       if (debugflag)
  293.  
  294.       if (definesflag)
  295.     {
  296.          fprintf(fdefines, 
  297.              "#define YY_%s_LSP_NEEDED\n",
  298.               parser_name);
  299.         }
  300.     }
  301.   if (semantic_parser)
  302.     {
  303.       fprintf(fguard, "\n    }\n}\n");
  304.       fprintf(faction, "\n    }\n}\n");
  305.     }
  306.   else
  307.     fprintf(faction, "\n}\n");
  308. }
  309.  
  310.  
  311. void
  312. output()
  313. {
  314.   int c;
  315.  
  316.    if (!semantic_parser)        /* JF Put out other stuff */
  317.     {
  318.       rewind(fattrs);
  319.       while ((c=getc(fattrs))!=EOF)
  320.         putc(c,ftable);
  321.     }
  322.  
  323.   if (semantic_parser)
  324.     fprintf(ftable, "#include \"%s\"\n", attrsfile);
  325.  
  326.  
  327.   free_itemsets();
  328.   output_defines();
  329.   output_token_translations();
  330. /*   if (semantic_parser) */
  331.   /* This is now unconditional because debugging printouts can use it.  */
  332.   output_gram();
  333.   FREE(ritem);
  334.   if (semantic_parser)
  335.     output_stos();
  336.   output_rule_data();
  337.   output_actions();
  338.   output_parser();
  339.   output_program();
  340. }
  341.  
  342.  
  343. void
  344. output_token_translations()
  345. {
  346.   register int i, j;
  347. /*   register short *sp; JF unused */
  348.  
  349.   if (translations)
  350.     {
  351.       fprintf(ftable,
  352.           "\n#define YYTRANSLATE(x) ((unsigned)(x) <= %d ? yytranslate[x] : %d)\n",
  353.           max_user_token_number, nsyms);
  354.     
  355.       if (ntokens < 127)  /* play it very safe; check maximum element value.  */
  356.         fprintf(ftable, "\nstatic const char yytranslate[] = {     0");
  357.       else
  358.     fprintf(ftable, "\nstatic const short yytranslate[] = {     0");
  359.     
  360.       j = 10;
  361.       for (i = 1; i <= max_user_token_number; i++)
  362.     {
  363.       putc(',', ftable);
  364.     
  365.       if (j >= 10)
  366.         {
  367.           putc('\n', ftable);
  368.           j = 1;
  369.         }
  370.       else
  371.         {
  372.           j++;
  373.         }
  374.     
  375.       fprintf(ftable, "%6d", token_translations[i]);
  376.     }
  377.     
  378.       fprintf(ftable, "\n};\n");
  379.     }
  380.   else
  381.     {
  382.       fprintf(ftable, "\n#define YYTRANSLATE(x) (x)\n");
  383.     } 
  384. }
  385.  
  386.  
  387. void
  388. output_gram()
  389. {
  390.   register int i;
  391.   register int j;
  392.   register short *sp;
  393.  
  394.   /* With the ordinary parser,
  395.      yyprhs and yyrhs are needed only for yydebug.  */
  396.   if (!semantic_parser)
  397.     fprintf(ftable, "\n#if YY_%s_DEBUG != 0",parser_name);
  398.  
  399.   fprintf(ftable, "\nstatic const short yyprhs[] = {     0");
  400.  
  401.   j = 10;
  402.   for (i = 1; i <= nrules; i++)
  403.     {
  404.       putc(',', ftable);
  405.  
  406.       if (j >= 10)
  407.     {
  408.       putc('\n', ftable);
  409.       j = 1;
  410.     }
  411.       else
  412.     {
  413.       j++;
  414.     }
  415.  
  416.       fprintf(ftable, "%6d", rrhs[i]);
  417.     }
  418.  
  419.   fprintf(ftable, "\n};\n");
  420.  
  421.   fprintf(ftable, "\nstatic const short yyrhs[] = {%6d", ritem[0]);
  422.  
  423.   j = 10;
  424.   for (sp = ritem + 1; *sp; sp++)
  425.     {
  426.       putc(',', ftable);
  427.  
  428.       if (j >= 10)
  429.     {
  430.       putc('\n', ftable);
  431.       j = 1;
  432.     }
  433.       else
  434.     {
  435.       j++;
  436.     }
  437.  
  438.       if (*sp > 0)
  439.     fprintf(ftable, "%6d", *sp);
  440.       else
  441.     fprintf(ftable, "     0");
  442.     }
  443.  
  444.   fprintf(ftable, "\n};\n");
  445.   if(!semantic_parser)
  446.     fprintf(ftable, "\n#endif\n");
  447.  
  448. }
  449.  
  450.  
  451. void
  452. output_stos()
  453. {
  454.   register int i;
  455.   register int j;
  456.  
  457.   fprintf(ftable, "\nstatic const short yystos[] = {     0");
  458.  
  459.   j = 10;
  460.   for (i = 1; i < nstates; i++)
  461.     {
  462.       putc(',', ftable);
  463.  
  464.       if (j >= 10)
  465.     {
  466.       putc('\n', ftable);
  467.       j = 1;
  468.     }
  469.       else
  470.     {
  471.       j++;
  472.     }
  473.  
  474.       fprintf(ftable, "%6d", accessing_symbol[i]);
  475.     }
  476.  
  477.   fprintf(ftable, "\n};\n");
  478. }
  479.  
  480.  
  481. void
  482. output_rule_data()
  483. {
  484.   register int i;
  485.   register int j;
  486.  
  487.   fprintf(ftable, 
  488.      "\n#if YY_%s_DEBUG != 0\nstatic const short yyrline[] = { 0",parser_name);
  489.  
  490.   j = 10;
  491.   for (i = 1; i <= nrules; i++)
  492.     {
  493.       putc(',', ftable);
  494.  
  495.       if (j >= 10)
  496.     {
  497.       putc('\n', ftable);
  498.       j = 1;
  499.     }
  500.       else
  501.     {
  502.       j++;
  503.     }
  504.  
  505.       fprintf(ftable, "%6d", rline[i]);
  506.     }
  507.  
  508.   /* Output the table of symbol names.  */
  509.  
  510.   fprintf(ftable,
  511.           "\n};\n\nstatic const char * const yytname[] = {   \"%s\"",
  512.           tags[0]);
  513.  
  514.   j = strlen (tags[0]) + 44;
  515.   for (i = 1; i <= nsyms; i++)
  516.     {
  517.       register char *p;
  518.       putc(',', ftable);
  519.       j++;
  520.  
  521.       if (j > 75)
  522.     {
  523.       putc('\n', ftable);
  524.       j = 0;
  525.     }
  526.  
  527.       putc ('\"', ftable);
  528.       j++;
  529.  
  530.       for (p = tags[i]; p && *p; p++)
  531.     {
  532.       if (*p == '"' || *p == '\\')
  533.         {
  534.           fprintf(ftable, "\\%c", *p);
  535.           j += 2;
  536.         }
  537.       else if (*p == '\n')
  538.         {
  539.           fprintf(ftable, "\\n");
  540.           j += 2;
  541.         }
  542.       else if (*p == '\t')
  543.         {
  544.           fprintf(ftable, "\\t");
  545.           j += 2;
  546.         }
  547.       else if (*p == '\b')
  548.         {
  549.           fprintf(ftable, "\\b");
  550.           j += 2;
  551.         }
  552.       else if (*p < 040 || *p >= 0177)
  553.         {
  554.           fprintf(ftable, "\\%03o", *p);
  555.           j += 4;
  556.         }
  557.       else
  558.         {
  559.           putc(*p, ftable);
  560.           j++;
  561.         }
  562.     }
  563.  
  564.       putc ('\"', ftable);
  565.       j++;
  566.     }
  567.  
  568.   fprintf(ftable, "\n};\n#endif\n\nstatic const short yyr1[] = {     0");
  569.  
  570.   j = 10;
  571.   for (i = 1; i <= nrules; i++)
  572.     {
  573.       putc(',', ftable);
  574.  
  575.       if (j >= 10)
  576.     {
  577.       putc('\n', ftable);
  578.       j = 1;
  579.     }
  580.       else
  581.     {
  582.       j++;
  583.     }
  584.  
  585.       fprintf(ftable, "%6d", rlhs[i]);
  586.     }
  587.  
  588.   FREE(rlhs + 1);
  589.  
  590.   fprintf(ftable, "\n};\n\nstatic const short yyr2[] = {     0");
  591.  
  592.   j = 10;
  593.   for (i = 1; i < nrules; i++)
  594.     {
  595.       putc(',', ftable);
  596.  
  597.       if (j >= 10)
  598.     {
  599.       putc('\n', ftable);
  600.       j = 1;
  601.     }
  602.       else
  603.     {
  604.       j++;
  605.     }
  606.  
  607.       fprintf(ftable, "%6d", rrhs[i + 1] - rrhs[i] - 1);
  608.     }
  609.  
  610.   putc(',', ftable);
  611.   if (j >= 10)
  612.     putc('\n', ftable);
  613.  
  614.   fprintf(ftable, "%6d\n};\n", nitems - rrhs[nrules] - 1);
  615.   FREE(rrhs + 1);
  616. }
  617.  
  618.  
  619. void
  620. output_defines()
  621. {
  622.   fprintf(ftable, "\n\n#define\tYYFINAL\t\t%d\n", final_state);
  623.   fprintf(ftable, "#define\tYYFLAG\t\t%d\n", MINSHORT);
  624.   fprintf(ftable, "#define\tYYNTBASE\t%d\n", ntokens);
  625. }
  626.  
  627.  
  628.  
  629. /* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable and yycheck.  */
  630.  
  631. void
  632. output_actions()
  633. {
  634.   nvectors = nstates + nvars;
  635.  
  636.   froms = NEW2(nvectors, short *);
  637.   tos = NEW2(nvectors, short *);
  638.   tally = NEW2(nvectors, short);
  639.   width = NEW2(nvectors, short);
  640.  
  641.   token_actions();
  642.   free_shifts();
  643.   free_reductions();
  644.   FREE(lookaheads);
  645.   FREE(LA);
  646.   FREE(LAruleno);
  647.   FREE(accessing_symbol);
  648.  
  649.   goto_actions();
  650.   FREE(goto_map + ntokens);
  651.   FREE(from_state);
  652.   FREE(to_state);
  653.  
  654.   sort_actions();
  655.   pack_table();
  656.   output_base();
  657.   output_table();
  658.   output_check();
  659. }
  660.  
  661.  
  662.  
  663. /* figure out the actions for the specified state, indexed by lookahead token type.
  664.  
  665.    The yydefact table is output now.  The detailed info
  666.    is saved for putting into yytable later.  */
  667.  
  668. void
  669. token_actions()
  670. {
  671.   register int i;
  672.   register int j;
  673.   register int k;
  674.  
  675.   actrow = NEW2(ntokens, short);
  676.  
  677.   k = action_row(0);
  678.   fprintf(ftable, "\nstatic const short yydefact[] = {%6d", k);
  679.   save_row(0);
  680.  
  681.   j = 10;
  682.   for (i = 1; i < nstates; i++)
  683.     {
  684.       putc(',', ftable);
  685.  
  686.       if (j >= 10)
  687.     {
  688.       putc('\n', ftable);
  689.       j = 1;
  690.     }
  691.       else
  692.     {
  693.       j++;
  694.     }
  695.  
  696.       k = action_row(i);
  697.       fprintf(ftable, "%6d", k);
  698.       save_row(i);
  699.     }
  700.  
  701.   fprintf(ftable, "\n};\n");
  702.   FREE(actrow);
  703. }
  704.  
  705.  
  706.  
  707. /* Decide what to do for each type of token if seen as the lookahead token in specified state.
  708.    The value returned is used as the default action (yydefact) for the state.
  709.    In addition, actrow is filled with what to do for each kind of token,
  710.    index by symbol number, with zero meaning do the default action.
  711.    The value MINSHORT, a very negative number, means this situation
  712.    is an error.  The parser recognizes this value specially.
  713.  
  714.    This is where conflicts are resolved.  The loop over lookahead rules
  715.    considered lower-numbered rules last, and the last rule considered that likes
  716.    a token gets to handle it.  */
  717.  
  718. int
  719. action_row(state)
  720. int state;
  721. {
  722.   register int i;
  723.   register int j;
  724.   register int k;
  725.   register int m;
  726.   register int n;
  727.   register int count;
  728.   register int default_rule;
  729.   register int nreds;
  730.   register int max;
  731.   register int rule;
  732.   register int shift_state;
  733.   register int symbol;
  734.   register unsigned mask;
  735.   register unsigned *wordp;
  736.   register reductions *redp;
  737.   register shifts *shiftp;
  738.   register errs *errp;
  739.   int nodefault = 0;  /* set nonzero to inhibit having any default reduction */
  740.  
  741.   for (i = 0; i < ntokens; i++)
  742.     actrow[i] = 0;
  743.  
  744.   default_rule = 0;
  745.   nreds = 0;
  746.   redp = reduction_table[state];
  747.  
  748.   if (redp)
  749.     {
  750.       nreds = redp->nreds;
  751.  
  752.       if (nreds >= 1)
  753.     {
  754.       /* loop over all the rules available here which require lookahead */
  755.       m = lookaheads[state];
  756.       n = lookaheads[state + 1];
  757.  
  758.       for (i = n - 1; i >= m; i--)
  759.         {
  760.           rule = - LAruleno[i];
  761.           wordp = LA + i * tokensetsize;
  762.           mask = 1;
  763.  
  764.           /* and find each token which the rule finds acceptable to come next */
  765.           for (j = 0; j < ntokens; j++)
  766.         {
  767.           /* and record this rule as the rule to use if that token follows.  */
  768.           if (mask & *wordp)
  769.             actrow[j] = rule;
  770.  
  771.           mask <<= 1;
  772.           if (mask == 0)
  773.             {
  774.               mask = 1;
  775.               wordp++;
  776.             }
  777.         }
  778.         }
  779.     }
  780.     }
  781.  
  782.   shiftp = shift_table[state];
  783.  
  784.   /* now see which tokens are allowed for shifts in this state.
  785.      For them, record the shift as the thing to do.  So shift is preferred to reduce.  */
  786.  
  787.   if (shiftp)
  788.     {
  789.       k = shiftp->nshifts;
  790.  
  791.       for (i = 0; i < k; i++)
  792.     {
  793.       shift_state = shiftp->shifts[i];
  794.       if (! shift_state) continue;
  795.  
  796.       symbol = accessing_symbol[shift_state];
  797.  
  798.       if (ISVAR(symbol))
  799.         break;
  800.  
  801.       actrow[symbol] = shift_state;
  802.  
  803.       /* do not use any default reduction if there is a shift for error */
  804.  
  805.       if (symbol == error_token_number) nodefault = 1;
  806.     }
  807.     }
  808.  
  809.   errp = err_table[state];
  810.  
  811.   /* See which tokens are an explicit error in this state
  812.      (due to %nonassoc).  For them, record MINSHORT as the action.  */
  813.  
  814.   if (errp)
  815.     {
  816.       k = errp->nerrs;
  817.  
  818.       for (i = 0; i < k; i++)
  819.     {
  820.       symbol = errp->errs[i];
  821.       actrow[symbol] = MINSHORT;
  822.     }
  823.     }
  824.  
  825.   /* now find the most common reduction and make it the default action for this state.  */
  826.  
  827.   if (nreds >= 1 && ! nodefault)
  828.     {
  829.       if (consistent[state])
  830.     default_rule = redp->rules[0];
  831.       else
  832.     {
  833.       max = 0;
  834.       for (i = m; i < n; i++)
  835.         {
  836.           count = 0;
  837.           rule = - LAruleno[i];
  838.     
  839.           for (j = 0; j < ntokens; j++)
  840.         {
  841.           if (actrow[j] == rule)
  842.             count++;
  843.         }
  844.     
  845.           if (count > max)
  846.         {
  847.           max = count;
  848.           default_rule = rule;
  849.         }
  850.         }
  851.     
  852.       /* actions which match the default are replaced with zero,
  853.          which means "use the default" */
  854.     
  855.       if (max > 0)
  856.         {
  857.           for (j = 0; j < ntokens; j++)
  858.         {
  859.           if (actrow[j] == default_rule)
  860.             actrow[j] = 0;
  861.         }
  862.     
  863.           default_rule = - default_rule;
  864.         }
  865.     }
  866.     }
  867.  
  868.   /* If have no default rule, the default is an error.
  869.      So replace any action which says "error" with "use default".  */
  870.  
  871.   if (default_rule == 0)
  872.     for (j = 0; j < ntokens; j++)
  873.       {
  874.     if (actrow[j] == MINSHORT)
  875.       actrow[j] = 0;
  876.       }
  877.  
  878.   return (default_rule);
  879. }
  880.  
  881.  
  882. void
  883. save_row(state)
  884. int state;
  885. {
  886.   register int i;
  887.   register int count;
  888.   register short *sp;
  889.   register short *sp1;
  890.   register short *sp2;
  891.  
  892.   count = 0;
  893.   for (i = 0; i < ntokens; i++)
  894.     {
  895.       if (actrow[i] != 0)
  896.     count++;
  897.     }
  898.  
  899.   if (count == 0)
  900.     return;
  901.  
  902.   froms[state] = sp1 = sp = NEW2(count, short);
  903.   tos[state] = sp2 = NEW2(count, short);
  904.  
  905.   for (i = 0; i < ntokens; i++)
  906.     {
  907.       if (actrow[i] != 0)
  908.     {
  909.       *sp1++ = i;
  910.       *sp2++ = actrow[i];
  911.     }
  912.     }
  913.  
  914.   tally[state] = count;
  915.   width[state] = sp1[-1] - sp[0] + 1;
  916. }
  917.  
  918.  
  919.  
  920. /* figure out what to do after reducing with each rule,
  921.    depending on the saved state from before the beginning
  922.    of parsing the data that matched this rule.
  923.  
  924.    The yydefgoto table is output now.  The detailed info
  925.    is saved for putting into yytable later.  */
  926.  
  927. void
  928. goto_actions()
  929. {
  930.   register int i;
  931.   register int j;
  932.   register int k;
  933.  
  934.   state_count = NEW2(nstates, short);
  935.  
  936.   k = default_goto(ntokens);
  937.   fprintf(ftable, "\nstatic const short yydefgoto[] = {%6d", k);
  938.   save_column(ntokens, k);
  939.  
  940.   j = 10;
  941.   for (i = ntokens + 1; i < nsyms; i++)
  942.     {
  943.       putc(',', ftable);
  944.  
  945.       if (j >= 10)
  946.     {
  947.       putc('\n', ftable);
  948.       j = 1;
  949.     }
  950.       else
  951.     {
  952.       j++;
  953.     }
  954.  
  955.       k = default_goto(i);
  956.       fprintf(ftable, "%6d", k);
  957.       save_column(i, k);
  958.     }
  959.  
  960.   fprintf(ftable, "\n};\n");
  961.   FREE(state_count);
  962. }
  963.  
  964.  
  965.  
  966. int
  967. default_goto(symbol)
  968. int symbol;
  969. {
  970.   register int i;
  971.   register int m;
  972.   register int n;
  973.   register int default_state;
  974.   register int max;
  975.  
  976.   m = goto_map[symbol];
  977.   n = goto_map[symbol + 1];
  978.  
  979.   if (m == n)
  980.     return (-1);
  981.  
  982.   for (i = 0; i < nstates; i++)
  983.     state_count[i] = 0;
  984.  
  985.   for (i = m; i < n; i++)
  986.     state_count[to_state[i]]++;
  987.  
  988.   max = 0;
  989.   default_state = -1;
  990.  
  991.   for (i = 0; i < nstates; i++)
  992.     {
  993.       if (state_count[i] > max)
  994.     {
  995.       max = state_count[i];
  996.       default_state = i;
  997.     }
  998.     }
  999.  
  1000.   return (default_state);
  1001. }
  1002.  
  1003.  
  1004. void
  1005. save_column(symbol, default_state)
  1006. int symbol;
  1007. int default_state;
  1008. {
  1009.   register int i;
  1010.   register int m;
  1011.   register int n;
  1012.   register short *sp;
  1013.   register short *sp1;
  1014.   register short *sp2;
  1015.   register int count;
  1016.   register int symno;
  1017.  
  1018.   m = goto_map[symbol];
  1019.   n = goto_map[symbol + 1];
  1020.  
  1021.   count = 0;
  1022.   for (i = m; i < n; i++)
  1023.     {
  1024.       if (to_state[i] != default_state)
  1025.     count++;
  1026.     }
  1027.  
  1028.   if (count == 0)
  1029.     return;
  1030.  
  1031.   symno = symbol - ntokens + nstates;
  1032.  
  1033.   froms[symno] = sp1 = sp = NEW2(count, short);
  1034.   tos[symno] = sp2 = NEW2(count, short);
  1035.  
  1036.   for (i = m; i < n; i++)
  1037.     {
  1038.       if (to_state[i] != default_state)
  1039.     {
  1040.       *sp1++ = from_state[i];
  1041.       *sp2++ = to_state[i];
  1042.     }
  1043.     }
  1044.  
  1045.   tally[symno] = count;
  1046.   width[symno] = sp1[-1] - sp[0] + 1;
  1047. }
  1048.  
  1049.  
  1050.  
  1051. /* the next few functions decide how to pack 
  1052.    the actions and gotos information into yytable. */
  1053.  
  1054. void
  1055. sort_actions()
  1056. {
  1057.   register int i;
  1058.   register int j;
  1059.   register int k;
  1060.   register int t;
  1061.   register int w;
  1062.  
  1063.   order = NEW2(nvectors, short);
  1064.   nentries = 0;
  1065.  
  1066.   for (i = 0; i < nvectors; i++)
  1067.     {
  1068.       if (tally[i] > 0)
  1069.     {
  1070.       t = tally[i];
  1071.       w = width[i];
  1072.       j = nentries - 1;
  1073.  
  1074.       while (j >= 0 && (width[order[j]] < w))
  1075.         j--;
  1076.  
  1077.       while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
  1078.         j--;
  1079.  
  1080.       for (k = nentries - 1; k > j; k--)
  1081.         order[k + 1] = order[k];
  1082.  
  1083.       order[j + 1] = i;
  1084.       nentries++;
  1085.     }
  1086.     }
  1087. }
  1088.  
  1089.  
  1090. void
  1091. pack_table()
  1092. {
  1093.   register int i;
  1094.   register int place;
  1095.   register int state;
  1096.  
  1097.   base = NEW2(nvectors, short);
  1098.   pos = NEW2(nentries, short);
  1099.   table = NEW2(MAXTABLE, short);
  1100.   check = NEW2(MAXTABLE, short);
  1101.  
  1102.   lowzero = 0;
  1103.   high = 0;
  1104.  
  1105.   for (i = 0; i < nvectors; i++)
  1106.     base[i] = MINSHORT;
  1107.  
  1108.   for (i = 0; i < MAXTABLE; i++)
  1109.     check[i] = -1;
  1110.  
  1111.   for (i = 0; i < nentries; i++)
  1112.     {
  1113.       state = matching_state(i);
  1114.  
  1115.       if (state < 0)
  1116.     place = pack_vector(i);
  1117.       else
  1118.     place = base[state];
  1119.  
  1120.       pos[i] = place;
  1121.       base[order[i]] = place;
  1122.     }
  1123.  
  1124.   for (i = 0; i < nvectors; i++)
  1125.     {
  1126.       if (froms[i])
  1127.     FREE(froms[i]);
  1128.       if (tos[i])
  1129.     FREE(tos[i]);
  1130.     }
  1131.  
  1132.   FREE(froms);
  1133.   FREE(tos);
  1134.   FREE(pos);
  1135. }
  1136.  
  1137.  
  1138.  
  1139. int
  1140. matching_state(vector)
  1141. int vector;
  1142. {
  1143.   register int i;
  1144.   register int j;
  1145.   register int k;
  1146.   register int t;
  1147.   register int w;
  1148.   register int match;
  1149.   register int prev;
  1150.  
  1151.   i = order[vector];
  1152.   if (i >= nstates)
  1153.     return (-1);
  1154.  
  1155.   t = tally[i];
  1156.   w = width[i];
  1157.  
  1158.   for (prev = vector - 1; prev >= 0; prev--)
  1159.     {
  1160.       j = order[prev];
  1161.       if (width[j] != w || tally[j] != t)
  1162.     return (-1);
  1163.  
  1164.       match = 1;
  1165.       for (k = 0; match && k < t; k++)
  1166.     {
  1167.       if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
  1168.         match = 0;
  1169.     }
  1170.  
  1171.       if (match)
  1172.     return (j);
  1173.     }
  1174.  
  1175.   return (-1);
  1176. }
  1177.  
  1178.  
  1179.  
  1180. int
  1181. pack_vector(vector)
  1182. int vector;
  1183. {
  1184.   register int i;
  1185.   register int j;
  1186.   register int k;
  1187.   register int t;
  1188.   register int loc;
  1189.   register int ok;
  1190.   register short *from;
  1191.   register short *to;
  1192.  
  1193.   i = order[vector];
  1194.   t = tally[i];
  1195.  
  1196.   if (t == 0)
  1197.     berror("pack_vector");
  1198.  
  1199.   from = froms[i];
  1200.   to = tos[i];
  1201.  
  1202.   for (j = lowzero - from[0]; j < MAXTABLE; j++)
  1203.     {
  1204.       ok = 1;
  1205.  
  1206.       for (k = 0; ok && k < t; k++)
  1207.     {
  1208.       loc = j + from[k];
  1209.       if (loc > MAXTABLE)
  1210.         fatals("maximum table size (%d) exceeded",MAXTABLE);
  1211.  
  1212.       if (table[loc] != 0)
  1213.         ok = 0;
  1214.     }
  1215.  
  1216.       for (k = 0; ok && k < vector; k++)
  1217.     {
  1218.       if (pos[k] == j)
  1219.         ok = 0;
  1220.     }
  1221.  
  1222.       if (ok)
  1223.     {
  1224.       for (k = 0; k < t; k++)
  1225.         {
  1226.           loc = j + from[k];
  1227.           table[loc] = to[k];
  1228.           check[loc] = from[k];
  1229.         }
  1230.  
  1231.       while (table[lowzero] != 0)
  1232.         lowzero++;
  1233.  
  1234.       if (loc > high)
  1235.         high = loc;
  1236.  
  1237.       return (j);
  1238.     }
  1239.     }
  1240.  
  1241.   berror("pack_vector");
  1242.   return 0;    /* JF keep lint happy */
  1243. }
  1244.  
  1245.  
  1246.  
  1247. /* the following functions output yytable, yycheck
  1248.    and the vectors whose elements index the portion starts */
  1249.  
  1250. void
  1251. output_base()
  1252. {
  1253.   register int i;
  1254.   register int j;
  1255.  
  1256.   fprintf(ftable, "\nstatic const short yypact[] = {%6d", base[0]);
  1257.  
  1258.   j = 10;
  1259.   for (i = 1; i < nstates; i++)
  1260.     {
  1261.       putc(',', ftable);
  1262.  
  1263.       if (j >= 10)
  1264.     {
  1265.       putc('\n', ftable);
  1266.       j = 1;
  1267.     }
  1268.       else
  1269.     {
  1270.       j++;
  1271.     }
  1272.  
  1273.       fprintf(ftable, "%6d", base[i]);
  1274.     }
  1275.  
  1276.   fprintf(ftable, "\n};\n\nstatic const short yypgoto[] = {%6d", base[nstates]);
  1277.  
  1278.   j = 10;
  1279.   for (i = nstates + 1; i < nvectors; i++)
  1280.     {
  1281.       putc(',', ftable);
  1282.  
  1283.       if (j >= 10)
  1284.     {
  1285.       putc('\n', ftable);
  1286.       j = 1;
  1287.     }
  1288.       else
  1289.     {
  1290.       j++;
  1291.     }
  1292.  
  1293.       fprintf(ftable, "%6d", base[i]);
  1294.     }
  1295.  
  1296.   fprintf(ftable, "\n};\n");
  1297.   FREE(base);
  1298. }
  1299.  
  1300.  
  1301. void
  1302. output_table()
  1303. {
  1304.   register int i;
  1305.   register int j;
  1306.  
  1307.   fprintf(ftable, "\n\n#define\tYYLAST\t\t%d\n\n", high);
  1308.   fprintf(ftable, "\nstatic const short yytable[] = {%6d", table[0]);
  1309.  
  1310.   j = 10;
  1311.   for (i = 1; i <= high; i++)
  1312.     {
  1313.       putc(',', ftable);
  1314.  
  1315.       if (j >= 10)
  1316.     {
  1317.       putc('\n', ftable);
  1318.       j = 1;
  1319.     }
  1320.       else
  1321.     {
  1322.       j++;
  1323.     }
  1324.  
  1325.       fprintf(ftable, "%6d", table[i]);
  1326.     }
  1327.  
  1328.   fprintf(ftable, "\n};\n");
  1329.   FREE(table);
  1330. }
  1331.  
  1332.  
  1333. void
  1334. output_check()
  1335. {
  1336.   register int i;
  1337.   register int j;
  1338.  
  1339.   fprintf(ftable, "\nstatic const short yycheck[] = {%6d", check[0]);
  1340.  
  1341.   j = 10;
  1342.   for (i = 1; i <= high; i++)
  1343.     {
  1344.       putc(',', ftable);
  1345.  
  1346.       if (j >= 10)
  1347.     {
  1348.       putc('\n', ftable);
  1349.       j = 1;
  1350.     }
  1351.       else
  1352.     {
  1353.       j++;
  1354.     }
  1355.  
  1356.       fprintf(ftable, "%6d", check[i]);
  1357.     }
  1358.  
  1359.   fprintf(ftable, "\n};\n");
  1360.   FREE(check);
  1361. }
  1362.  
  1363.  
  1364.  
  1365. /* copy the parser code into the ftable file at the end.  */
  1366.  
  1367. void
  1368.  output_parser()
  1369. {
  1370.  register int c;
  1371.  output_section(fparser,ftable);
  1372.  rewind(faction);
  1373.  for(c=getc(faction);c!=EOF;c=getc(faction))
  1374.         putc(c,ftable);
  1375.  output_section(fparser,ftable);
  1376. }
  1377. void
  1378. output_section(fin,fout)
  1379. FILE *fin,*fout;
  1380. {
  1381.   register int c;
  1382.   int dummy;
  1383.   int *pcounter=&dummy;
  1384.   char *fil_name;
  1385.   fil_name="?";
  1386.   if(fin==fparser) 
  1387.    {pcounter=&line_fparser;fil_name=parser_fname;}
  1388.   else if(fin==fhskel) 
  1389.    {pcounter=&line_fhskel;fil_name=hskel_fname;}
  1390.   /* Loop over lines in the standard parser file.  */
  1391.   if (!nolinesflag)
  1392.     fprintf(fout, "\n#line %d \"%s\"\n", (*pcounter), quoted_filename(fil_name));
  1393.  
  1394.   while (1)
  1395.     {
  1396.       
  1397.  
  1398.       /* now write out the line... */
  1399.       for ( c = getc(fin); c != '\n' && c != EOF; c = getc(fin))
  1400.       {if (c == '$')
  1401.         {
  1402.           if (!nolinesflag)
  1403.            {fprintf(fout,
  1404.                 "\n/* #line %d \"%s\" */\n#line @\n",
  1405.          (*pcounter), quoted_filename(fil_name));
  1406.                 }
  1407.           return;
  1408.         }
  1409.            else if(c=='@')
  1410.         {fprintf(fout,"%s",parser_name);
  1411.         }
  1412.        else
  1413.         putc(c, fout);
  1414.           }
  1415.       if (c == EOF)
  1416.     break;
  1417.       else if(c=='\n') (*pcounter)++;
  1418.       putc(c, fout);
  1419.     }
  1420. }
  1421.  
  1422.  
  1423. void
  1424. output_program()
  1425. {
  1426.   register int c;
  1427.   extern int lineno;
  1428.   int is_escaped=0,is_commented=0;
  1429.   char quoted='\0',last='\0';
  1430.   int len_match=0,i;
  1431.   char *match_open="%header{";
  1432.   char *match_close="%}";
  1433.   char *match_wait=match_open;
  1434.   if (!nolinesflag)
  1435.     fprintf(ftable, "#line %d \"%s\"\n", lineno, quoted_filename(infile));
  1436.  
  1437.   
  1438.   for (c = getc(finput);c != EOF;last=c,c = getc(finput))
  1439.     {
  1440.      if(!match_wait[len_match])
  1441.        {if(match_wait==match_open)
  1442.          {match_wait=match_close;
  1443.           if (!nolinesflag && definesflag)
  1444.        fprintf(fdefines, "\n#line %d \"%s\"\n", lineno, quoted_filename(infile));
  1445.  
  1446.          }
  1447.         else 
  1448.          {match_wait=match_open;}
  1449.         len_match=0;
  1450.        }
  1451.      else if(c!=match_wait[len_match] || is_escaped || is_commented || quoted)
  1452.        {for(i=0;i<len_match;i++)
  1453.           {if(match_wait==match_close && definesflag)
  1454.              putc(match_wait[i],fdefines);
  1455.            putc(match_wait[i],ftable);}
  1456.          len_match=0;
  1457.        };
  1458.      if(c==match_wait[len_match] && !is_escaped && !is_commented && !quoted) 
  1459.        {len_match++;}
  1460.      else
  1461.        {if(match_wait==match_close && definesflag)
  1462.           putc(c,fdefines);
  1463.         putc(c,ftable);
  1464.        }
  1465.      if(c=='\n') lineno++;
  1466.      if(is_escaped)
  1467.        {is_escaped=0;}
  1468.      else if(c=='\\') 
  1469.          {is_escaped=1;}
  1470.      else if(is_commented==1) 
  1471.        {if(last=='*' && c=='/') 
  1472.          is_commented=0;}
  1473.      else if(is_commented==2) 
  1474.        {if(c=='\n') 
  1475.          is_commented=0;}
  1476.      else if((c=='"'|| c== '\'')) 
  1477.          {if(!quoted) quoted=c;
  1478.           else if(quoted==c) quoted='\0';
  1479.          }
  1480.      else if(quoted) {}
  1481.      else if(last=='/' && c=='*') is_commented=1;
  1482.      else if(last=='/' && c=='/') is_commented=2;
  1483.      
  1484.       
  1485.     }
  1486. }
  1487.  
  1488.  
  1489. void
  1490. free_itemsets()
  1491. {
  1492.   register core *cp,*cptmp;
  1493.  
  1494.   FREE(state_table);
  1495.  
  1496.   for (cp = first_state; cp; cp = cptmp) {
  1497.     cptmp=cp->next;
  1498.     FREE(cp);
  1499.   }
  1500. }
  1501.  
  1502.  
  1503. void
  1504. free_shifts()
  1505. {
  1506.   register shifts *sp,*sptmp;/* JF derefrenced freed ptr */
  1507.  
  1508.   FREE(shift_table);
  1509.  
  1510.   for (sp = first_shift; sp; sp = sptmp) {
  1511.     sptmp=sp->next;
  1512.     FREE(sp);
  1513.   }
  1514. }
  1515.  
  1516.  
  1517. void
  1518. free_reductions()
  1519. {
  1520.   register reductions *rp,*rptmp;/* JF fixed freed ptr */
  1521.  
  1522.   FREE(reduction_table);
  1523.  
  1524.   for (rp = first_reduction; rp; rp = rptmp) {
  1525.     rptmp=rp->next;
  1526.     FREE(rp);
  1527.   }
  1528. }
  1529. void output_token_defines();
  1530. void output_token_const_def();
  1531. void output_token_const_decl();
  1532.  
  1533. void output_about_token()
  1534. { register int i;
  1535.  
  1536.   output_section(fparser,ftable);
  1537.   output_token_defines(ftable);
  1538.   output_section(fparser,ftable);
  1539.   output_token_const_decl(ftable);
  1540.   output_section(fparser,ftable);  /* new section */
  1541.   output_token_enum(ftable); /* enum */
  1542.   output_section(fparser,ftable);
  1543.   output_token_const_def(ftable);
  1544.   output_section(fparser,ftable);
  1545.   if (definesflag)
  1546.     {
  1547.       output_section(fhskel,fdefines);
  1548.       output_token_defines(fdefines);
  1549.       output_section(fhskel,fdefines);
  1550.       output_token_const_decl(fdefines);
  1551.       output_section(fhskel,fdefines);  /* new section */
  1552.       output_token_enum(fdefines); /* enum */
  1553.       output_section(fhskel,fdefines);
  1554.       if (semantic_parser)
  1555.     for (i = ntokens; i < nsyms; i++)
  1556.       {
  1557.         /* don't make these for dummy nonterminals made by gensym.  */
  1558.         if (*tags[i] != '@')
  1559.           fprintf(fdefines, "#define\tNT%s\t%d\n", tags[i], i);
  1560.       }
  1561.     }
  1562.  
  1563. };
  1564. void output_token_defines(file)
  1565. FILE *file;
  1566. {output_token_defines_fmt(file,"#define\t%s\t%d\n",0);
  1567.  if (semantic_parser)
  1568.   output_token_defines_fmt(file,"#define\tT%s\t%d\n",1);
  1569. };
  1570. void output_token_const_def(file)
  1571. FILE *file;
  1572. {char line[256];
  1573.  sprintf(line,"const int YY_%s_CLASS::%%s=%%d;\n",parser_name);
  1574.  output_token_defines_fmt(file,line,0);
  1575.  sprintf(line,"const int YY_%s_CLASS::T%%s=%%d;\n",parser_name);
  1576.  if (semantic_parser)
  1577.   output_token_defines_fmt(file,line,1);
  1578. };
  1579. void output_token_const_decl(file)
  1580. FILE *file;
  1581. {char line[256];
  1582.  output_token_defines_fmt(file,"static const int %s;\n",0);
  1583.  if (semantic_parser)
  1584.   output_token_defines_fmt(file,"static const int T%s;\n",1);
  1585. };
  1586. /* create a list like
  1587.     ,FIRST_TOKEN=256
  1588.     ,SECOND_TOKEN=257
  1589. */
  1590. void output_token_enum(file)
  1591. FILE *file;
  1592. {
  1593.  output_token_defines_fmt(file,"\t,%s=%d\n",0);
  1594.  if (semantic_parser) /* just for compatibility with semantic parser */
  1595.   output_token_defines_fmt(file,"\t,T%s=%d\n",1);
  1596. };
  1597.  
  1598.  
  1599. void
  1600. output_token_defines_fmt(file,fmt,notrans)
  1601. FILE *file;
  1602. char *fmt;
  1603. int notrans;
  1604. {
  1605.   bucket *bp;
  1606.  
  1607.   for (bp = firstsymbol; bp; bp = bp->next)
  1608.     {
  1609.       if (bp->value >= ntokens) continue;
  1610.  
  1611.       /* For named tokens, but not literal ones, define the name.  */
  1612.       /* The value is the user token number.  */
  1613.  
  1614.       if ('\'' != *tags[bp->value] && bp != errtoken)
  1615.     {
  1616.       register char *cp = tags[bp->value];
  1617.       register char c;
  1618.  
  1619.       /* Don't #define nonliteral tokens whose names contain periods.  */
  1620.  
  1621.       while ((c = *cp++) && c != '.');
  1622.       if (!c)
  1623.         {
  1624.               fprintf(file, fmt, tags[bp->value],
  1625.                 (translations && !notrans ? bp->user_token_number : bp->value));
  1626.         }
  1627.     }
  1628.     }
  1629.  
  1630.   putc('\n', file);
  1631. }
  1632.  
  1633.  
  1634. char *quoted_filename(f)
  1635. char *f;
  1636. {
  1637.  static char *buffer=NULL;
  1638.  static int buff_size=0;
  1639.  char *p;
  1640.  if(buff_size<strlen(f)*2+1)
  1641.   {
  1642.    if(buffer !=NULL ) free(buffer);
  1643.    buffer=xmalloc(strlen(f)*2+1);
  1644.   }
  1645.  for(p=buffer;*f;f++)
  1646.   {if(*f=='\\')
  1647.     *p++ ='\\';
  1648.    *p++ =*f;
  1649.   }
  1650.  *p++='\0';
  1651.  return buffer;
  1652. }
  1653.