home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / alde_c / misc / util / lex / out1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1983-12-26  |  11.7 KB  |  334 lines

  1. /*
  2.  * Copyright (c) 1978 Charles H. Forsyth
  3.  *
  4.  * Modified 02-Dec-80 Bob Denny -- Conditionalize debug code for smaller size
  5.  *                           01 -- Removed ending() function code from here
  6.  *                                  to lex.c, so ytab.c code could share the
  7.  *                                  same overlay region as this module.
  8.  *                           02 -- Removed nfaprint(), llactr(), newcase(),
  9.  *                                  cclprint(), chprint() and setline(),
  10.  *                                  the rest of this can share an overlay.
  11.  *                                  They're in 'out2.c'. This is now 'out1.c'.
  12.  *          29-May-81 Bob Denny -- More extern hacking for RSX overlaying.
  13.  *          19-Mar-82 Bob Denny -- New compiler and library
  14.  *          03-May-82 Bob Denny -- Final touches, remove unreferenced autos
  15.  *          28-Aug-82 Bob Denny -- Put "=" into table initializers to make
  16.  *                                  new compiler happy. Add "-s" code to
  17.  *                                  supress "#include <stdio.h>" in output.
  18.  *                                  Tables output 8 values/line instead of
  19.  *                                  16.  Overran R.H. edge on 3 digit octals.
  20.  *                                  Change output format for readability.
  21.  *          31-Aug-82 Bob Denny -- Add lexswitch( ...) to llstin so table
  22.  *                                  name selected by -t switch is automatically
  23.  *                                  switched-to at yylex() startup time.  Removed
  24.  *                                  hard reference to "lextab" from yylex();
  25.  *                                  This module generates extern declaration
  26.  *                                  for forward reference.
  27.  *          14-Apr-83 Bob Denny -- Add VAX11C support.  Remove usage of remote
  28.  *                                  formats (damn!) not supported on VAX-11 C.
  29.  *                                 Use "short int" for 16-bit table items under
  30.  *                                  VAX-11 C to save size.
  31.  *                                 Contitional out flaky debug code.  Probably
  32.  *                                  related to non-functional minimization.
  33.  *            20-Nov-83 Scott Guthery -- Adapt for IBM PC & DeSmet C
  34.  *            26-Dec-83 Scott Guthery -- Removed "extern FILE lexin" from llstin
  35.  *                                       code and put it lex.h.
  36.  */
  37.  
  38. /*
  39.  * lex -- output human- and machine-readable tables
  40.  */
  41.  
  42. #include <stdio.h>
  43. #include "lexlex.h"
  44.  
  45. extern char *ignore;
  46. extern char *illeg;
  47. extern char *breakc;
  48. extern int nlook;
  49.  
  50. char strdec1[] = "\nstruct lextab %s =\t{\n";
  51. char strdec2[] = "\t\t\t%d,\t\t/* Highest state */\n";
  52. char strdec3[] = "\t\t\t_D%s\t/* --> \"Default state\" table */\n";
  53. char strdec4[] = "\t\t\t_N%s\t/* --> \"Next state\" table */\n";
  54. char strdec5[] = "\t\t\t_C%s\t/* --> \"Check value\" table */\n";
  55. char strdec6[] = "\t\t\t_B%s\t/* --> \"Base\" table */\n";
  56. char strdec7[] = "\t\t\t%d,\t\t/* Index of last entry in \"next\" */\n";
  57. char strdec8[] = "\t\t\t%s,\t\t/* --> Byte-int move routine */\n";
  58. char strdec9[] = "\t\t\t_F%s\t/* --> \"Final state\" table */\n";
  59. char strdec10[] = "\t\t\t_A%s\t/* --> Action routine */\n";
  60. char strdec11[] = "\n\t\t\t%s%s\t/* Look-ahead vector */\n";
  61.  
  62.  
  63. char ptabnam[] = { "         " };
  64.  
  65. /*
  66.  * Print the minimised DFA, and at the same time, construct the vector which
  67.  * indicates final states by associating them with their translation index.
  68.  * (DFA printout supressed ifndef DEBUG.)
  69.  */
  70. dfaprint()
  71. {
  72.         register struct move *dp;
  73.         register struct dfa *st;
  74.         register i;
  75.         int fi, k, l;
  76.  
  77.         vstart("LL16BIT _F%s", tabname);
  78. #ifdef DEBUG
  79.         fprintf(lexlog, "\nMinimised DFA for complete syntax\n");
  80. #endif
  81.         for (i = 0; i < ndfa; i++) {
  82. #ifdef DEBUG
  83.                 fprintf(lexlog, "\nstate %d", i);
  84. #endif
  85.                 st = &dfa[i];
  86.                 k = -1;
  87.                 if (fi = st->df_name->s_final) {
  88.                         k = nfa[fi].n_trans - trans;
  89. #ifdef DEBUG
  90.                         fprintf(lexlog, " (final %d[%d])", fi, k);
  91. #endif
  92.                         if (nfa[fi].n_flag&FLOOK) {
  93.                                 k |= (nfa[fi].n_look+1)<<11;
  94. #ifdef DEBUG
  95.                                 fprintf(lexlog, " restore %d", nfa[fi].n_look);
  96. #endif
  97.                         }
  98.                 }
  99.                 if (l = st->df_name->s_look)
  100. #ifdef DEBUG
  101.                         fprintf(lexlog, " look-ahead %o", l);
  102. #else
  103.                         ;
  104. #endif
  105.                 vel(" %d,", k);
  106. #ifdef MINIM_OK
  107.                 k = st->df_name->s_group->s_els[0];
  108.                 if (k!=i) {
  109.                         fprintf(lexlog, " deleted\n");
  110.                         continue;
  111.                 }
  112.  
  113.                 fprintf(lexlog, "\n");
  114.                 for (dp = st->df_base; dp < st->df_max; dp = range(st, dp))
  115.                 if (st->df_default)
  116.                       fprintf(lexlog, "\t.\tsame as %d\n", st->df_default-dfa);
  117. #endif
  118.         }
  119.         vel(" %d,", -1);        /* blocking state */
  120.  
  121.         vend();
  122. }
  123.  
  124. #ifdef MINIM_OK
  125.  
  126. range(st, dp)
  127. register struct dfa *st;
  128. register struct move *dp;
  129. {
  130.         int low, high, last;
  131.         struct set *s;
  132.         register a;
  133.  
  134.         while (dp < st->df_max && dp->m_check!=st)
  135.                 dp++;
  136. /***************************************************
  137.  * This always returns given the above statement ! *
  138.  ***************************************************/
  139.         if (dp >= st->df_max)
  140.                 return(dp);
  141.  
  142.         low = dp - st->df_base;
  143. /*
  144.         s = dp->m_next->s_group->s_els[0];
  145. */
  146.         s = dp->m_next;
  147.         for (last = low-1; dp < st->df_max &&
  148.                            dp->m_check==st &&
  149.                            (a = dp - st->df_base)==last+1 &&
  150. /*
  151.                            dp->m_next->s_state->s_els[0]==s; dp++)
  152. */
  153.                            dp->m_next==s; dp++)
  154.                 last = a;
  155.         high = last;
  156.         fprintf(lexlog, "\t");
  157.         if (high==low)
  158.                 chprint(low);
  159.         else {
  160.                 fprintf(lexlog, "[");
  161.                 if (high-low > 4) {
  162.                         chprint(low);
  163.                         fprintf(lexlog, "-");
  164.                         chprint(high);
  165.                 } else {
  166.                         while (low<=high)
  167.                                 chprint(low++);
  168.                 }
  169.                 fprintf(lexlog, "]");
  170.         }
  171.         if (s->s_state==NULL)
  172.                 fprintf(lexlog, "\tNULL\n"); else
  173.                 fprintf(lexlog, "\t%d\n", s->s_state-dfa);
  174.         return(dp);
  175. }
  176. #endif
  177.  
  178. heading()
  179. {
  180.         char *ver;
  181.         fprintf(llout,
  182.          "/*\n * Created by IBM PC LEX from file \"%s\"\n", infile);
  183.         if(sflag == 0)                  /* If "standalone" switch off */
  184.                 {
  185.                 fprintf(llout,
  186.                         " *\t- for use with standard I/O\n */\n");
  187.                 fprintf(llout, "\n#include <stdio.h>\n");
  188.                 }
  189.         else
  190.                 fprintf(llout, " *\t- for use with stand-alone I/O\n */\n");
  191.  
  192.         fprintf(llout, "#include <lex.h>\n");
  193.         fprintf(llout, "#define LL16BIT int\n");
  194.         fprintf(llout, "extern int _lmov%c();\n",
  195.                         (ndfa <= 255) ? 'b' : 'i');
  196.         fprintf(llout, "extern struct lextab *_tabp;\n");
  197.         fprintf(llout, "extern int lexval, yyline;\n");
  198.         fprintf(llout, "extern char lbuf[], *llend;\n");
  199. }
  200.  
  201. dfawrite()
  202. {
  203.         register struct move *dp;
  204.         register i, a;
  205.         struct dfa *st, *def;
  206.         struct set *xp;
  207.         char *xcp;
  208.  
  209.         setline();
  210.         fprintf(llout,
  211.                 "\n#define\tLLTYPE1\t%s\n", ndfa<=255? "char": "LL16BIT");
  212.         vstart("LLTYPE1 _N%s", tabname);
  213.         for (i = 0; i <= llnxtmax; i++)
  214.                 if (xp = move[i].m_next)
  215.                         vel(" %d,", xp->s_state-dfa); else
  216.                         vel(" %d,", ndfa);
  217.         vend();
  218.  
  219.         vstart("LLTYPE1 _C%s", tabname);
  220.         for (i = 0; i <= llnxtmax; i++)
  221.                 if (st = move[i].m_check)
  222.                         vel(" %d,", st-dfa); else
  223.                         vel(" %d,", -1);
  224.         vend();
  225.         vstart("LLTYPE1 _D%s", tabname);
  226.         for (i = 0; i < ndfa; i++)
  227.                 if (def = dfa[i].df_default)
  228.                         vel(" %d,", def-dfa); else
  229.                         vel(" %d,", ndfa); /* refer to blocking state */
  230.         vend();
  231.         vstart("LL16BIT _B%s", tabname);
  232.         for (i = 0; i < ndfa; i++)
  233.                 if (dp = dfa[i].df_base)
  234.                         vel(" %d,", dp-move); else
  235.                         vel(" %d,", 0);
  236.         vel(" %d,", 0); /* for blocking state */
  237.         vend();
  238.         if (nlook) {
  239.                 fprintf(llout, "char    *llsave[%d];\n", nlook);
  240.                 vstart("LL16BIT _L%s", tabname);
  241.                 a = nlook<=NBPC? NCHARS-1: -1;
  242.                 for (i = 0; i < ndfa; i++)
  243.                         vel(" 0%o,", dfa[i].df_name->s_look&a);
  244.                 vel(" %d,", 0);
  245.                 vend();
  246.         }
  247.         dospccl(ignore, "LLIGN", "X");
  248.         dospccl(breakc, "LLBRK", "Y");
  249.         dospccl(illeg, "LLILL", "Z");
  250.  
  251.         strcpy(ptabnam, tabname); strcat(ptabnam,",");
  252.         fprintf(llout, strdec1, tabname);
  253.         fprintf(llout, strdec2, ndfa);
  254.         fprintf(llout, strdec3, ptabnam);
  255.         fprintf(llout, strdec4, ptabnam);
  256.         fprintf(llout, strdec5, ptabnam);
  257.         fprintf(llout, strdec6, ptabnam);
  258.         fprintf(llout, strdec7, llnxtmax);
  259.         fprintf(llout, strdec8, ndfa<=255?"_lmovb":"_lmovi");
  260.         fprintf(llout, strdec9, ptabnam);
  261.         fprintf(llout, strdec10, ptabnam);
  262.         fprintf(llout, strdec11, nlook?"_L":"", nlook?ptabnam:"NULL,   ");
  263.         refccl(ignore, "Ignore", "X");
  264.         refccl(breakc, "Break", "Y");
  265.         refccl(illeg, "Illegal", "Z");
  266.         fprintf(llout, "\t\t\t};\n");
  267.         if(sflag == 0)                  /* If stdio flavor */
  268.                 {
  269.                 fprintf(llout, "\n/* Standard I/O selected */\n");
  270. /*
  271.                 fprintf(llout, "extern FILE lexin;\n\n");
  272. */
  273.                 fprintf(llout, "llstin()\n   {\n   if(lexin == NULL)\n");
  274.                 fprintf(llout, "      lexin = stdin;\n");
  275.                 }
  276.         else                            /* Stand-alone flavor */
  277.                 {
  278.                 fprintf(llout, "\n/* Stand-alone selected */\n");
  279.                 fprintf(llout, "\llstin()\n   {\n");
  280.                 }
  281.         fprintf(llout, "   if(_tabp == NULL)\n");
  282.         fprintf(llout, "      lexswitch(&%s);\n   }\n\n", tabname);
  283.         fclose(llout);
  284. }
  285.  
  286. dospccl(cp, s, tag)
  287. register char *cp;
  288. char *s, *tag;
  289. {
  290.         register n;
  291.         char cclnam[16];
  292.  
  293.         if (cp==0)
  294.                 return;
  295.         fprintf(llout, "#define\t%s\t%s\n", s, tag);
  296.         strcpy(cclnam, tag); strcat(cclnam, tabname);
  297.         vstart("char _%s", cclnam);
  298.         for (n = sizeof(ccls[0]); n--;)
  299.  
  300.                 vel(" 0%o,", *cp++&0377);
  301.         vend();
  302. }
  303.  
  304. refccl(cp, nm, tag)
  305. char *cp, *nm, *tag;
  306. {
  307.         if (cp==0)
  308.                 fprintf(llout, "\t\t\t0,\t\t/* No %s class */\n", nm);
  309.         else
  310.                 fprintf(llout, "\t_%s%s,\t/* %s class */\n", tag, tabname, nm);
  311. }
  312.  
  313. int     vnl;
  314.  
  315. vstart(fmt, str)
  316. char *fmt;
  317. char *str;              /*** WATCH IT ***/
  318. {
  319.         vnl = 0;
  320.         putc('\n', llout);
  321.         fprintf(llout, fmt, str);
  322.         fprintf(llout, "[] =\n   {\n  ");
  323. }
  324.  
  325. vend()
  326. {
  327.         fprintf(llout, "\n   };\n");
  328. }
  329.  
  330. vel(fmt, val)
  331. char *fmt;
  332. int val;                /*** WATCH IT ***/
  333. {
  334.         fprintf(llout, fmt, val);
  335.         if ((++vnl&07)==0)
  336.                 fprintf(llout, "\n  ");
  337. }
  338.