home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 December / simtel1292_SIMTEL_1292_Walnut_Creek.iso / msdos / c / lex.arc / OUT1.C < prev    next >
Text File  |  1986-03-13  |  12KB  |  335 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. char ptabnam[] = { "         " };
  63.  
  64. /*
  65.  * Print the minimised DFA, and at the same time, construct the vector which
  66.  * indicates final states by associating them with their translation index.
  67.  * (DFA printout supressed ifndef DEBUG.)
  68.  */
  69. dfaprint()
  70. {
  71.         register struct move *dp;
  72.         register struct dfa *st;
  73.         register int i;
  74.         int fi, k, l;
  75.  
  76.         vstart("LL16BIT _F%s", tabname);
  77. #ifdef DEBUG
  78.         fprintf(lexlog, "\nMinimised DFA for complete syntax\n");
  79. #endif
  80.         for (i = 0; i < ndfa; i++) {
  81. #ifdef DEBUG
  82.                 fprintf(lexlog, "\nstate %d", i);
  83. #endif
  84.                 st = &dfa[i];
  85.                 k = -1;
  86.                 if (fi = st->df_name->s_final) {
  87.                         k = nfa[fi].n_trans - trans;
  88. #ifdef DEBUG
  89.                         fprintf(lexlog, " (final %d[%d])", fi, k);
  90. #endif
  91.                         if (nfa[fi].n_flag&FLOOK) {
  92.                                 k |= (nfa[fi].n_look+1)<<11;
  93. #ifdef DEBUG
  94.                                 fprintf(lexlog, " restore %d", nfa[fi].n_look);
  95. #endif
  96.                         }
  97.                 }
  98.                 if (l = st->df_name->s_look)
  99. #ifdef DEBUG
  100.                         fprintf(lexlog, " look-ahead %o", l);
  101. #else
  102.                         ;
  103. #endif
  104.                 vel(" %d,", k);
  105. #ifdef MINIM_OK
  106.                 k = st->df_name->s_group->s_els[0];
  107.                 if (k!=i) {
  108.                         fprintf(lexlog, " deleted\n");
  109.                         continue;
  110.                 }
  111.  
  112.                 fprintf(lexlog, "\n");
  113.                 for (dp = st->df_base; dp < st->df_max; dp = range(st, dp))
  114.                 if (st->df_default)
  115.                       fprintf(lexlog, "\t.\tsame as %d\n", st->df_default-dfa);
  116. #endif
  117.         }
  118.         vel(" %d,", -1);        /* blocking state */
  119.         vend();
  120. }
  121.  
  122. #ifdef MINIM_OK
  123.  
  124. range(st, dp)
  125. register struct dfa *st;
  126. register struct move *dp;
  127. {
  128.         int low, high, last;
  129.         struct set *s;
  130.         register int a;
  131.  
  132.         while (dp < st->df_max && dp->m_check!=st)
  133.                 dp++;
  134. /***************************************************
  135.  * This always returns given the above statement ! *
  136.  ***************************************************/
  137.         if (dp >= st->df_max)
  138.                 return(dp);
  139.  
  140.         low = dp - st->df_base;
  141. /*
  142.         s = dp->m_next->s_group->s_els[0];
  143. */
  144.         s = dp->m_next;
  145.         for (last = low-1; dp < st->df_max &&
  146.                            dp->m_check==st &&
  147.                            (a = dp - st->df_base)==last+1 &&
  148. /*
  149.                            dp->m_next->s_state->s_els[0]==s; dp++)
  150. */
  151.                            dp->m_next==s; dp++)
  152.                 last = a;
  153.         high = last;
  154.         fprintf(lexlog, "\t");
  155.         if (high==low)
  156.                 chprint(low);
  157.         else {
  158.                 fprintf(lexlog, "[");
  159.                 if (high-low > 4) {
  160.                         chprint(low);
  161.                         fprintf(lexlog, "-");
  162.                         chprint(high);
  163.                 } else {
  164.                         while (low<=high)
  165.                                 chprint(low++);
  166.                 }
  167.                 fprintf(lexlog, "]");
  168.         }
  169.         if (s->s_state==NULL)
  170.                 fprintf(lexlog, "\tNULL\n"); else
  171.                 fprintf(lexlog, "\t%d\n", s->s_state-dfa);
  172.         return(dp);
  173. }
  174. #endif
  175.  
  176. heading()
  177. {
  178.         char *ver;
  179.         fprintf(llout,
  180.          "\/\*\n * Created by IBM PC LEX from file \"%s\"\n", infile);
  181.         if(sflag == 0)                  /* If "standalone" switch off */
  182.                 {
  183.                 fprintf(llout,
  184.                         " *\t- for use with standard I/O\n */\n");
  185.                 fprintf(llout, "\n#include <stdio.h>\n");
  186.                 }
  187.         else
  188.                 fprintf(llout, " *\t- for use with stand-alone I/O\n */\n");
  189.  
  190.         fprintf(llout, "#include <lex.h>\n");
  191.         fprintf(llout, "#define LL16BIT int\n");
  192.         fprintf(llout, "extern int _lmov%c();\n",
  193.                         (ndfa <= 255) ? 'b' : 'i');
  194.         fprintf(llout, "extern struct lextab *_tabp;\n");
  195.                 fprintf(llout, "extern int  yyline;\n");
  196.                 fprintf(llout, "extern char *llend;\n");
  197.                 fprintf(llout, "int lexval;\n");
  198.                 fprintf(llout, "char lbuf[];\n");
  199. }
  200.  
  201. dfawrite()
  202. {
  203.         register struct move *dp;
  204.         register int 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.         vstart("LLTYPE1 _C%s", tabname);
  219.         for (i = 0; i <= llnxtmax; i++)
  220.                 if (st = move[i].m_check)
  221.                         vel(" %d,", st-dfa); else
  222.                         vel(" %d,", -1);
  223.         vend();
  224.         vstart("LLTYPE1 _D%s", tabname);
  225.         for (i = 0; i < ndfa; i++)
  226.                 if (def = dfa[i].df_default)
  227.                         vel(" %d,", def-dfa); else
  228.                         vel(" %d,", ndfa); /* refer to blocking state */
  229.         vend();
  230.         vstart("LL16BIT _B%s", tabname);
  231.         for (i = 0; i < ndfa; i++)
  232.                 if (dp = dfa[i].df_base)
  233.                         vel(" %d,", dp-move); else
  234.                         vel(" %d,", 0);
  235.         vel(" %d,", 0); /* for blocking state */
  236.         vend();
  237.         if (nlook) {
  238.                 fprintf(llout, "char    *llsave[%d];\n", nlook);
  239.                 vstart("LL16BIT _L%s", tabname);
  240.                 a = nlook<=NBPC? NCHARS-1: -1;
  241.                 for (i = 0; i < ndfa; i++)
  242.                         vel(" 0%o,", dfa[i].df_name->s_look&a);
  243.                 vel(" %d,", 0);
  244.                 vend();
  245.         }
  246.         dospccl(ignore, "LLIGN", "X");
  247.         dospccl(breakc, "LLBRK", "Y");
  248.         dospccl(illeg, "LLILL", "Z");
  249.  
  250.         strcpy(ptabnam, tabname); strcat(ptabnam,",");
  251.         fprintf(llout, strdec1, tabname);
  252.         fprintf(llout, strdec2, ndfa);
  253.         fprintf(llout, strdec3, ptabnam);
  254.         fprintf(llout, strdec4, ptabnam);
  255.         fprintf(llout, strdec5, ptabnam);
  256.         fprintf(llout, strdec6, ptabnam);
  257.         fprintf(llout, strdec7, llnxtmax);
  258.         fprintf(llout, strdec8, ndfa<=255?"_lmovb":"_lmovi");
  259.         fprintf(llout, strdec9, ptabnam);
  260.         fprintf(llout, strdec10, ptabnam);
  261.         fprintf(llout, strdec11, nlook?"_L":"", nlook?ptabnam:"NULL,   ");
  262.         refccl(ignore, "Ignore", "X");
  263.         refccl(breakc, "Break", "Y");
  264.         refccl(illeg, "Illegal", "Z");
  265.         fprintf(llout, "\t\t\t};\n");
  266.         if(sflag == 0)                  /* If stdio flavor */
  267.                 {
  268.                 fprintf(llout, "\n/* Standard I/O selected */\n");
  269.  
  270.                 fprintf(llout, "FILE *lexin;\n\n");
  271.  
  272.                 fprintf(llout, "llstin()\n   {\n   if(lexin == NULL)\n");
  273.                 fprintf(llout, "      lexin = stdin;\n");
  274.                 }
  275.         else                            /* Stand-alone flavor */
  276.                 {
  277.                 fprintf(llout, "\n/* Stand-alone selected */\n");
  278.                 fprintf(llout, "\llstin()\n   {\n");
  279.                 }
  280.         fprintf(llout, "   if(_tabp == NULL)\n");
  281.         fprintf(llout, "      lexswitch(&%s);\n   }\n\n", tabname);
  282. }
  283.  
  284. dospccl(cp, s, tag)
  285. register char *cp;
  286. char *s, *tag;
  287. {
  288.         register int n;
  289.         char cclnam[16];
  290.  
  291.         if (cp==0)
  292.                 return;
  293.         fprintf(llout, "#define\t%s\t%s\n", s, tag);
  294.         strcpy(cclnam, tag); strcat(cclnam, tabname);
  295.         vstart("char _%s", cclnam);
  296.         for (n = sizeof(ccls[0]); n--;)
  297.                 vel(" 0%o,", *cp++&0377);
  298.         vend();
  299. }
  300.  
  301. refccl(cp, nm, tag)
  302. char *cp, *nm, *tag;
  303. {
  304.         if (cp==0)
  305.                 fprintf(llout, "\t\t\t0,\t\t/* No %s class */\n", nm);
  306.         else
  307.                 fprintf(llout, "\t_%s%s,\t/* %s class */\n", tag, tabname, nm);
  308. }
  309.  
  310. int     vnl;
  311.  
  312. vstart(fmt, str)
  313. char *fmt;
  314. char *str;              /*** WATCH IT ***/
  315. {
  316.         vnl = 0;
  317.         putc('\n', llout);
  318.         fprintf(llout, fmt, str);
  319.         fprintf(llout, "[] =\n   {\n  ");
  320. }
  321.  
  322. vend()
  323. {
  324.         fprintf(llout, "\n   };\n");
  325. }
  326.  
  327. vel(fmt, val)
  328. char *fmt;
  329. int val;                /*** WATCH IT ***/
  330. {
  331.         fprintf(llout, fmt, val);
  332.         if ((++vnl&07)==0)
  333.                 fprintf(llout, "\n  ");
  334. }
  335.