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. 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 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 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 lexval, yyline;\n");
  196.         fprintf(llout, "extern char lbuf[], *llend;\n");
  197. }
  198.  
  199. dfawrite()
  200. {
  201.         register struct move *dp;
  202.         register i, a;
  203.         struct dfa *st, *def;
  204.         struct set *xp;
  205.         char *xcp;
  206.  
  207.         setline();
  208.         fprintf(llout,
  209.                 "\n#define\tLLTYPE1\t%s\n", ndfa<=255? "char": "LL16BIT");
  210.         vstart("LLTYPE1 _N%s", tabname);
  211.         for (i = 0; i <= llnxtmax; i++)
  212.                 if (xp = move[i].m_next)
  213.                         vel(" %d,", xp->s_state-dfa); else
  214.                         vel(" %d,", ndfa);
  215.         vend();
  216.         vstart("LLTYPE1 _C%s", tabname);
  217.         for (i = 0; i <= llnxtmax; i++)
  218.                 if (st = move[i].m_check)
  219.                         vel(" %d,", st-dfa); else
  220.                         vel(" %d,", -1);
  221.         vend();
  222.         vstart("LLTYPE1 _D%s", tabname);
  223.         for (i = 0; i < ndfa; i++)
  224.                 if (def = dfa[i].df_default)
  225.                         vel(" %d,", def-dfa); else
  226.                         vel(" %d,", ndfa); /* refer to blocking state */
  227.         vend();
  228.         vstart("LL16BIT _B%s", tabname);
  229.         for (i = 0; i < ndfa; i++)
  230.                 if (dp = dfa[i].df_base)
  231.                         vel(" %d,", dp-move); else
  232.                         vel(" %d,", 0);
  233.         vel(" %d,", 0); /* for blocking state */
  234.         vend();
  235.         if (nlook) {
  236.                 fprintf(llout, "char    *llsave[%d];\n", nlook);
  237.                 vstart("LL16BIT _L%s", tabname);
  238.                 a = nlook<=NBPC? NCHARS-1: -1;
  239.                 for (i = 0; i < ndfa; i++)
  240.                         vel(" 0%o,", dfa[i].df_name->s_look&a);
  241.                 vel(" %d,", 0);
  242.                 vend();
  243.         }
  244.         dospccl(ignore, "LLIGN", "X");
  245.         dospccl(breakc, "LLBRK", "Y");
  246.         dospccl(illeg, "LLILL", "Z");
  247.  
  248.         strcpy(ptabnam, tabname); strcat(ptabnam,",");
  249.         fprintf(llout, strdec1, tabname);
  250.         fprintf(llout, strdec2, ndfa);
  251.         fprintf(llout, strdec3, ptabnam);
  252.         fprintf(llout, strdec4, ptabnam);
  253.         fprintf(llout, strdec5, ptabnam);
  254.         fprintf(llout, strdec6, ptabnam);
  255.         fprintf(llout, strdec7, llnxtmax);
  256.         fprintf(llout, strdec8, ndfa<=255?"_lmovb":"_lmovi");
  257.         fprintf(llout, strdec9, ptabnam);
  258.         fprintf(llout, strdec10, ptabnam);
  259.         fprintf(llout, strdec11, nlook?"_L":"", nlook?ptabnam:"NULL,   ");
  260.         refccl(ignore, "Ignore", "X");
  261.         refccl(breakc, "Break", "Y");
  262.         refccl(illeg, "Illegal", "Z");
  263.         fprintf(llout, "\t\t\t};\n");
  264.         if(sflag == 0)                  /* If stdio flavor */
  265.                 {
  266.                 fprintf(llout, "\n/* Standard I/O selected */\n");
  267. /*
  268.                 fprintf(llout, "extern FILE lexin;\n\n");
  269. */
  270.                 fprintf(llout, "llstin()\n   {\n   if(lexin == NULL)\n");
  271.                 fprintf(llout, "      lexin = stdin;\n");
  272.                 }
  273.         else                            /* Stand-alone flavor */
  274.                 {
  275.                 fprintf(llout, "\n/* Stand-alone selected */\n");
  276.                 fprintf(llout, "\llstin()\n   {\n");
  277.                 }
  278.         fprintf(llout, "   if(_tabp == NULL)\n");
  279.         fprintf(llout, "      lexswitch(&%s);\n   }\n\n", tabname);
  280.         fclose(llout);
  281. }
  282.  
  283. dospccl(cp, s, tag)
  284. register char *cp;
  285. char *s, *tag;
  286. {
  287.         register n;
  288.         char cclnam[16];
  289.  
  290.         if (cp==0)
  291.                 return;
  292.         fprintf(llout, "#define\t%s\t%s\n", s, tag);
  293.         strcpy(cclnam, tag); strcat(cclnam, tabname);
  294.         vstart("char _%s", cclnam);
  295.         for (n = sizeof(ccls[0]); n--;)
  296.                 vel(" 0%o,", *cp++&0377);
  297.         vend();
  298. }
  299.  
  300. refccl(cp, nm, tag)
  301. char *cp, *nm, *tag;
  302. {
  303.         if (cp==0)
  304.                 fprintf(llout, "\t\t\t0,\t\t/* No %s class */\n", nm);
  305.         else
  306.                 fprintf(llout, "\t_%s%s,\t/* %s class */\n", tag, tabname, nm);
  307. }
  308.  
  309. int     vnl;
  310.  
  311. vstart(fmt, str)
  312. char *fmt;
  313. char *str;              /*** WATCH IT ***/
  314. {
  315.         vnl = 0;
  316.         putc('\n', llout);
  317.         fprintf(llout, fmt, str);
  318.         fprintf(llout, "[] =\n   {\n  ");
  319. }
  320.  
  321. vend()
  322. {
  323.         fprintf(llout, "\n   };\n");
  324. }
  325.  
  326. vel(fmt, val)
  327. char *fmt;
  328. int val;                /*** WATCH IT ***/
  329. {
  330.         fprintf(llout, fmt, val);
  331.         if ((++vnl&07)==0)
  332.                 fprintf(llout, "\n  ");
  333. }
  334.